這是Ble極簡系列的第二篇文章禽最,上一篇Android BLE低功耗藍(lán)牙開發(fā)極簡系列(一)之掃描與連接主要是掃描連接,這一篇主要是讀寫操作。
發(fā)現(xiàn)服務(wù)
在連接成功后局嘁,可以通過Gatt進行discoverServices()。
if (newState == BluetoothProfile.STATE_CONNECTED) {//當(dāng)藍(lán)牙設(shè)備已經(jīng)連接
//獲取ble設(shè)備上面的服務(wù)
Toast.makeText(MainActivity.this, "連接成功", Toast.LENGTH_SHORT).show();
Log.i("haha", "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
Log.d("haha", "onConnectionStateChange: " + "連接成功")
}
在mGattCallback 回調(diào)添加Servicest的相關(guān)回調(diào)
//發(fā)現(xiàn)服務(wù)回調(diào)晦墙。
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d("haha", "onServicesDiscovered: " + "發(fā)現(xiàn)服務(wù) : " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
//成功
}
}
讀寫開關(guān)
當(dāng)返回的status == BluetoothGatt.GATT_SUCCESS時导狡,進行讀寫以及通知相關(guān)的操作, 調(diào)用writeDescriptor()偎痛,注意設(shè)置setValue為ENABLE_INDICATION_VALUE旱捧,否則可能后續(xù)讀取不到數(shù)據(jù)。
if (status == BluetoothGatt.GATT_SUCCESS) {
//成功
isServiceConnected = true;
boolean serviceFound;
Log.d("haha", "onServicesDiscovered: " + "發(fā)現(xiàn)服務(wù) : " + status)
if (mBluetoothGatt != null && isServiceConnected) {
BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);
BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_NOTIFICATION);
boolean b = mBluetoothGatt.setCharacteristicNotification(characteristic, true);
if (b) {
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
for (BluetoothGattDescriptor descriptor : descriptors) {
boolean b1 = descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
if (b1) {
mBluetoothGatt.writeDescriptor(descriptor);
Log.d(TAG, "startRead: " + "監(jiān)聽收數(shù)據(jù)");
}
}
}
}
設(shè)置成功,會在onDescriptorWrite方法進行回調(diào)枚赡,注意UUID_SERVICE氓癌,UUID_NOTIFICATION特征值UUID,可以詢問公司固件端的開發(fā)人員贫橙,和開發(fā)人員配合修改贪婉。
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.d(TAG, "onDescriptorWrite: " + "設(shè)置成功");
}
發(fā)送數(shù)據(jù)
public void startSend(View view) {
if (mBluetoothGatt != null && isServiceConnected) {
BluetoothGattService gattService = mBluetoothGatt.getService(UUID_SERVICE);
BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(UUID_WRITE);
byte[] bytes = new byte[2];
bytes[0] = 04;
bytes[1] = 01;
characteristic.setValue(bytes);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mBluetoothGatt.writeCharacteristic(characteristic);
}
}
讀取數(shù)據(jù)
讀取數(shù)據(jù)在onCharacteristicChanged方法中,注意進制間的轉(zhuǎn)換卢肃。
@Override
public final void onCharacteristicChanged(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
byte[] value = characteristic.getValue();
Log.d(TAG, "onCharacteristicChanged: " + value);
String s0 = Integer.toHexString(value[0] & 0xFF);
String s = Integer.toHexString(value[1] & 0xFF);
Log.d(TAG, "onCharacteristicChanged: " + s0 + "疲迂、" + s);
for (byte b : value) {
Log.d(TAG, "onCharacteristicChanged: " + b);
}
}
斷開操作
if (mBluetoothGatt != null) {
mBluetoothGatt.close();
}
注意事項
一定要進行讀寫開關(guān)操作,注意descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)莫湘,否則可能讀取不到數(shù)據(jù)尤蒿。
Github
喜歡可以關(guān)注博主BleDemo