Java Code Examples for android.bluetooth.BluetoothGatt#readDescriptor()

The following examples show how to use android.bluetooth.BluetoothGatt#readDescriptor() . 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: UpdateService.java    From Android-nRF-Beacon-for-Eddystone with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void start(BluetoothGatt bluetoothGatt) {
    switch (requestType) {
        case READ_CHARACTERISTIC:
            if (!bluetoothGatt.readCharacteristic(characteristic)) {
                throw new IllegalArgumentException("Characteristic is not valid: " + characteristic.getUuid().toString());
            }
            break;
        case READ_DESCRIPTOR:
            if (!bluetoothGatt.readDescriptor(descriptor)) {
                throw new IllegalArgumentException("Descriptor is not valid");
            }
            break;
        case WRITE_CHARACTERISTIC:
            if (!bluetoothGatt.writeCharacteristic(characteristic)) {
                throw new IllegalArgumentException("Characteristic is not valid");
            }
            break;
        case WRITE_DESCRIPTOR:
            if (!bluetoothGatt.writeDescriptor(descriptor)) {
                throw new IllegalArgumentException("Characteristic is not valid");
            }
            break;
    }
}
 
Example 2
Source File: DescriptorReadOperation.java    From tap-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecute(@NonNull BluetoothGatt gatt) {
    BluetoothGattDescriptor d = extractDescriptor(gatt);
    if (d == null) {
        postOnError(ErrorStrings.NO_DESCRIPTOR);
        return;
    }

    if (!gatt.readDescriptor(d)) {
        postOnError(ErrorStrings.READ_OP_INIT_FAIL);
    }
}
 
Example 3
Source File: BleManagerHandler.java    From Android-BLE-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean internalReadDescriptor(@Nullable final BluetoothGattDescriptor descriptor) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || descriptor == null || !connected)
		return false;

	log(Log.VERBOSE, "Reading descriptor " + descriptor.getUuid());
	log(Log.DEBUG, "gatt.readDescriptor(" + descriptor.getUuid() + ")");
	return gatt.readDescriptor(descriptor);
}
 
Example 4
Source File: BleManager.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean internalReadDescriptor(final BluetoothGattDescriptor descriptor) {
	final BluetoothGatt gatt = bluetoothGatt;
	if (gatt == null || descriptor == null)
		return false;

	return gatt.readDescriptor(descriptor);
}
 
Example 5
Source File: BleGattExecutor.java    From Bluefruit_LE_Connect_Android with MIT License 5 votes vote down vote up
private BleGattExecutor.ServiceAction serviceReadAction(final BluetoothGattService gattService, final String characteristicUuidString, final String descriptorUuidString) {
    return new BleGattExecutor.ServiceAction() {
        @Override
        public boolean execute(BluetoothGatt bluetoothGatt) {
            final UUID characteristicUuid = UUID.fromString(characteristicUuidString);
            final BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(characteristicUuid);
            if (characteristic != null) {
                if (descriptorUuidString == null) {
                    // Read Characteristic
                    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0) {
                        bluetoothGatt.readCharacteristic(characteristic);
                        return false;
                    } else {
                        Log.w(TAG, "read: characteristic not readable: " + characteristicUuidString);
                        return true;
                    }
                } else {
                    // Read Descriptor
                    final UUID descriptorUuid = UUID.fromString(descriptorUuidString);
                    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descriptorUuid);
                    if (descriptor != null) {
                        bluetoothGatt.readDescriptor(descriptor);
                        return false;
                    } else {
                        Log.w(TAG, "read: descriptor not found: " + descriptorUuidString);
                        return true;
                    }
                }
            } else {
                Log.w(TAG, "read: characteristic not found: " + characteristicUuidString);
                return true;
            }
        }
    };
}
 
