protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid, boolean enable) { if (IS_DEBUG) Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID=" + characteristicUuid + ", enable=" + enable + " )"); BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); gatt.setCharacteristicNotification(characteristic, enable); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 }); return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? }
@Override protected boolean startOperation(BluetoothGatt bluetoothGatt) { bluetoothGattDescriptor.setValue(data); /* * According to the source code below Android 7.0.0 the BluetoothGatt.writeDescriptor() function used * writeType of the parent BluetoothCharacteristic which caused operation failure (for instance when * setting Client Characteristic Config). With WRITE_TYPE_DEFAULT problem did not occurred. * Compare: * https://android.googlesource.com/platform/frameworks/base/+/android-6.0.1_r74/core/java/android/bluetooth/BluetoothGatt.java#1039 * https://android.googlesource.com/platform/frameworks/base/+/android-7.0.0_r1/core/java/android/bluetooth/BluetoothGatt.java#947 */ final BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattDescriptor.getCharacteristic(); final int originalWriteType = bluetoothGattCharacteristic.getWriteType(); bluetoothGattCharacteristic.setWriteType(bluetoothGattCharacteristicDefaultWriteType); final boolean success = bluetoothGatt.writeDescriptor(bluetoothGattDescriptor); bluetoothGattCharacteristic.setWriteType(originalWriteType); return success; } }
@Override public void execute() { if (mBluetoothGatt == null || value == null || mBluetoothGattDescriptor == null) { Log.e(TAG, "Error: " + TAG + ".execute, object = null"); } else { mBluetoothGattDescriptor.setValue(value); if (!mBluetoothGatt.writeDescriptor(mBluetoothGattDescriptor)) { Log.e(TAG, "Error: " + TAG + " was not executed correctly: " + mBluetoothGattDescriptor.getUuid().toString()); } } }
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService gattService = gatt.getService(UUID.fromString(C5_OPENXC_BLE_SERVICE_UUID)); BluetoothGattCharacteristic gattCharacteristic = gattService.getCharacteristic(UUID.fromString(C5_OPENXC_BLE_CHARACTERISTIC_NOTIFY_UUID)); gatt.setCharacteristicNotification(gattCharacteristic, true); BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(C5_OPENXC_BLE_DESCRIPTOR_NOTIFY_UUID)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); } }
/** Will cancel an in-progress connection, and close the socket */ public void cancel() { gat.setCharacteristicNotification(cc,false); descriptor.setValue( BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); gat.writeDescriptor(descriptor); gat.disconnect(); gat.close(); Log.i("H7ConnectThread", "Closing HRsensor"); }
private void setCharacteristicNotification(BluetoothGatt bluetoothgatt, BluetoothGattCharacteristic bluetoothgattcharacteristic, boolean flag) { bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag); BluetoothGattDescriptor descriptor = bluetoothgattcharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); bluetoothgatt.writeDescriptor(descriptor); } }
public void setNotifyListener(UUID serviceUUID, UUID characteristicUUID, NotifyListener listener) { if (null == gatt) { Log.e(TAG, "실패"); return; } BluetoothGattCharacteristic chara = gatt.getService(serviceUUID).getCharacteristic(characteristicUUID); if (null == chara) { Log.e(TAG, "실패"); return; } this.gatt.setCharacteristicNotification(chara, true); BluetoothGattDescriptor descriptor = chara.getDescriptor(MibandUUID.UUID_DESCRIPTOR_UPDATE_NOTIFICATION); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); this.gatt.writeDescriptor(descriptor); this.notifyListeners.put(characteristicUUID, listener); }
private void setupDescriptorSuccess() { when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); when(gattCharacteristic.getDescriptor(eq(clientCharacteristicConfig))).thenReturn(gattDescriptor); when(gatt.setCharacteristicNotification(Mockito.eq(gattCharacteristic), Mockito.anyBoolean())).thenReturn(true); when(gatt.writeDescriptor(gattDescriptor)).thenReturn(true); }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting indication"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting indication"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting indication"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
@Test public void testWriteDescriptorFail() { when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); when(gattCharacteristic.getDescriptor(eq(clientCharacteristicConfig))).thenReturn(gattDescriptor); when(gatt.setCharacteristicNotification(Mockito.eq(gattCharacteristic), Mockito.anyBoolean())).thenReturn(true); when(gatt.writeDescriptor(gattDescriptor)).thenReturn(false); subscribeCommand.execute(device, operationCommandObserver, gatt); verifyCommandFail(subscribeCommand, BluetoothGatt.GATT_FAILURE); }
public void setNotify(View view) { BluetoothGattService service = getGattService(BleServerActivity.UUID_SERVICE); if (service != null) { // 设置Characteristic通知 BluetoothGattCharacteristic characteristic = service.getCharacteristic(BleServerActivity.UUID_CHAR_READ_NOTIFY);//通过UUID获取可通知的Characteristic mBluetoothGatt.setCharacteristicNotification(characteristic, true); // 向Characteristic的Descriptor属性写入通知开关,使蓝牙设备主动向手机发送数据 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(BleServerActivity.UUID_DESC_NOTITY); // descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);//和通知类似,但服务端不主动发数据,只指示客户端读取数据 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }