Android Bluetooth developmentrecently do Bluetooth car, need the Android side to control the movement of the car, this article records the development process. Use HC-06 wireless Bluetooth serial pass module. This is also true for other Bluetooth devices.
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 not paired///based on the MAC address of the Bluetooth device that was found, get the device Mbtdevice = Adapter.getremotedevice ( Device.getaddress ()); If the device name is the specified device, give the prompt 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, "detected", Toast.length_long). Show ();}}};
The broadcast returns the different devices and their state, the Getaction () method is used to get the state, and the bond_bonded represents the state that is already paired. (Note that pairing and connection are two completely different concepts, pairing success is not a successful connection, only successful pairing can not send information)
of course, the broadcasting mechanism should pay attention to the registration of broadcasting:
Intentfilter filter = new Intentfilter (bluetoothdevice.action_found); Registerreceiver (bluerevever, filter); Filter = new Intentfilter (bluetoothadapter.action_discovery_finished); Registerreceiver (bluerevever, filter);
Connecting devices:
because blocking the connection will block 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 or 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, and other devices are not tested
to use it, simply create a Clientthread object and then execute its run () method, as follows:
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Bluetooth development