Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#addDescriptor()

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#addDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HealthThermometerServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 7 votes vote down vote up
public HealthThermometerServiceFragment() {
  mTemperatureMeasurementCharacteristic =
      new BluetoothGattCharacteristic(TEMPERATURE_MEASUREMENT_UUID,
          BluetoothGattCharacteristic.PROPERTY_INDICATE,
          /* No permissions */ 0);

  mTemperatureMeasurementCharacteristic.addDescriptor(
      Peripheral.getClientCharacteristicConfigurationDescriptor());

  mTemperatureMeasurementCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(TEMPERATURE_MEASUREMENT_DESCRIPTION));

  mMeasurementIntervalCharacteristic =
      new BluetoothGattCharacteristic(
          MEASUREMENT_INTERVAL_UUID,
          (BluetoothGattCharacteristic.PROPERTY_READ |
              BluetoothGattCharacteristic.PROPERTY_WRITE |
              BluetoothGattCharacteristic.PROPERTY_INDICATE),
          (BluetoothGattCharacteristic.PERMISSION_READ |
              BluetoothGattCharacteristic.PERMISSION_WRITE));

  mMeasurementIntervalCCCDescriptor = Peripheral.getClientCharacteristicConfigurationDescriptor();
  mMeasurementIntervalCharacteristic.addDescriptor(mMeasurementIntervalCCCDescriptor);

  mMeasurementIntervalCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(MEASUREMENT_INTERVAL_DESCRIPTION));

  mHealthThermometerService = new BluetoothGattService(HEALTH_THERMOMETER_SERVICE_UUID,
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  mHealthThermometerService.addCharacteristic(mTemperatureMeasurementCharacteristic);
  mHealthThermometerService.addCharacteristic(mMeasurementIntervalCharacteristic);
}
 
Example 2
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyNotificationTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.setNotify(characteristic, true);
    verify(gatt).setCharacteristicNotification(characteristic, true);
    assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);
    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example 3
Source File: AbstractHOGPServer.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: BatteryServiceFragment.java    From ble-test-peripheral-android with Apache License 2.0 6 votes vote down vote up
public BatteryServiceFragment() {
  mBatteryLevelCharacteristic =
      new BluetoothGattCharacteristic(BATTERY_LEVEL_UUID,
          BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
          BluetoothGattCharacteristic.PERMISSION_READ);

  mBatteryLevelCharacteristic.addDescriptor(
      Peripheral.getClientCharacteristicConfigurationDescriptor());

  mBatteryLevelCharacteristic.addDescriptor(
      Peripheral.getCharacteristicUserDescriptionDescriptor(BATTERY_LEVEL_DESCRIPTION));

  mBatteryService = new BluetoothGattService(BATTERY_SERVICE_UUID,
      BluetoothGattService.SERVICE_TYPE_PRIMARY);
  mBatteryService.addCharacteristic(mBatteryLevelCharacteristic);
}
 
Example 5
Source File: GattServer.java    From blefun-androidthings with Apache License 2.0 6 votes vote down vote up
private BluetoothGattService createAwesomenessService() {
    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);

    // Counter characteristic (read-only, supports notifications)
    BluetoothGattCharacteristic counter = new BluetoothGattCharacteristic(CHARACTERISTIC_COUNTER_UUID,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ);
    BluetoothGattDescriptor counterConfig = new BluetoothGattDescriptor(DESCRIPTOR_CONFIG, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
    counter.addDescriptor(counterConfig);
    BluetoothGattDescriptor counterDescription = new BluetoothGattDescriptor(DESCRIPTOR_USER_DESC, BluetoothGattDescriptor.PERMISSION_READ);
    counter.addDescriptor(counterDescription);

    // Interactor characteristic
    BluetoothGattCharacteristic interactor = new BluetoothGattCharacteristic(CHARACTERISTIC_INTERACTOR_UUID,
            BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE, BluetoothGattCharacteristic.PERMISSION_WRITE);
    BluetoothGattDescriptor interactorDescription = new BluetoothGattDescriptor(DESCRIPTOR_USER_DESC, BluetoothGattDescriptor.PERMISSION_READ);
    interactor.addDescriptor(interactorDescription);

    service.addCharacteristic(counter);
    service.addCharacteristic(interactor);

    return service;
}
 
