Android Bluetooth 2.0 method of using the detailed _android

Source: Internet
Author: User
Tags uuid

This article for you to share the Android Operation Bluetooth 2.0 use method, for everyone's reference, the specific content as follows

1.Android Operation Bluetooth 2.0 usage process
(1) Find device UUID
(2) Obtain the Bluetooth adapter, makes the Bluetooth in the discovery mode, obtains the socket of the next machine, and establishes the connection with the host computer, obtains obtains the input flow and the output stream, two flows are not empty, indicates the connection success. Otherwise, the connection fails.
(3). Communicate with the socket of the lower machine.
(4). Disconnect (Close the stream, close the socket) after the communication is over

2 Next connect the code directly:
2.1 Device UUID found (common vendors will provide to developers)

Copy Code code as follows:
UUID My_uuid = uuid.fromstring ("00001101-0000-1000-8000-00805F9B34FB");

2.2 Establishing a connection with the Bluetooth device

 Bluetoothadapter Mybluetoothadapter = null;//Bluetooth adapter Bluetoothserversocket mBThServer = n ull;//Upper Computer <span style= "font-family:arial, Helvetica, Sans-serif;" >ServerSocket</span> bluetoothsocket mbthsocket = socket InputStream Mminstream of null;//lower machine = null;//input stream output Stream mmoutstream = null;//output stream <span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); > </span> 
Mybluetoothadapter = Bluetoothadapter.getdefaultadapter ()//Get adapter set<bluetoothdevice> pairedDevices = Mybluetoothadapter. Getbondeddevices ()//Get all Bluetooth devices under the adapter if (paireddevices.size () > 0) {for (Iterator<blueto Othdevice> iterator = paireddevices. iterator (); Iterator.hasnext ();) 
    {Bluetoothdevice device = (bluetoothdevice) iterator. Next (); if (Device_name1.equals (Device.getname ()) | | Device_name2.equals (Device.getname ()) | | Device_name3.equals (Device.getname ()) | | Device_name4.equals (Device.getname ())) {try {mybluetoothadapter.enable ();//adapter settings available Intent disc Overableintent = new Intent (bluetoothadapter.action_request_discoverable);//make Bluetooth in discovery mode, duration 150s D 
                Iscoverableintent. Putextra (Bluetoothadapter.extra_discoverable_duration, 
        150); Mbthsocket = device. Createrfcommsockettoservicerecord (my_UUID)//Get the socket INT SDK of the next machine = Integer.parseint (Build.VERSION.SDK); 
        if (SDK >=) {mbthsocket = device. Createinsecurerfcommsockettoservicerecord (MY_UUID); 
        else {mbthsocket = device. Createrfcommsockettoservicerecord (MY_UUID); } mbthserver = Mybluetoothadapter. Listenusingrfcommwithservicerecord ("Myservers Ocket ", my_uuid); Monitor available equipment mbthsocket.connect (); Establish connection Mminstream = Mbthsocket.getinputstream ();//Get input stream mmoutstream = Mbthsocket.getoutputstream ();// Gets the output stream} catch (IOException e) {ett.settext ("Device connection Exception! 
      "); } if ((Mminstream!= null) && (mminstream!= null))//If the two are not empty, the connection fails {ett.settext ("set The standby connection is successful! 
      "); else {ett.settext ("Device connection failed!") 
      "); 
    } break; 
 } 
  } 
}

2.3 Start sending data, and read data (byte array)

 if ((Mminstream = null) | | (Mminstream = null)) { 
  Readflage = -2;//Connection exception return 
  ; 
} 
Mmoutstream.write (Cmd_find);//write Lookup instruction 
thread.sleep (200); 

Note:both Cmd_find and Recdata are byte array byte[].
The above code sends instructions and reads the data one at a time. It's simple.

2.4 Disconnect

if ((Mminstream = null) | | (Mminstream = null)) {return 
  ; 
} 
Close the stream and socket 
mmoutstream.close (); 
Mminstream.close (); 
Mbthsocket.close (); 
Mbthserver.close (); 

Finally summed up, the basic 3 strides, the first to establish a connection, the second send data read data, the third step disconnected. Today is all that, later will write about Bluetooth 4.0 ble in the use of Android, these two still have a lot of different, everybody please look forward to.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.