Java Code Examples for android.bluetooth.BluetoothGattDescriptor#getPermissions()

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#getPermissions() . 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: GattUtils.java    From bitgatt with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * To prevent the descriptor from changing out from under us we need to copy it
 * <p>
 * This may happen under high throughput/concurrency
 *
 * @param descriptor The descriptor to be copied
 * @return The shallow-ish copy of the descriptor
 */
public @Nullable
BluetoothGattDescriptorCopy copyDescriptor(@Nullable BluetoothGattDescriptor descriptor) {
    if (null == descriptor || null == descriptor.getUuid()) {
        return null;
    }
    BluetoothGattDescriptorCopy newDescriptor =
            new BluetoothGattDescriptorCopy(descriptor.getUuid(),
                    descriptor.getPermissions());
    if (newDescriptor.getValue() != null) {
        newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
    }
    if (newDescriptor.getCharacteristic() != null) {
        BluetoothGattCharacteristicCopy oldCharacteristic = newDescriptor.getCharacteristic();
        BluetoothGattCharacteristicCopy copyOfCharacteristic = new BluetoothGattCharacteristicCopy(oldCharacteristic.getUuid(), oldCharacteristic.getProperties(), oldCharacteristic.getPermissions());
        if (oldCharacteristic.getValue() != null) {
            copyOfCharacteristic.setValue(Arrays.copyOf(oldCharacteristic.getValue(), oldCharacteristic.getValue().length));
        }

        copyOfCharacteristic.addDescriptor(newDescriptor);
    }
    return newDescriptor;
}
 
Example 2
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * To prevent the characteristic from changing out from under us we need to copy it
 * <p>
 * This may happen under high throughput/concurrency
 *
 * @param characteristic The characteristic to be copied
 * @return The shallow-ish copy of the characteristic
 */
public @Nullable
BluetoothGattCharacteristicCopy copyCharacteristic(@Nullable BluetoothGattCharacteristic characteristic) {
    if (null == characteristic || null == characteristic.getUuid()) {
        return null;
    }
    BluetoothGattCharacteristicCopy newCharacteristic =
            new BluetoothGattCharacteristicCopy(characteristic.getUuid(),
                    characteristic.getProperties(), characteristic.getPermissions());
    if (characteristic.getValue() != null) {
        newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
    }
    if (!characteristic.getDescriptors().isEmpty()) {
        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
            BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
            if (descriptor.getValue() != null) {
                newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
            }
            newCharacteristic.addDescriptor(newDescriptor);
        }
    }
    if (characteristic.getService() != null) {
        BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(characteristic.getService().getUuid(), characteristic.getService().getType());
        newService.addCharacteristic(newCharacteristic);
    }
    return newCharacteristic;
}
 
Example 3
Source File: GattUtils.java    From bitgatt with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will return the copy of the service
 *
 * @param service The gatt service
 * @return a shallow-ish copy of the service
 */

public @Nullable
BluetoothGattServiceCopy copyService(@Nullable BluetoothGattService service) {
    if (null == service || null == service.getUuid()) {
        return null;
    }
    BluetoothGattServiceCopy newService = new BluetoothGattServiceCopy(service.getUuid(), service.getType());
    if (!service.getIncludedServices().isEmpty()) {
        for (BluetoothGattService includedService : service.getIncludedServices()) {
            BluetoothGattServiceCopy newGattService = new BluetoothGattServiceCopy(includedService.getUuid(), includedService.getType());
            newService.addService(newGattService);
        }
    }
    if (!service.getCharacteristics().isEmpty()) {
        // why not use the copy characteristic method, it will implicitly link itself to the null service
        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            BluetoothGattCharacteristicCopy newCharacteristic = new BluetoothGattCharacteristicCopy(characteristic.getUuid(), characteristic.getProperties(), characteristic.getPermissions());
            if (characteristic.getValue() != null) {
                newCharacteristic.setValue(Arrays.copyOf(characteristic.getValue(), characteristic.getValue().length));
            }
            // why not use the copy descriptor method?  It will implicitly link itself to the null characteristic
            for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                BluetoothGattDescriptorCopy newDescriptor = new BluetoothGattDescriptorCopy(descriptor.getUuid(), descriptor.getPermissions());
                if (descriptor.getValue() != null) {
                    newDescriptor.setValue(Arrays.copyOf(descriptor.getValue(), descriptor.getValue().length));
                }
                newCharacteristic.addDescriptor(newDescriptor);
            }
            newService.addCharacteristic(newCharacteristic);
        }
    }
    return newService;
}
 