Example 6
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyCharacteristicWithDescriptorChild(){
    byte[] data = null;
    BluetoothGattCharacteristic characteristic = mock(BluetoothGattCharacteristic.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(characteristic.getValue()).thenReturn(data);
    when(characteristic.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(characteristic.getPermissions()).thenReturn(BluetoothGattCharacteristic.PERMISSION_READ);
    when(characteristic.getProperties()).thenReturn(BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    characteristic.addDescriptor(descriptor);
    BluetoothGattCharacteristicCopy result = new GattUtils().copyCharacteristic(characteristic);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for characteristic: %s, with data: %s", characteristic.getUuid(), Arrays.toString(characteristic.getValue())));
        return;
    }
    Assert.assertNotEquals(descriptor, characteristic.getDescriptor(descriptor.getUuid()));
}
 
Example 7
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyServiceInfiniteRecursion() {
    BluetoothGattService service = new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    service.addService(new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_SECONDARY));
    service.addService(new BluetoothGattService(UUID.randomUUID(), BluetoothGattService.SERVICE_TYPE_SECONDARY));
    service.addService(service);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PERMISSION_READ, BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.randomUUID(), BluetoothGattDescriptor.PERMISSION_WRITE);
    descriptor.setValue(new byte[]{0x01, 0x02, 0x04, 0x21});
    characteristic.addDescriptor(descriptor);
    service.addCharacteristic(characteristic);
    BluetoothGattServiceCopy copyOfservice = new GattUtils().copyService(service);
    Assert.assertNotNull(copyOfservice);
    Assert.assertEquals(service.getIncludedServices().size(), copyOfservice.getIncludedServices().size());
    Assert.assertEquals(service.getCharacteristics().size(), copyOfservice.getCharacteristics().size());
    Assert.assertTrue(Arrays.equals(descriptor.getValue(),
        copyOfservice.getCharacteristic(characteristic.getUuid()).getDescriptor(descriptor.getUuid()).getValue()));
}
 
Example 8
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 6 votes vote down vote up
@Test
public void setNotifyDisableTest() throws Exception {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_NOTIFY,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.setNotify(characteristic, false);
    verify(gatt).setCharacteristicNotification(characteristic, false);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[0], descriptor.getValue()[0]);
    assertEquals(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE[1], descriptor.getValue()[1]);
    verify(gatt).writeDescriptor(descriptor);

    callback.onDescriptorWrite(gatt, descriptor, 0);

    verify(peripheralCallback).onNotificationStateUpdate(peripheral, characteristic, 0);
}
 
Example 9
Source File: GattDatabase.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Build this {@link BluetoothGattCharacteristic}, and add it to it's parent {@link BluetoothGattService}.
 */
public final ServiceBuilder build()
{
    m_characteristic = new BluetoothGattCharacteristic(m_charUuid, m_properties, m_permissions);
    m_characteristic.setValue(m_value);
    for (BluetoothGattDescriptor desc : m_descriptors)
    {
        m_characteristic.addDescriptor(desc);
    }
    m_serviceBuilder.addCharacteristic(m_characteristic);
    return m_serviceBuilder;
}
 
Example 10
Source File: AddGattServerServiceCharacteristicTransaction.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * It looks like some phones do not implement these on their own while others implement one or the other
 * or both, so to be sure we want to make sure that any characteristics that we create have these critical
 * descriptors so that we have an opportunity to respond to any read or write requests coming into our
 * gatt server.
 * @param characteristic The characteristic that we are adding to a service
 */

private void addCriticalDescriptorsIfNecessary(BluetoothGattCharacteristic characteristic) {
    CharacteristicNotificationDescriptor notificationDescriptor = new CharacteristicNotificationDescriptor();
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(notificationDescriptor.getUuid());
    if(descriptor == null) {
        characteristic.addDescriptor(notificationDescriptor);
    }
    CharacteristicNamespaceDescriptor namespaceDescriptor = new CharacteristicNamespaceDescriptor();
    BluetoothGattDescriptor characteristicNamespaceDescriptor = characteristic.getDescriptor(namespaceDescriptor.getUuid());
    if(characteristicNamespaceDescriptor == null) {
        characteristic.addDescriptor(namespaceDescriptor);
    }
}
 
