public void readManufacturer(){
BluetoothGattService manufacturerService = mBluetoothGatt.getService(UUID.fromString(MANUFACTURER_SERVICE));
if (manufacturerService != null) {
//Log.d("demo", String.valueOf(manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC))));
BluetoothGattCharacteristic manufacturerCharacteristic = manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC));
Log.d("demo", String.valueOf(manufacturerCharacteristic));
Log.d("demo", String.valueOf(manufacturerCharacteristic != null));
if (manufacturerCharacteristic != null) {
Log.d("demo","intoNotify");
setCharacteristicNotification(manufacturerCharacteristic,true);
}
}
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,boolean enable){
Log.d("demo", "setCharacteristicNotification");
if(mBluetoothAdapter == null || mBluetoothGatt == null){
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
if(UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())){
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
打出來的Log:
08-11 14:58:54.594 4768-4768/com.example.chongyanghu.mybletest D/demo: readBatteryAction
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: android.bluetooth.BluetoothGattCharacteristic@1893971
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: true
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: intoNotify
08-11 14:58:54.595 4768-4768/com.example.chongyanghu.mybletest D/demo: setCharacteristicNotification
進入了setCharacteristicNotification,為什麼沒有回調onCharacteristicChanged方法,求大牛幫忙看看~
自己解決了
public void readManufacturer() {
BluetoothGattService manufacturerService = mBluetoothGatt.getService(UUID.fromString(MANUFACTURER_SERVICE));
if (manufacturerService != null) {
//Log.d("demo", String.valueOf(manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC))));
BluetoothGattCharacteristic manufacturerCharacteristic = manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC));
Log.d("demo", "manufacturerCharacteristic = " + String.valueOf(manufacturerCharacteristic != null));
final int charaProp = manufacturerCharacteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
Log.d("demo", "intoNotify--->" + String.valueOf((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0));
readCharacteristic(manufacturerCharacteristic);
}
}
}