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

The following examples show how to use android.bluetooth.BluetoothGattDescriptor#getUuid() . These examples are extracted from open source projects. 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
/**
 * If the Gatt server is not null, this will return an error to the caller and return true if the
 * remote descriptor's characteristic is null, or if the remote descriptor's characteristic's service is null,
 * or if the remote descriptor's characteristic's UUID is null, or if the remote descriptor's UUID is null,
 * or if the remote descriptor's characteristic's UUID is null, or the remote descriptor's characteristic's service's UUID is null,
 * or if the hosted descriptor is not found, or if the hosted descriptor's hosted characteristic is not found,
 * or if the hosted descriptor's hosted characteristic's hosted service is not found.
 * <p>
 * If the gatt server is null, this will return true even though we did not actually send the response to allow the null
 * gatt server mocking code to take over if we are in mock mode
 *
 * @param conn       The gatt server connection
 * @param descriptor The remote descriptor provided in the descriptor write or read request
 * @param device     The bluetooth device
 * @param requestId  The request id
 * @param offset     The offset
 * @return true if the gatt server is null, or if one of the chained remote descriptor,
 * characteristic, service, or hosted chained descriptor, characteristic, services is null
 */

private boolean ifNotHostingDescriptorRespondError(GattServerConnection conn, BluetoothGattDescriptor descriptor, BluetoothDevice device, int requestId, int offset) {
    UUID descUuid = descriptor.getUuid();
    if (conn.getServer() == null) {
        Timber.w("[%s] The server instance was null!", getDeviceMacFromDevice(device));
        return false;
    }

    if (checkCharacteristicAndCharacteristicService(conn, device, descriptor.getCharacteristic(), requestId, offset)) {
        return true;
    }
    // if we can get here, we know that the characteristic and service are both non-null
    BluetoothGattService hostedService = descriptor.getCharacteristic().getService();
    BluetoothGattCharacteristic hostedCharacteristic = hostedService.getCharacteristic(descriptor.getCharacteristic().getUuid());
    if (hostedCharacteristic.getDescriptor(descUuid) == null) {
        Timber.w("[%s] The expected descriptor wasn't hosted! Returning error", device);
        returnErrorToRemoteClient(conn, device, requestId, offset);
        return true;
    }
    return false;
}
 
Example 2
Source Project: bitgatt   File: GattUtils.java    License: 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 3
private void onDescriptorWrite_updateThread(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final byte[] data, final int gattStatus)
{
    final UUID uuid = descriptor.getUuid();
    logger().i(logger().descriptorName(uuid));
    logger().log_status(gattStatus);

    final P_Task_WriteDescriptor task_write = m_queue.getCurrent(P_Task_WriteDescriptor.class, m_device);

    if (task_write != null && task_write.isFor(descriptor))
    {
        task_write.onDescriptorWrite(gatt, descriptor.getUuid(), gattStatus);
    }
    else
    {
        final P_Task_ToggleNotify task_toggleNotify = m_queue.getCurrent(P_Task_ToggleNotify.class, m_device);

        if (task_toggleNotify != null && task_toggleNotify.isFor(descriptor))
        {
            task_toggleNotify.onDescriptorWrite(gatt, uuid, gattStatus);
        }
        else
        {
            fireUnsolicitedEvent(new BleCharacteristicWrapper(descriptor.getCharacteristic()), new BleDescriptorWrapper(descriptor), BleDevice.ReadWriteListener.Type.WRITE, BleDevice.ReadWriteListener.Target.DESCRIPTOR, data, gattStatus);
        }
    }
}
 
Example 4
private void onDescriptorWrite_updateThread(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final byte[] data, final int gattStatus)
{
    final UUID uuid = descriptor.getUuid();
    logger().i(logger().descriptorName(uuid));
    logger().log_status(gattStatus);

    final P_Task_WriteDescriptor task_write = m_queue.getCurrent(P_Task_WriteDescriptor.class, m_device);

    if (task_write != null && task_write.isFor(descriptor))
    {
        task_write.onDescriptorWrite(gatt, descriptor.getUuid(), gattStatus);
    }
    else
    {
        final P_Task_ToggleNotify task_toggleNotify = m_queue.getCurrent(P_Task_ToggleNotify.class, m_device);

        if (task_toggleNotify != null && task_toggleNotify.isFor(descriptor))
        {
            task_toggleNotify.onDescriptorWrite(gatt, uuid, gattStatus);
        }
        else
        {
            fireUnsolicitedEvent(new BleCharacteristicWrapper(descriptor.getCharacteristic()), new BleDescriptorWrapper(descriptor), BleDevice.ReadWriteListener.Type.WRITE, BleDevice.ReadWriteListener.Target.DESCRIPTOR, data, gattStatus);
        }
    }
}
 