Example 11
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testCopyServiceWithCharacteristicWithDescriptorChild(){
    byte[] data = null;
    BluetoothGattService service = mock(BluetoothGattService.class);
    when(service.getUuid()).thenReturn(UUID.fromString("70FE657D-BBB0-4D17-B6A6-8C0545C4001F"));
    when(service.getType()).thenReturn(BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic characteristic = mock(BluetoothGattCharacteristic.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(characteristic.getValue()).thenReturn(data);
    when(characteristic.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(characteristic.getPermissions()).thenReturn(BluetoothGattCharacteristic.PERMISSION_READ);
    when(characteristic.getProperties()).thenReturn(BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    characteristic.addDescriptor(descriptor);
    service.addCharacteristic(characteristic);
    BluetoothGattServiceCopy result = new GattUtils().copyService(service);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for service: %s, with characteristic data: %s", characteristic.getUuid(), Arrays.toString(characteristic.getValue())));
        return;
    }
    Assert.assertNotEquals(descriptor, characteristic.getDescriptor(descriptor.getUuid()));
    Assert.assertNotEquals(characteristic, service.getCharacteristic(characteristic.getUuid()));
}
 
Example 12
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testCopyServiceWithCharacteristicWithDescriptorChildWithData() {
    byte[] data = new byte[]{0x01, 0x02, 0x03};
    BluetoothGattService service = mock(BluetoothGattService.class);
    when(service.getUuid()).thenReturn(UUID.fromString("70FE657D-BBB0-4D17-B6A6-8C0545C4001F"));
    when(service.getType()).thenReturn(BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic characteristic = mock(BluetoothGattCharacteristic.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(characteristic.getValue()).thenReturn(data);
    when(characteristic.getUuid()).thenReturn(UUID.fromString("E69169D1-11A7-4545-B7FC-02A3ADFC3EAC"));
    when(characteristic.getPermissions()).thenReturn(BluetoothGattCharacteristic.PERMISSION_READ);
    when(characteristic.getProperties()).thenReturn(BluetoothGattCharacteristic.PROPERTY_NOTIFY);
    BluetoothGattDescriptor descriptor = mock(BluetoothGattDescriptor.class);
    // this should be the same as the real thing, the data inside could change as it's pointing
    // to something else.
    when(descriptor.getValue()).thenReturn(data);
    when(descriptor.getUuid()).thenReturn(UUID.fromString("F752AF23-EA2C-45BA-892C-5964B0D244A2"));
    when(descriptor.getPermissions()).thenReturn(BluetoothGattDescriptor.PERMISSION_READ);
    characteristic.addDescriptor(descriptor);
    service.addCharacteristic(characteristic);
    BluetoothGattServiceCopy result = new GattUtils().copyService(service);
    if(result == null) {
        fail(String.format(Locale.ENGLISH, "The result was null for service: %s, with characteristic data: %s", characteristic.getUuid(), Arrays.toString(characteristic.getValue())));
        return;
    }
    Assert.assertNotEquals(descriptor, characteristic.getDescriptor(descriptor.getUuid()));
    Assert.assertNotEquals(characteristic, service.getCharacteristic(characteristic.getUuid()));
    Assert.assertArrayEquals(descriptor.getValue(), data);
}
 
Example 13
Source File: BleCharacteristic.java    From SweetBlue with GNU General Public License v3.0 5 votes vote down vote up
private BleCharacteristic(final UUID uuid, final int properties, final int permissions, final BleDescriptor[] descriptors)
{
	m_native = new BluetoothGattCharacteristic(uuid, properties, permissions);

	for( int i = 0; i < descriptors.length; i++ )
	{
		m_native.addDescriptor(descriptors[i].m_native);
	}
}
 
Example 14
Source File: BluetoothPeripheralTest.java    From blessed-android with MIT License 5 votes vote down vote up
@Test
public void readDescriptor() {
    BluetoothGattCallback callback = connectAndGetCallback();
    callback.onConnectionStateChange(gatt, GATT_SUCCESS, STATE_CONNECTED);

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, 0);
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.fromString("00002A1C-0000-1000-8000-00805f9b34fb"),PROPERTY_INDICATE,0);
    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(UUID.fromString("00002903-0000-1000-8000-00805f9b34fb"),0);
    service.addCharacteristic(characteristic);
    characteristic.addDescriptor(descriptor);

    when(gatt.getServices()).thenReturn(Arrays.asList(service));

    peripheral.readDescriptor(descriptor);

    verify(gatt).readDescriptor(descriptor);

    byte[] originalByteArray = new byte[]{0x01};
    descriptor.setValue(originalByteArray);
    callback.onDescriptorRead(gatt, descriptor, 0);

    ArgumentCaptor<BluetoothPeripheral> captorPeripheral = ArgumentCaptor.forClass(BluetoothPeripheral.class);
    ArgumentCaptor<byte[]> captorValue = ArgumentCaptor.forClass(byte[].class);
    ArgumentCaptor<BluetoothGattDescriptor> captorDescriptor = ArgumentCaptor.forClass(BluetoothGattDescriptor.class);
    ArgumentCaptor<Integer> captorStatus = ArgumentCaptor.forClass(Integer.class);
    verify(peripheralCallback).onDescriptorRead(captorPeripheral.capture(), captorValue.capture(), captorDescriptor.capture(), captorStatus.capture());

    byte[] value = captorValue.getValue();
    assertEquals(0x01, value[0]);
    assertNotEquals(value, originalByteArray);   // Check if the byte array has been copied
    assertEquals(peripheral, captorPeripheral.getValue());
    assertEquals(descriptor, captorDescriptor.getValue());
    assertEquals(GATT_SUCCESS, (int) captorStatus.getValue() );
}
 
Example 15
Source File: RxBleClientMock.java    From RxAndroidBle with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: GattDatabase.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Build this {@link BluetoothGattCharacteristic}, and add it to it's parent {@link BluetoothGattService}.
 */
public final ServiceBuilder build()
{
    m_characteristic = new BluetoothGattCharacteristic(m_charUuid, m_properties, m_permissions);
    m_characteristic.setValue(m_value);
    for (BluetoothGattDescriptor desc : m_descriptors)
    {
        m_characteristic.addDescriptor(desc);
    }
    m_serviceBuilder.addCharacteristic(m_characteristic);
    return m_serviceBuilder;
}
 
Example 17
Source File: BleCharacteristic.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
private BleCharacteristic(final UUID uuid, final int properties, final int permissions, final BleDescriptor[] descriptors)
{
	m_native = new BluetoothGattCharacteristic(uuid, properties, permissions);

	for( int i = 0; i < descriptors.length; i++ )
	{
		m_native.addDescriptor(descriptors[i].m_native);
	}
}
 
Example 18
Source File: BLEServicePeripheral.java    From unity-bluetooth with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BLEServicePeripheral(final Activity activity) {
    super(activity);

    mBtAdvertiser = mBtAdapter.getBluetoothLeAdvertiser();
    mBtGattCharacteristic = new BluetoothGattCharacteristic(
            UUID.fromString(CHARACTERISTIC_UUID),
            BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE
            , BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_READ);
    BluetoothGattDescriptor dataDescriptor = new BluetoothGattDescriptor(
            UUID.fromString(CHARACTERISTIC_CONFIG_UUID),
            BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
    mBtGattCharacteristic.addDescriptor(dataDescriptor);
}
 
Example 19
Source File: NodeTest.java    From BlueSTSDK_Android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example 20
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
private void addLocalGattServerCharacteristicDescriptor(DumperContext dumpContext, Iterator<String> args) throws InterruptedException {
    int index = 0;
    String localServiceUuid = null;
    String characteristicUuid = null;
    String descriptorUuid = null;
    int permissions = -1;
    while (args.hasNext()) {
        if (index == 0) {
            localServiceUuid = args.next();
        } else if (index == 1) {
            characteristicUuid = args.next();
        } else if (index == 2) {
            descriptorUuid = args.next();
        } else if (index == 3) {
            permissions = Integer.parseInt(args.next());
        }
        index++;
    }
    if (localServiceUuid == null) {
        logError(dumpContext, new IllegalArgumentException("No local server service uuid provided"));
        return;
    } else if (characteristicUuid == null) {
        logError(dumpContext, new IllegalArgumentException("No characteristic uuid provided"));
        return;
    } else if (descriptorUuid == null) {
        logError(dumpContext, new IllegalArgumentException("No characteristic descriptor uuid provided"));
        return;
    } else if (permissions == -1) {
        logError(dumpContext, new IllegalArgumentException("No characteristic permissions provided"));
        return;
    }
    GattServerConnection conn = fitbitGatt.getServer();
    if (conn == null) {
        logError(dumpContext, new IllegalArgumentException("No valid connection for provided mac"));
        return;
    }
    BluetoothGattService localService = conn.getServer().getService(UUID.fromString(localServiceUuid));
    if (localService == null) {
        logError(dumpContext, new IllegalStateException("No local service for the uuid" + localServiceUuid + "found"));
        return;
    }
    BluetoothGattCharacteristic localCharacteristic =
        localService.getCharacteristic(UUID.fromString(characteristicUuid));
    if (!localService.addCharacteristic(localCharacteristic)) {
        logError(dumpContext, new IllegalStateException("Couldn't get characteristic from service"));
        return;
    }
    BluetoothGattDescriptor localDescriptor = new BluetoothGattDescriptor(UUID.fromString(descriptorUuid), permissions);
    if (!localCharacteristic.addDescriptor(localDescriptor)) {
        logError(dumpContext, new IllegalStateException("Couldn't add descriptor " + descriptorUuid + " to the local characteristic " + characteristicUuid + " on the service " + localServiceUuid));
        return;
    }
    // don't worry, this instance can only be registered once, but we do want for it to have
    // a fresh dumper context
    serverConnectionListener.setContext(dumpContext);
    conn.registerConnectionEventListener(serverConnectionListener);
    CountDownLatch cdl = new CountDownLatch(1);
    AddGattServerServiceCharacteristicDescriptorTransaction tx = new AddGattServerServiceCharacteristicDescriptorTransaction(conn, GattState.ADD_SERVICE_CHARACTERISTIC_DESCRIPTOR_SUCCESS, localService, localCharacteristic, localDescriptor);
    conn.runTx(tx, result -> {
        logSuccessOrFailure(result, dumpContext,
            "Successfully added " + localDescriptor.getUuid().toString() + " to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString(),
            "Failed to add " + localDescriptor.getUuid().toString() + " to " + localCharacteristic.getUuid().toString() + " on " + localService.getUuid().toString());
        cdl.countDown();
    });
    cdl.await();
}