下面列出了android.bluetooth.BluetoothGatt#readDescriptor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
@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);
}
}
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);
}
private boolean internalReadDescriptor(final BluetoothGattDescriptor descriptor) {
final BluetoothGatt gatt = bluetoothGatt;
if (gatt == null || descriptor == null)
return false;
return gatt.readDescriptor(descriptor);
}
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;
}
}
};
}
@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();
}
});
}
}
/**
* 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;
}
@Override
protected boolean startOperation(BluetoothGatt bluetoothGatt) {
return bluetoothGatt.readDescriptor(bluetoothGattDescriptor);
}
@Override
public void execute(BluetoothGatt gatt) {
L.d("Reading from " + mDescriptor);
BluetoothGattDescriptor descriptor = gatt.getService(mService).getCharacteristic(mCharacteristic).getDescriptor(mDescriptor);
gatt.readDescriptor(descriptor);
}