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 isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID); if (service != null) { mHeartRateCharacteristic = service.getCharacteristic(HEART_RATE_MEASUREMENT_CHARACTERISTIC_UUID); } return mHeartRateCharacteristic != null; }
@Override public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(CYCLING_SPEED_AND_CADENCE_SERVICE_UUID); if (service != null) { mCSCMeasurementCharacteristic = service.getCharacteristic(CSC_MEASUREMENT_CHARACTERISTIC_UUID); } return mCSCMeasurementCharacteristic != null; }
@Override public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(RUNNING_SPEED_AND_CADENCE_SERVICE_UUID); if (service != null) { mRSCMeasurementCharacteristic = service.getCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID); } return mRSCMeasurementCharacteristic != null; }
@Override protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService llService = gatt.getService(LINK_LOSS_SERVICE_UUID); if (llService != null) { mLinkLossCharacteristic = llService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); } return mLinkLossCharacteristic != null; }
@Override protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(HT_SERVICE_UUID); if (service != null) { mHTCharacteristic = service.getCharacteristic(HT_MEASUREMENT_CHARACTERISTIC_UUID); } return mHTCharacteristic != null; }
@Override protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { super.isOptionalServiceSupported(gatt); final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID); if (service != null) { mBodySensorLocationCharacteristic = service.getCharacteristic(BODY_SENSOR_LOCATION_CHARACTERISTIC_UUID); } return mBodySensorLocationCharacteristic != null; }
@Override protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { // Initialize Battery characteristic super.isOptionalServiceSupported(gatt); // TODO If there are some optional characteristics, initialize them there. final BluetoothGattService service = gatt.getService(SERVICE_UUID); if (service != null) { mOptionalCharacteristic = service.getCharacteristic(READABLE_CHARACTERISTIC_UUID); } return mOptionalCharacteristic != null; }
@Override protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(BP_SERVICE_UUID); if (service != null) { mBPMCharacteristic = service.getCharacteristic(BPM_CHARACTERISTIC_UUID); mICPCharacteristic = service.getCharacteristic(ICP_CHARACTERISTIC_UUID); } return mBPMCharacteristic != null; }
@Override protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { super.isOptionalServiceSupported(gatt); final BluetoothGattService iaService = gatt.getService(IMMEDIATE_ALERT_SERVICE_UUID); if (iaService != null) { mAlertLevelCharacteristic = iaService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); } return mAlertLevelCharacteristic != null; }
/** * This method should return true if the profile matches the given device. That means if the device has the required services. * @param gatt the GATT device * @return true if the device is supported by that profile, false otherwise. */ public static boolean matchDevice(final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID); return service != null && service.getCharacteristic(UART_TX_CHARACTERISTIC_UUID) != null && service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID) != null; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(EXPERIMENTAL_BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(EXPERIMENTAL_BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(GLS_SERVICE_UUID); if (service != null) { mGlucoseMeasurementCharacteristic = service.getCharacteristic(GM_CHARACTERISTIC); mGlucoseMeasurementContextCharacteristic = service.getCharacteristic(GM_CONTEXT_CHARACTERISTIC); mRecordAccessControlPointCharacteristic = service.getCharacteristic(RACP_CHARACTERISTIC); } return mGlucoseMeasurementCharacteristic != null && mRecordAccessControlPointCharacteristic != null; }
@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); } }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(DFU_CONTROL_POINT_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mControlPointCharacteristic = characteristic; mPacketCharacteristic = dfuService.getCharacteristic(DFU_PACKET_UUID); return mPacketCharacteristic != null; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(DFU_CONTROL_POINT_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mControlPointCharacteristic = characteristic; mPacketCharacteristic = dfuService.getCharacteristic(DFU_PACKET_UUID); return mPacketCharacteristic != null; }
byte[] data_to_write; // Your data BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SREVICE); BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter(); BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(....); BluetoothGatt mBG = mDevice.connectGatt(....); BluetoothGattService mSVC = mBG.getService(service_uuid); BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(characteristic_uuid); mCH.setValue(data_to_write); mBG.writeCharacteristic(mCH);
@Test public void testOnError() { when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); when(gatt.readCharacteristic(eq(gattCharacteristic))).thenReturn(true); readCommand.execute(device, operationCommandObserver, gatt); readCommand.onError(BluetoothGatt.GATT_FAILURE); verifyCommandFail(); }