Java Code Examples for android.bluetooth.BluetoothGattDescriptor#PERMISSION_WRITE

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#PERMISSION_WRITE . 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: TxConcurrencyTests.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Before
public void before() {
    Context appContext = InstrumentationRegistry.getInstrumentation().getContext();
    FitbitGatt.getInstance().startGattClient(appContext);
    // started
    FitbitBluetoothDevice device = new FitbitBluetoothDevice(MOCK_ADDRESS, "fooDevice");
    fakeData = new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    characteristic = new BluetoothGattCharacteristic(CHAR_NAME, PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_WRITE);
    descriptor = new BluetoothGattDescriptor(DESCRIPTOR_NAME, BluetoothGattDescriptor.PERMISSION_WRITE);
    connection = FitbitGatt.getInstance().getConnection(device);
    if (connection == null) {
        connection = new GattConnection(device, appContext.getMainLooper());
        FitbitGatt.getInstance().getConnectionMap().put(device, connection);
    }
    connection.setMockMode(true);
    connection.setState(GattState.CONNECTED);
    handler = new Handler(handlerThread.getLooper());
}
 
Example 2
Source File: GattUtilsTest.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCopyServiceFull() {
    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 3
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 4
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 5
Source File: TimeProfile.java    From sample-bluetooth-le-gattserver with Apache License 2.0 6 votes vote down vote up
/**
 * Return a configured {@link BluetoothGattService} instance for the
 * Current Time Service.
 */
public static BluetoothGattService createTimeService() {
    BluetoothGattService service = new BluetoothGattService(TIME_SERVICE,
            BluetoothGattService.SERVICE_TYPE_PRIMARY);

    // Current Time characteristic
    BluetoothGattCharacteristic currentTime = new BluetoothGattCharacteristic(CURRENT_TIME,
            //Read-only characteristic, supports notifications
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ);
    BluetoothGattDescriptor configDescriptor = new BluetoothGattDescriptor(CLIENT_CONFIG,
            //Read/write descriptor
            BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
    currentTime.addDescriptor(configDescriptor);

    // Local Time Information characteristic
    BluetoothGattCharacteristic localTime = new BluetoothGattCharacteristic(LOCAL_TIME_INFO,
            //Read-only characteristic
            BluetoothGattCharacteristic.PROPERTY_READ,
            BluetoothGattCharacteristic.PERMISSION_READ);

    service.addCharacteristic(currentTime);
    service.addCharacteristic(localTime);

    return service;
}
 
Example 6
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 7
Source File: CharacteristicNotificationDescriptor.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will create a 0x2902 characteristic descriptor
 */
public CharacteristicNotificationDescriptor() {
    super(notificationDescriptorTwentyNineOhTwo, BluetoothGattDescriptor.PERMISSION_READ |
            BluetoothGattDescriptor.PERMISSION_WRITE |
            BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED |
            BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
}
 
Example 8
Source File: CharacteristicNamespaceDescriptor.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will create a 0x2904 characteristic descriptor
 */
public CharacteristicNamespaceDescriptor() {
    super(notificationDescriptorTwentyNineOhFour, BluetoothGattDescriptor.PERMISSION_READ |
            BluetoothGattDescriptor.PERMISSION_WRITE |
            BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED |
            BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
}
 
Example 9
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 10
Source File: Helper.java    From react-native-ble-manager with Apache License 2.0 5 votes vote down vote up
public static WritableMap decodePermissions(BluetoothGattDescriptor descriptor) {

		// NOTE: props strings need to be consistent across iOS and Android
		WritableMap props = Arguments.createMap();
		int permissions = descriptor.getPermissions();

		if ((permissions & BluetoothGattDescriptor.PERMISSION_READ) != 0x0 ) {
			props.putString("Read", "Read");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_WRITE) != 0x0 ) {
			props.putString("Write", "Write");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED) != 0x0 ) {
			props.putString("ReadEncrypted", "ReadEncrypted");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED) != 0x0 ) {
			props.putString("WriteEncrypted", "WriteEncrypted");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM) != 0x0 ) {
			props.putString("ReadEncryptedMITM", "ReadEncryptedMITM");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM) != 0x0 ) {
			props.putString("WriteEncryptedMITM", "WriteEncryptedMITM");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED) != 0x0 ) {
			props.putString("WriteSigned", "WriteSigned");
		}

		if ((permissions & BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED_MITM) != 0x0 ) {
			props.putString("WriteSignedMITM", "WriteSignedMITM");
		}

		return props;
	}
 
Example 11
Source File: Peripheral.java    From ble-test-peripheral-android with Apache License 2.0 5 votes vote down vote up
public static BluetoothGattDescriptor getClientCharacteristicConfigurationDescriptor() {
  BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
      CLIENT_CHARACTERISTIC_CONFIGURATION_UUID,
      (BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
  descriptor.setValue(new byte[]{0, 0});
  return descriptor;
}
 
Example 12
Source File: Peripheral.java    From ble-test-peripheral-android with Apache License 2.0 5 votes vote down vote up
public static BluetoothGattDescriptor getCharacteristicUserDescriptionDescriptor(String defaultValue) {
  BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
      CHARACTERISTIC_USER_DESCRIPTION_UUID,
      (BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE));
  try {
    descriptor.setValue(defaultValue.getBytes("UTF-8"));
  } finally {
    return descriptor;
  }
}
 
Example 13
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
String getStringRepresentationOfPermissionsForDescriptor(BluetoothGattDescriptor descriptor) {
    StringBuilder permissionBuilder = new StringBuilder();
    ArrayList<Integer> permissions = new ArrayList<>(8);
    int descriptorPermissions = descriptor.getPermissions();
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_READ) == BluetoothGattDescriptor.PERMISSION_READ) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_READ);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED) == BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM) == BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_WRITE) == BluetoothGattDescriptor.PERMISSION_WRITE) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_WRITE);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED) == BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM) == BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED) == BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);
    }
    if ((descriptorPermissions & BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED_MITM) == BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED_MITM) {
        permissions.add(BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED_MITM);
    }
    for (int i = 0; i < permissions.size(); i++) {
        int permission = permissions.get(i);
        switch (permission) {
            case BluetoothGattDescriptor.PERMISSION_READ:
                permissionBuilder.append("read");
                break;
            case BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED:
                permissionBuilder.append("read-encrypted");
                break;
            case BluetoothGattDescriptor.PERMISSION_WRITE:
                permissionBuilder.append("write");
                break;
            case BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM:
                permissionBuilder.append("read-encrypted-mitm");
                break;
            case BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED:
                permissionBuilder.append("write-encrypted");
                break;
            case BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM:
                permissionBuilder.append("write-encrypted-mitm");
                break;
            case BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED:
                permissionBuilder.append("write-signed");
                break;
            case BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED_MITM:
                permissionBuilder.append("write-signed-mitm");
                break;
            default:
                permissionBuilder.append("unknown");
        }
        if (i < permissions.size() - 1) {
            permissionBuilder.append(", ");
        }
    }
    return permissionBuilder.toString();
}