Example 5
Source Project: bitgatt   File: GattUtils.java    License: 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 6
Source Project: bitgatt   File: GattUtils.java    License: 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 7
Source Project: RxAndroidBle   File: LoggerUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void logCallback(String callbackName, BluetoothGatt gatt, int status, BluetoothGattDescriptor descriptor,
                               boolean valueMatters) {
    if (!RxBleLog.isAtLeast(LogConstants.INFO)) {
        return;
    }
    AttributeLogWrapper value = new AttributeLogWrapper(descriptor.getUuid(), descriptor.getValue(), valueMatters);
    RxBleLog.i(commonMacMessage(gatt) + commonCallbackMessage() + commonStatusMessage() + commonValueMessage(),
            callbackName, status, value);
}
 
Example 8
public P_Task_ReadDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, Type type, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);
	
	m_type = type;
	m_descriptorUuid = descriptor.getUuid();
}
 
Example 9
public P_Task_WriteDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, FutureData futureData, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);

	m_descriptorUuid = descriptor.getUuid();
	m_futureData = futureData;
}
 
Example 10
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
		BluetoothGattDescriptor descriptor, int status) {
	BleLog.i(TAG, "onDescriptorWrite: " + BleUtils.getGattStatus(status));
	UUID uuid = descriptor.getUuid();

	sendBleMessage(BleConstants.MSG_BLE_ID_DESCRIPTOR_WRITE, status, uuid);
	onNextWrite();
}
 
Example 11
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
	final byte[] data = descriptor.getValue();
	BleLog.i(TAG, "onCharacteristicRead: " + HexUtil.encodeHexStr(data));
	UUID uuid = descriptor.getUuid();

	sendBleMessage(BleConstants.MSG_BLE_ID_DESCRIPTOR_READ, status, data, uuid);
	onNextWrite();
}
 
Example 12
public P_Task_ReadDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, Type type, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);
	
	m_type = type;
	m_descriptorUuid = descriptor.getUuid();
}
 
Example 13
public P_Task_WriteDescriptor(BleDevice device, BluetoothGattDescriptor descriptor, FutureData futureData, boolean requiresBonding, ReadWriteListener readListener, BleTransaction txn, PE_TaskPriority priority)
{
	super(device, descriptor.getCharacteristic(), readListener, requiresBonding, txn, priority);

	m_descriptorUuid = descriptor.getUuid();
	m_futureData = futureData;
}
 
