Android Bluetooth 4.0 development Introduction, android4.0
I have been studying a Bluetooth function recently because I am a newbie and have been learning it for a long time before I understand it. On the Internet, I copied Bluetooth 4.0, that is, a few dome files, which are all English annotations, for friends with poor English
It's really a hard nut to crack. There's no need to describe Lori. The key method interfaces are included ......... forget it. I will introduce my recent learning experiences. I will not talk about anything simple about enabling Bluetooth to search for Bluetooth or anything about Baidu android Bluetooth 4.0. after reading my blog, I will also introduce some important interface callbacks on the Internet.
What are some specific things in Bluetooth: Bluetooth name (name) Bluetooth address (adress) Bluetooth signal strength (riss) Bluetooth service (service) there is also a small service under the Bluetooth service, that is, the specific thing to do should be the other services we commonly use. I have never used them and I don't know. hey
Bluetooth communication process: Open the search (you can specify the uuid to find the device) connection, and do not talk about it. After the connection, first obtain the bluetooth service (found through the UUID) and then find the small service in the bluetooth service called bluetooth
Characteristic (also obtained through UUID), you can read and write it. There are three types of reading and writing, it seems that it is useless according to your own needs;
Next we will introduce
When thgattcallback was written on the Internet, there was no explanation at all. After finding it for a long time, I was so tired that I could use the device to adjust it one by one: the following code and comments contain data sending:
BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// Connection status trigger
If (newState = maid. STATE_CONNECTED ){
Log. d ("Bluetooth", "------- ConnectSucceed ------");
Descrithgatt. discoverServices (); // discovers a service
} Else if (newState = descrithprofile. STATE_DISCONNECTED ){
Log. d ("Bluetooth", "------- Disconnect ------");
If (export thadapter = null |! Descrithadapter. isEnabled ()){
Descrithadapter = descrithmanager. getAdapter ();
Optional thadapter. enable ();
Define thadapter. startLeScan (uuid, leScanCallback );
}
}
}
/**
* Device service callback discovered
*/
@ Override
Public void onServicesDiscovered (descrithgatt gatt, int status ){
If (status = descrithgatt. GATT_SUCCESS ){
Final byte [] buffer = new byte [5];
String open = "open = 1"; // sent data
Try {
Byte [] data = open. getBytes ("UTF-8 ");
WriteDataToDevice (data); // send (a separate method)
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
}
}
}
@ Override
Public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status ){
If (status = descrithgatt. GATT_SUCCESS ){
Log. d ("000", "-------- data sent successfully ----- status :");
}
}
@ Override
Public void onCharacteristicRead (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status ){
Log. d ("Bluetooth reply data is", characteristic. getValue (). toString ());
}
@ Override
Public void onCharacteristicChanged (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic ){
}
.....
};
/**
* Send data to the device
*/
Public void writeDataToDevice (byte [] data ){
If (then thgatt = null ){
Log. d ("00", "---------------- export thgatt = null -----------------------");
Return;
}
Required thgattservice = required thgatt
. GetService (BLURTOOTH_SERVICE); // UUID of the service to be used in the bluetooth device
If (descrithgattservice = null ){
Log. d ("pcg", "service not found! ");
Descrithgatt. disconnect ();
Return;
}
Descrithgattcharacteristic mCharac = descrithgattservice
. GetCharacteristic (BLUETOOTH_CHARACTERISTIC); // UUID of a specific feature of the service
If (mCharac = null ){
// The error message is not found.
Descrithgatt. disconnect ();
Return;
}
Descrithgatt. setCharacteristicNotification (mCharac, true );
Log. d ("pcg", "data:" + Arrays. toString (data ));
MCharac. setValue (data); // you can specify the data to be sent.
Try {
Thread. sleep (200 );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
WriteCharacteristic (mCharac );
}
Here is the introduction. After reading this download of dome, it will be clearer. let's take a look at the development process. If you understand it, you can use it flexibly, by doing this function, we found that all the dome in the world was written by one person. It was all in English. I began to suspect that everyone was just copying the official dome. Even the interaction was a big piece of broadcast. I will introduce it here. Do not spray it if it is not easy to write!