Java Code Examples for android.bluetooth.BluetoothGattCharacteristic#PERMISSION_READ_ENCRYPTED

The following examples show how to use android.bluetooth.BluetoothGattCharacteristic#PERMISSION_READ_ENCRYPTED . 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: 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 2
Source File: GattServerTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@BeforeClass
public static void before() {
    Timber.plant(new Timber.DebugTree());
    mockContext = InstrumentationRegistry.getInstrumentation().getContext();
    main = new BluetoothGattService(UUID.fromString("adabfb00-6e7d-4601-bda2-bffaa68956ba"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic txChar = new BluetoothGattCharacteristic(UUID.fromString("ADABFB01-6E7D-4601-BDA2-BFFAA68956BA"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
        BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED);
    main.addCharacteristic(txChar);
    BluetoothGattCharacteristic rxChar = new BluetoothGattCharacteristic(UUID.fromString("ADABFB02-6E7D-4601-BDA2-BFFAA68956BA"),
        BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    main.addCharacteristic(rxChar);
    liveData = new BluetoothGattService(UUID.fromString("558dfa00-4fa8-4105-9f02-4eaa93e62980"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic ldChar = new BluetoothGattCharacteristic(UUID.fromString("558DFA01-4FA8-4105-9F02-4EAA93E62980"),
        BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    liveData.addCharacteristic(ldChar);
    dncs = new BluetoothGattService(UUID.fromString("16bcfd00-253f-c348-e831-0db3e334d580"), BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic notifChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD02-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(notifChar);
    cpChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD01-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(cpChar);
    BluetoothGattCharacteristic flsChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD04-253F-C348-E831-0DB3E334D580"),
        BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ,
        BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
    dncs.addCharacteristic(flsChar);
}
 
Example 3
Source File: GattServerTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void notifyGattServerWithNullPointerException() throws InterruptedException {
    CountDownLatch cdl = new CountDownLatch(1);
    TransactionResult.TransactionResultStatus[] results = new TransactionResult.TransactionResultStatus[1];
    FitbitGatt.FitbitGattCallback callback = new NoOpGattCallback() {
        @Override
        public void onGattServerStarted(GattServerConnection serverConnection) {
            super.onGattServerStarted(serverConnection);
            FitbitBluetoothDevice fbtDevice = new FitbitBluetoothDevice(MOCK_ADDRESS, "foobar");
            BluetoothGattCharacteristic notifChar = new BluetoothGattCharacteristic(UUID.fromString("16BCFD02-253F-C348-E831-0DB3E334D580"),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_READ,
                BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED | BluetoothGattCharacteristic.PERMISSION_WRITE);
            NotifyGattServerCharacteristicMockTransaction notifyTx = new NotifyGattServerCharacteristicMockTransaction(serverConnection, fbtDevice, GattState.NOTIFY_CHARACTERISTIC_SUCCESS, notifChar, true, true);
            notifyTx.setShouldThrow(true);
            serverConnection.runTx(notifyTx, result -> {
                results[0] = result.resultStatus;
                cdl.countDown();
            });
        }
    };
    FitbitGatt.getInstance().registerGattEventListener(callback);
    FitbitGatt.getInstance().startGattServer(mockContext);

    cdl.await(1, TimeUnit.SECONDS);
    assertEquals(TransactionResult.TransactionResultStatus.FAILURE, results[0]);
    assertEquals(GattState.IDLE, FitbitGatt.getInstance().getServer().getGattState());
}
 
Example 4
Source File: GattReadWriteTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void readCharacteristicTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    ReadGattCharacteristicMockTransaction readChar = new ReadGattCharacteristicMockTransaction(conn, GattState.READ_CHARACTERISTIC_SUCCESS, characteristic, fakeData, false);
    conn.runTx(readChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.SUCCESS) && result.resultState.equals(readChar.getSuccessState()));
    });
}
 
Example 5
Source File: GattReadWriteTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void writeCharacteristicTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    WriteGattCharacteristicMockTransaction writeChar = new WriteGattCharacteristicMockTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, characteristic, fakeData, false);
    conn.runTx(writeChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.SUCCESS) && result.resultState.equals(writeChar.getSuccessState()));
    });
}
 
