List of usage examples for android.bluetooth BluetoothGattCharacteristic getDescriptor
public BluetoothGattDescriptor getDescriptor(UUID uuid)
From source file:com.example.android.bluetoothlegatt.BluetoothLeService.java
/** * Enables or disables notification on a give characteristic. * * @param characteristic Characteristic to act on. * @param enabled If true, enable notification. False otherwise. *//*from ww w .ja v a 2 s . c o m*/ public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enabled)) { Log.e(TAG, "setCharacteristicNotification fail! enabled=" + enabled); } if ((UUID.fromString("0000fff4-0000-1000-8000-00805f9b34fb")).equals(characteristic.getUuid())) { //mBluetoothGatt.writeCharacteristic(characteristic); List<BluetoothGattDescriptor> descs = characteristic.getDescriptors(); for (int i = 0; i < descs.size(); i++) { BluetoothGattDescriptor desc = descs.get(i); desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); Log.d(TAG, "writeDescriptor notify, uuid=" + desc.getUuid().toString()); mBluetoothGatt.writeDescriptor(desc); } //BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); //descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); //mBluetoothGatt.writeDescriptor(descriptor); } // This is specific to Heart Rate Measurement. 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); } }
From source file:no.nordicsemi.android.nrftoolbox.dfu.DfuService.java
/** * Enables or disables the notifications for given characteristic. This method is SYNCHRONOUS and wait until the * {@link BluetoothGattCallback#onDescriptorWrite(BluetoothGatt, BluetoothGattDescriptor, int)} will be called or the connection state will change from {@link #STATE_CONNECTED_AND_READY}. If * connection state will change, or an error will occur, an exception will be thrown. * //ww w.j a v a 2 s. c o m * @param gatt * the GATT device * @param characteristic * the characteristic to enable or disable notifications for * @param enable * <code>true</code> to enable notifications, <code>false</code> to disable them * @throws DfuException * @throws UploadAbortedException */ private void setCharacteristicNotification(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic, final boolean enable) throws DeviceDisconnectedException, DfuException, UploadAbortedException { if (mConnectionState != STATE_CONNECTED_AND_READY) throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState); mErrorState = 0; if (mNotificationsEnabled == enable) return; logi((enable ? "Enabling " : "Disabling") + " notifications..."); // enable notifications locally gatt.setCharacteristicNotification(characteristic, enable); // enable notifications on the device final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); // We have to wait until device gets disconnected or an error occur try { synchronized (mLock) { while ((mNotificationsEnabled != enable && mConnectionState == STATE_CONNECTED_AND_READY && mErrorState == 0 && !mAborted) || mPaused) mLock.wait(); } } catch (final InterruptedException e) { loge("Sleeping interrupted", e); } if (mAborted) throw new UploadAbortedException(); if (mErrorState != 0) throw new DfuException("Unable to set notifications state", mErrorState); if (mConnectionState != STATE_CONNECTED_AND_READY) throw new DeviceDisconnectedException("Unable to set notifications state", mConnectionState); }