Getting started with Bluetooth 4.0 ble
1. Android 4.3 and later versions are required for mobile phones
2. Permissions:
<Uses-Permission Android: Name = "android. Permission. Bluetooth"/>
<Uses-Permission Android: Name = "android. Permission. effecth_admin"/>
Uses-feature must be added for mobile phone installation that only supports ble.
<Uses-feature Android: Name = "android. Hardware. javasth_le" Android: required = "true"/>
3. Ble device Scan
3.1 obtain the Bluetooth adapter: mblustmthadapter = descrithmanager. getadapter ();
3.2 start scanning: mblustmthadapter. startlescan (mlescancallback );
During scanning, the lescancallback callback interface must be implemented to return scan results:
Protected lescancallback mlescancallback = new descrithadapter. lescancallback () {@ overridepublic void onlescan (final effecthdevice device, final int Arg, byte [] scanrecord) {log. D (TAG, "Find device->" + device. getname () + "Mac:" + device. getaddress (); runonuithread (New runnable () {public void run () {string MAC = device. getaddress (); string name = device. getname (); // deviceadapter. adddevice (New effecthitem (name, Mac); deviceadapter. adddevice (New effecthitem (name, Mac, and Arg); deviceadapter. notifydatasetchanged ();}});}};
3.3 after obtaining the scan result, you can connect the device through the obtained device address. On the connection, you will get a descrithgatt for the operation. You need to implement the descrithgattcallback callback, which is used to return the result, such as the connection status and subsequent read/write operations
Final effecthdevice device = mblustmthadapter. getremotedevice (Address); If (device = NULL) {log. W (TAG, "device not found. unable to connect. "); Return false;} // We Want to directly connect to the device, so we are setting the // autoconnect // parameter to false. mbluw.thgatt = device. connectgatt (this, true, mgattcallback );
Mgattcallback callback function:
Private Final implements thgattcallback mgattcallback = new implements thgattcallback () {@ overridepublic void onconnectionstatechange (implements thgatt GATT, int status, int newstate) {If (newstate = implements thprofile. state_connected) {broadcastupdate (action_gatt_connected); log. I (TAG, "connected to GATT server. "); // attempts to discover services after successful connection. log. I (TAG, "attempting to start service discovery:" + mbluw.thgatt. discoverservices (); isconnectd = true;} else if (newstate = descrithprofile. state_disconnected) {log. I (TAG, "disconnected from GATT server. "); isconnectd = false; broadcastupdate (action_gatt_disconnected, GATT. getdevice (). getaddress ());}}//... omitted}
3.4 In a bluetooth device, it contains multiple thgattservices, and each gatethgattservice contains multiple gatethgattcharacteristic
After Bluetooth is connected, call mbluw.thgatt. discoverservices () to discover services on a bluetooth device. Then you can obtain the service list mbluw.thgatt in the device. getservices (); or you can use UUID to obtain a certain service named thgattservice gattservice = mbluw.thgatt. getservice (UUID );
In the service, call gattservice. getcharacteristics () to obtain the characteristic set ();
In characteristic, you can useMblustmthgatt. readcharacteristic (characteristic); to read the data in it. The result is obtained in the mgattcallback callback function.
Write data such:Characteristic. Setvalue (data );
Mbluw.thgatt. Wirtecharacteristic (mcurrentcharacteristic );
4. Close the connection mbluw.thgatt. Close ();