Example 6
Source File: GattReadWriteTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void writeCharacteristicWithBtOffTest() {
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    WriteGattCharacteristicMockTransaction writeChar = new WriteGattCharacteristicMockTransaction(conn, GattState.WRITE_CHARACTERISTIC_SUCCESS, characteristic, fakeData, false);
    conn.setState(GattState.BT_OFF);
    conn.runTx(writeChar, result -> {
        assert(result.resultStatus.equals(TransactionResult.TransactionResultStatus.FAILURE) && result.resultState.equals(writeChar.getSuccessState()));
    });
}
 
Example 7
Source File: ScannedDevicesInvalidationTests.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testConnectionTxResetTtl(){
    conn.setState(GattState.CONNECTED);
    conn.setDisconnectedTTL(300);
    final byte[] fakeData = new byte[]{'a','b','c','d','e','f','g','h','i', 'j'};
    BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.randomUUID(), BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    ReadGattCharacteristicMockTransaction readChar = new ReadGattCharacteristicMockTransaction(conn, GattState.READ_CHARACTERISTIC_SUCCESS, characteristic, fakeData, false);
    conn.runTx(readChar, result -> assertEquals("Tx result was successful", result.resultState, GattState.READ_CHARACTERISTIC_SUCCESS));
    gatt.doDecrementAndInvalidateClosedConnections();
    assertEquals(FitbitGatt.MAX_TTL, conn.getDisconnectedTTL());
}
 
Example 8
Source File: Helper.java    From react-native-ble-manager with Apache License 2.0 5 votes vote down vote up
public static WritableMap decodePermissions(BluetoothGattCharacteristic characteristic) {

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

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

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

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

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

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

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

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

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

		return props;
	}
 
Example 9
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
String getStringRepresentationOfPermissionsForCharacteristic(BluetoothGattCharacteristic characteristic) {
    StringBuilder permissionBuilder = new StringBuilder();
    ArrayList<Integer> permissions = new ArrayList<>(8);
    int characteristicPermissions = characteristic.getPermissions();
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ) == BluetoothGattCharacteristic.PERMISSION_READ) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED) == BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM) == BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE) == BluetoothGattCharacteristic.PERMISSION_WRITE) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED) == BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM) == BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED) == BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED);
    }
    if ((characteristicPermissions & BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM) == BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM) {
        permissions.add(BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM);
    }
    for (int i = 0; i < permissions.size(); i++) {
        int permission = permissions.get(i);
        switch (permission) {
            case BluetoothGattCharacteristic.PERMISSION_READ:
                permissionBuilder.append("read");
                break;
            case BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED:
                permissionBuilder.append("read-encrypted");
                break;
            case BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM:
                permissionBuilder.append("read-encrypted-mitm");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE:
                permissionBuilder.append("write");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED:
                permissionBuilder.append("write-encrypted");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM:
                permissionBuilder.append("write-encrypted-mitm");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED:
                permissionBuilder.append("write-signed");
                break;
            case BluetoothGattCharacteristic.PERMISSION_WRITE_SIGNED_MITM:
                permissionBuilder.append("write-signed-mitm");
                break;
            default:
                permissionBuilder.append("unknown");
        }
        if (i < permissions.size() - 1) {
            permissionBuilder.append(", ");
        }
    }
    return permissionBuilder.toString();
}
 
Example 10
Source File: GattDatabase.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
public final T read_encrypted()
{
    m_permissions |= BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED;
    return (T) this;
}
 
Example 11
Source File: GattDatabase.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public final T read_encrypted()
{
    m_permissions |= BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED;
    return (T) this;
}
 
Example 12
Source File: GattDatabase.java    From SweetBlue with GNU General Public License v3.0 4 votes vote down vote up
public final T read_encrypted()
{
    m_permissions |= BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED;
    return (T) this;
}