Android Bluetooth developmentrecently do Bluetooth car, need the Android side to control the movement of the car. The development process is documented in this article.
Use HC-06 wireless Bluetooth serial pass module. For other Bluetooth devices the same applies to this article.
the process of Bluetooth development:
get the local Bluetooth adapter-turn on Bluetooth--Search device--connect device--Send Message
first, in order to avoid the previous we write Bluetooth permissions:
<uses-permission android:name= "Android.permission.BLUETOOTH"/><uses-permission android:name= " Android.permission.BLUETOOTH_ADMIN "/>
Bluetooth object to use:
Private Bluetoothadapter adapter = null;//used to get the Bluetooth adapter private bluetoothdevice Mbtdevice = null;//used to get the Bluetooth device private Bluetoothsocket Mbtsocket = null;//used to establish communication
Get the Bluetooth adapter:
adapter = Bluetoothadapter.getdefaultadapter ();
Turn on bluetooth:
Boolean enabled = Adapter.enable (); if (!enabled) { adapter.enable ();}
Search Device:
Adapter.startdiscovery ();
The search device is returned as a broadcast, so we need to define a broadcast receiver:
Private Broadcastreceiver Bluerevever = new Broadcastreceiver () {@Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method stubstring action = Intent.getaction (); if (Action.equals (bluetoothdevice.action_ FOUND) {Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device); if (Device.getbondstate () !=bluetoothdevice.bond_bonded) {//Get the device name and MAC address//to the MAC address of the bluetooth device that was searched, get the device Mbtdevice = Adapter.getremotedevice ( Device.getaddress ()); Assuming that the device name is the specified device, give the hint if (Device.getname (). Equals ("HC-06")) {Toast.maketext (Mainactivity.this,device.getname ()), Toast.length_long). Show ();}} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals (ACTION)) {Toast.maketext (mainactivity.this, "Test Complete", Toast.length_long). Show ();}}};
the broadcast returns the different devices and the state in which they are located. The Getaction () method is used to get the state, bond_bonded represents the state that is already paired. (Note that pairing and connection are two completely different concepts, the pairing success is not successful, only the pairing success can not send information)
of course, the broadcasting mechanism should pay attention to the registration of radio:
Intentfilter filter = new Intentfilter (bluetoothdevice.action_found); Registerreceiver (bluerevever, filter); Filter = new Intentfilter (bluetoothadapter.action_discovery_finished); Registerreceiver (bluerevever, filter);
Connecting devices:
since blocking the connection will clog the thread, we need to re-open a new thread to establish the connection:
Private class Clientthread extends thread{public void Run () { try { //Cancel the action of the search device. Otherwise the next device connection will fail adapter.canceldiscovery (); Get socket Mbtsocket = Mbtdevice.createrfcommsockettoservicerecord (uuid.fromstring (") based on device 00001101-0000-1000-8000-00805F9B34FB ")); Connect socket mbtsocket.connect (); } catch (IOException e) { //TODO auto-generated catch block E.printstacktrace (); Toast.maketext (Mainactivity.this, "Connection Failed!!!!
!
!!! ", Toast.length_long). Show ();}}}
The UUID here is useful for connecting HC-06. Other devices are not tested.
when you want to use just create a Clientthread object and then run its run () method, for example, the following:
The process of creating the connection thread Mbtclientconnectthread = new Clientthread (); Open process Mbtclientconnectthread.start ();
Send message:
public void Sendmessagehandle (String msg) { if (Mbtsocket = = null) { Toast.maketext ( Mainactivity.this, "no connection!. ", Toast.length_long). Show (); return; } try { OutputStream os = Mbtsocket.getoutputstream (); Os.write (Msg.getbytes ()); The value sent is: Msg toast.maketext (mainactivity.this, "Send success!!
", Toast.length_long). Show (); } catch (IOException e) { //TODO auto-generated catch block e.printstacktrace (); Toast.maketext (Mainactivity.this, "Send failed!
!!
。!!
!!!
", Toast.length_long). Show (); } }
The UUID here is useful for connecting HC-06, and other devices are not tested
Android Bluetooth development