Example 14
private void refreshData() {
        // Remove old data
        mInfoData.clear();

        // Services
        List<BluetoothGattService> services = mBlePeripheral == null ? null : mBlePeripheral.getServices();     // Check if mBlePeripheral is null (crash detected on Google logs)

        if (services != null) {
            // Order services so "DIS" is at the top (if present)
            Collections.sort(services, (serviceA, serviceB) -> {
                final boolean isServiceADis = serviceA.getUuid().equals(kDisServiceUUID);
                final boolean isServiceBDis = serviceB.getUuid().equals(kDisServiceUUID);
                return isServiceADis ? -1 : (isServiceBDis ? 1 : serviceA.getUuid().compareTo(serviceB.getUuid()));
            });

            final Handler mainHandler = new Handler(Looper.getMainLooper());
            // Discover characteristics and descriptors
            for (BluetoothGattService service : services) {
                // Service
                final UUID serviceUuid = service.getUuid();
                final int instanceId = service.getInstanceId();
                String serviceUUIDString = BleUtils.uuidToString(serviceUuid);
                String serviceName = BleUUIDNames.getNameForUUID(getContext(), serviceUUIDString);
                String finalServiceName = serviceName != null ? serviceName : LocalizationManager.getInstance().getString(getContext(), "info_type_service");
                ElementPath serviceElementPath = new ElementPath(serviceUuid, instanceId, null, null, finalServiceName, serviceUUIDString);
                mInfoData.mServices.add(serviceElementPath);

                // Characteristics
                List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
                List<ElementPath> characteristicNamesList = new ArrayList<>(characteristics.size());
                for (final BluetoothGattCharacteristic characteristic : characteristics) {
                    final UUID characteristicUuid = characteristic.getUuid();
                    final String characteristicUuidString = BleUtils.uuidToString(characteristicUuid);
                    String characteristicName = BleUUIDNames.getNameForUUID(getContext(), characteristicUuidString);
                    String finalCharacteristicName = characteristicName != null ? characteristicName : characteristicUuidString;
                    final ElementPath characteristicElementPath = new ElementPath(serviceUuid, instanceId, characteristicUuid, null, finalCharacteristicName, characteristicUuidString);
                    characteristicNamesList.add(characteristicElementPath);

                    // Read characteristic
                    if (BlePeripheral.isCharacteristicReadable(service, characteristicUuid)) {
                        final String characteristicKey = characteristicElementPath.getKey();
                        mBlePeripheral.readCharacteristic(service, characteristicUuid, (status, data) -> {
                            if (status == BluetoothGatt.GATT_SUCCESS) {
                                mInfoData.mValuesMap.put(characteristicKey, data);

                                // Update UI
                                mainHandler.post(this::updateUI);
                            }
                        });

                        /*
                        mBlePeripheral.readCharacteristic(service, characteristicUuid, status -> {
                            if (status == BluetoothGatt.GATT_SUCCESS) {
                                mInfoData.mValuesMap.put(characteristicKey, characteristic.getValue());

                                // Update UI
                                mainHandler.post(this::updateUI);
                            }
                        });*/
                    }

                    // Descriptors
                    List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
                    List<ElementPath> descriptorNamesList = new ArrayList<>(descriptors.size());
                    for (final BluetoothGattDescriptor descriptor : descriptors) {
                        final UUID descriptorUuid = descriptor.getUuid();
                        final String descriptorUuidString = BleUtils.uuidToString(descriptorUuid);
                        String descriptorName = BleUUIDNames.getNameForUUID(getContext(), descriptorUuidString);
                        String finalDescriptorName = descriptorName != null ? descriptorName : descriptorUuidString;
                        final ElementPath descriptorElementPath = new ElementPath(serviceUuid, instanceId, characteristicUuid, descriptorUuid, finalDescriptorName, descriptorUuidString);
                        descriptorNamesList.add(descriptorElementPath);

                        // Read descriptor
//                    if (BlePeripheral.isDescriptorReadable(service, characteristicUuid, descriptorUuid)) {
                        final String descriptorKey = descriptorElementPath.getKey();
                        mBlePeripheral.readDescriptor(service, characteristicUuid, descriptorUuid, status -> {
                            if (status == BluetoothGatt.GATT_SUCCESS) {
                                mInfoData.mValuesMap.put(descriptorKey, descriptor.getValue());

                                // Update UI
                                mainHandler.post(this::updateUI);
                            }
                        });
//                    }
                    }

                    mInfoData.mDescriptors.put(characteristicElementPath.getKey(), descriptorNamesList);
                }
                mInfoData.mCharacteristics.put(serviceElementPath.getKey(), characteristicNamesList);
            }
        }

        updateUI();
    }
 
Example 15
Source Project: RxAndroidBle   File: LoggerUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static AttributeLogWrapper wrap(BluetoothGattDescriptor descriptor, boolean valueMatters) {
    return new AttributeLogWrapper(descriptor.getUuid(), descriptor.getValue(), valueMatters);
}
 
Example 16
public BleGattDescriptor(BluetoothGattDescriptor descriptor) {
    this.mUuid = new ParcelUuid(descriptor.getUuid());
    this.mPermissions = descriptor.getPermissions();
    this.mValue = descriptor.getValue();
}
 
Example 17
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
    BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
    int offset,
    byte[] value) {
  super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded,
      offset, value);
  Log.v(TAG, "Descriptor Write Request " + descriptor.getUuid() + " " + Arrays.toString(value));
  int status = BluetoothGatt.GATT_SUCCESS;
  if (descriptor.getUuid() == CLIENT_CHARACTERISTIC_CONFIGURATION_UUID) {
    BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
    boolean supportsNotifications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
    boolean supportsIndications = (characteristic.getProperties() &
        BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0;

    if (!(supportsNotifications || supportsIndications)) {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    } else if (value.length != 2) {
      status = BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
    } else if (Arrays.equals(value, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsDisabled(characteristic);
      descriptor.setValue(value);
    } else if (supportsNotifications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, false /* indicate */);
      descriptor.setValue(value);
    } else if (supportsIndications &&
        Arrays.equals(value, BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)) {
      status = BluetoothGatt.GATT_SUCCESS;
      mCurrentServiceFragment.notificationsEnabled(characteristic, true /* indicate */);
      descriptor.setValue(value);
    } else {
      status = BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED;
    }
  } else {
    status = BluetoothGatt.GATT_SUCCESS;
    descriptor.setValue(value);
  }
  if (responseNeeded) {
    mGattServer.sendResponse(device, requestId, status,
        /* No need to respond with offset */ 0,
        /* No need to respond with a value */ null);
  }
}