Example 6
Source File: ReadGattDescriptorTransaction.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
@Override
protected void transaction(GattTransactionCallback callback) {
    super.transaction(callback);
    getConnection().setState(GattState.READING_DESCRIPTOR);
    boolean success = false;
    BluetoothGatt localGatt = getConnection().getGatt();
    if(localGatt != null) {
        try {
            success = localGatt.readDescriptor(descriptor);
        } catch (NullPointerException ex) {
            Timber.w(ex, "[%s] We are going to fail this tx due to the stack NPE, this is probably poor peripheral behavior, this should become a FW bug.", getDevice());
            if (getDevice() != null) {
                Timber.w("[%s] btDevice %s characteristic %s", getDevice(), getDevice().getBtDevice(), this.descriptor.getUuid());
            }
            // Ensure that the flag is set to false, and that is is
            // impossible to be anything else stepping through after
            // this ... strategy time
            success = false;
        }
    } else {
        Timber.w("Couldn't read descriptor because gatt was null");
    }
    TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
    if (!success) {
        getConnection().setState(GattState.READ_DESCRIPTOR_FAILURE);
        builder.resultStatus(TransactionResult.TransactionResultStatus.FAILURE)
            .gattState(getConnection().getGattState());
        mainThreadHandler.post(() -> {
            callCallbackWithTransactionResultAndRelease(callback, builder.build());
            getConnection().setState(GattState.IDLE);
            // we want to apply this strategy to every phone, so we will provide an empty target android
            // device
            Strategy strategy = strategyProvider.
                getStrategyForPhoneAndGattConnection(null, getConnection(),
                    Situation.TRACKER_WENT_AWAY_DURING_GATT_OPERATION);
            if (strategy != null) {
                strategy.applyStrategy();
            }
        });
    }
}
 
Example 7
Source File: DfuBaseService.java    From microbit with Apache License 2.0 4 votes vote down vote up
/**
 * Reads the value of the Service Changed Client Characteristic Configuration descriptor (CCCD).
 *
 * @param gatt           the GATT device
 * @param characteristic the Service Changed characteristic
 * @return <code>true</code> if Service Changed CCCD is enabled ans set to INDICATE
 * @throws DeviceDisconnectedException
 * @throws DfuException
 * @throws UploadAbortedException
 */
private boolean isServiceChangedCCCDEnabled(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) throws DeviceDisconnectedException, DfuException, UploadAbortedException {
    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to read Service Changed CCCD", mConnectionState);
    // If the Service Changed characteristic or the CCCD is not available we return false.
    if (characteristic == null)
        return false;

    final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
    if (descriptor == null)
        return false;

    mRequestCompleted = false;
    mError = 0;

    logi("Reading Service Changed CCCD value...");
    sendLogBroadcast(LOG_LEVEL_VERBOSE, "Reading Service Changed CCCD value...");

    gatt.readDescriptor(descriptor);

    // We have to wait until device receives a response or an error occur
    try {
        synchronized (mLock) {
            while ((!mRequestCompleted && mConnectionState == STATE_CONNECTED_AND_READY && mError == 0 && !mAborted) || mPaused)
                mLock.wait();
        }
    } catch (final InterruptedException e) {
        loge("Sleeping interrupted", e);
    }

    if (mAborted)
        throw new UploadAbortedException();

    if (mError != 0)
        throw new DfuException("Unable to read Service Changed CCCD", mError);

    if (mConnectionState != STATE_CONNECTED_AND_READY)
        throw new DeviceDisconnectedException("Unable to read Service Changed CCCD", mConnectionState);

    return mServiceChangedIndicationsEnabled;
}
 
Example 8
Source File: DescriptorReadOperation.java    From RxAndroidBle with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
    return bluetoothGatt.readDescriptor(bluetoothGattDescriptor);
}
 
Example 9
Source File: GattDescriptorReadOperation.java    From puck-central-android with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(BluetoothGatt gatt) {
    L.d("Reading from " + mDescriptor);
    BluetoothGattDescriptor descriptor = gatt.getService(mService).getCharacteristic(mCharacteristic).getDescriptor(mDescriptor);
    gatt.readDescriptor(descriptor);
}