Example 4
Source File: BlePeripheral.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private static int getDescriptorPermissions(BluetoothGattService service, String characteristicUUIDString, String descriptorUUIDString) {
    final UUID characteristicUuid = UUID.fromString(characteristicUUIDString);
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);

    int permissions = 0;
    if (characteristic != null) {
        final UUID descriptorUuid = UUID.fromString(descriptorUUIDString);
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
        if (descriptor != null) {
            permissions = descriptor.getPermissions();
        }
    }

    return permissions;
}
 
Example 5
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 6
Source File: BleManager.java    From Bluefruit_LE_Connect_Android with MIT License 5 votes vote down vote up
private int getDescriptorPermissions(BluetoothGattService service, String characteristicUUIDString, String descriptorUUIDString) {
    final UUID characteristicUuid = UUID.fromString(characteristicUUIDString);
    BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUuid);

    int permissions = 0;
    if (characteristic != null) {
        final UUID descriptorUuid = UUID.fromString(descriptorUUIDString);
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
        if (descriptor != null) {
            permissions = descriptor.getPermissions();
        }
    }

    return permissions;
}
 
Example 7
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();
}
 
Example 8
Source File: Peripheral.java    From react-native-ble-manager with Apache License 2.0 4 votes vote down vote up
public WritableMap asWritableMap(BluetoothGatt gatt) {

		WritableMap map = asWritableMap();

		WritableArray servicesArray = Arguments.createArray();
		WritableArray characteristicsArray = Arguments.createArray();

		if (connected && gatt != null) {
			for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext();) {
				BluetoothGattService service = it.next();
				WritableMap serviceMap = Arguments.createMap();
				serviceMap.putString("uuid", UUIDHelper.uuidToString(service.getUuid()));

				for (Iterator<BluetoothGattCharacteristic> itCharacteristic = service.getCharacteristics()
						.iterator(); itCharacteristic.hasNext();) {
					BluetoothGattCharacteristic characteristic = itCharacteristic.next();
					WritableMap characteristicsMap = Arguments.createMap();

					characteristicsMap.putString("service", UUIDHelper.uuidToString(service.getUuid()));
					characteristicsMap.putString("characteristic", UUIDHelper.uuidToString(characteristic.getUuid()));

					characteristicsMap.putMap("properties", Helper.decodeProperties(characteristic));

					if (characteristic.getPermissions() > 0) {
						characteristicsMap.putMap("permissions", Helper.decodePermissions(characteristic));
					}

					WritableArray descriptorsArray = Arguments.createArray();

					for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
						WritableMap descriptorMap = Arguments.createMap();
						descriptorMap.putString("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
						if (descriptor.getValue() != null) {
							descriptorMap.putString("value",
									Base64.encodeToString(descriptor.getValue(), Base64.NO_WRAP));
						} else {
							descriptorMap.putString("value", null);
						}

						if (descriptor.getPermissions() > 0) {
							descriptorMap.putMap("permissions", Helper.decodePermissions(descriptor));
						}
						descriptorsArray.pushMap(descriptorMap);
					}
					if (descriptorsArray.size() > 0) {
						characteristicsMap.putArray("descriptors", descriptorsArray);
					}
					characteristicsArray.pushMap(characteristicsMap);
				}
				servicesArray.pushMap(serviceMap);
			}
			map.putArray("services", servicesArray);
			map.putArray("characteristics", characteristicsArray);
		}

		return map;
	}
 
Example 9
Source File: BleGattDescriptor.java    From Android-BluetoothKit with Apache License 2.0 4 votes vote down vote up
public BleGattDescriptor(BluetoothGattDescriptor descriptor) {
    this.mUuid = new ParcelUuid(descriptor.getUuid());
    this.mPermissions = descriptor.getPermissions();
    this.mValue = descriptor.getValue();
}