/** * Adds a {@link BluetoothGattCharacteristic} with specified parameters. * * @param uuid characteristic UUID * @param data locally stored value of the characteristic * @param properties OR-ed {@link BluetoothGattCharacteristic} property constants * @param descriptors list of characteristic descriptors. Use {@link DescriptorsBuilder} to create them. */ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid, @NonNull byte[] data, int properties, List<BluetoothGattDescriptor> descriptors) { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(uuid, properties, 0); for (BluetoothGattDescriptor descriptor : descriptors) { characteristic.addDescriptor(descriptor); } characteristic.setValue(data); this.bluetoothGattCharacteristics.add(characteristic); return this; }
@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { BluetoothGattDescriptor bluetoothGattDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); if (bluetoothGattDescriptor == null) { //adding notification descriptor if not present bluetoothGattDescriptor = new BluetoothGattDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID, 0); bluetoothGattCharacteristic.addDescriptor(bluetoothGattDescriptor); } return bluetoothGattDescriptor; } });
private BluetoothGattCharacteristic createReadNotifyChar(UUID uuid){ BluetoothGattCharacteristic temp = new BluetoothGattCharacteristic(uuid, BluetoothGattCharacteristic.PROPERTY_READ|BluetoothGattCharacteristic .PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ| BluetoothGattCharacteristic.PERMISSION_WRITE); temp.addDescriptor(new BluetoothGattDescriptor(UUID.fromString ("00002902-0000-1000-8000-00805f9b34fb"), BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE)); return temp; }
/** * Setup Battery Service * * @return the service */ private static BluetoothGattService setUpBatteryService() { final BluetoothGattService service = new BluetoothGattService(SERVICE_BATTERY, BluetoothGattService.SERVICE_TYPE_PRIMARY); // Battery Level final BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic( CHARACTERISTIC_BATTERY_LEVEL, BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED); final BluetoothGattDescriptor clientCharacteristicConfigurationDescriptor = new BluetoothGattDescriptor( DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE); clientCharacteristicConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor); while (!service.addCharacteristic(characteristic)); return service; }
/** * Setup Battery Service * * @return the service */ private BluetoothGattService setUpBatteryService() { final BluetoothGattService service = new BluetoothGattService(SERVICE_BATTERY, BluetoothGattService.SERVICE_TYPE_PRIMARY); // Battery Level final BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic( CHARACTERISTIC_BATTERY_LEVEL, BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED); final BluetoothGattDescriptor clientCharacteristicConfigurationDescriptor = new BluetoothGattDescriptor( DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE); clientCharacteristicConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor); service.addCharacteristic(characteristic); return service; }
characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor); characteristic.addDescriptor(reportReferenceDescriptor); DESCRIPTOR_REPORT_REFERENCE, BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED); characteristic.addDescriptor(descriptor); DESCRIPTOR_REPORT_REFERENCE, BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED); characteristic.addDescriptor(descriptor);
characteristic.addDescriptor(clientCharacteristicConfigurationDescriptor); characteristic.addDescriptor(reportReferenceDescriptor); DESCRIPTOR_REPORT_REFERENCE, BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED); characteristic.addDescriptor(descriptor); DESCRIPTOR_REPORT_REFERENCE, BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED); characteristic.addDescriptor(descriptor);
characteristicRead.addDescriptor(new BluetoothGattDescriptor(UUID_DESC_NOTITY, BluetoothGattCharacteristic.PERMISSION_WRITE)); service.addCharacteristic(characteristicRead);