下面列出了android.bluetooth.BluetoothGatt#GATT_FAILURE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.d(TAG, "onConnectionStateChange, New state : " + newState + ", Status : " + status);
if (status == BluetoothGatt.GATT_FAILURE) {
EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
return;
} else if (status == 133) {
EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
return;
} else if (status != BluetoothGatt.GATT_SUCCESS) {
// TODO need to check this status
return;
}
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.e(TAG, "Connected to GATT server.");
gatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.e(TAG, "Disconnected from GATT server.");
EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
}
}
private String getGattStatusMessage(final int status) {
final String statusMessage;
if (status == BluetoothGatt.GATT_SUCCESS) {
statusMessage = "SUCCESS";
} else if (status == BluetoothGatt.GATT_FAILURE) {
statusMessage = "FAILED";
} else if (status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED) {
statusMessage = "NOT PERMITTED";
} else if (status == 133) {
statusMessage = "Found the strange 133 bug";
} else {
statusMessage = "UNKNOWN (" + status + ")";
}
return statusMessage;
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
// Notify connection failure if service discovery failed.
if (status == BluetoothGatt.GATT_FAILURE) {
connectFailure();
return;
}
// Save reference to each UART characteristic.
tx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(TX_CHAR_UUID);
rx = gatt.getService(SERIAL_SERVICE_UUID).getCharacteristic(RX_CHAR_UUID);
enableRXNotification();
// Notify of connection completion.
notifyOnConnected(context);
}
@Test
public void testGeneral() {
CommandResult commandResult = new CommandResult(Neatle.createUUID(1), new byte[]{22, 21}, BluetoothGatt.GATT_SUCCESS, 1234556);
assertEquals(BluetoothGatt.GATT_SUCCESS, commandResult.getStatus());
assertEquals(1234556, commandResult.getTimestamp());
assertEquals(Neatle.createUUID(1), commandResult.getUUID());
assertTrue(commandResult.wasSuccessful());
assertNotNull(commandResult.toString());
CommandResult commandResult2 = new CommandResult(Neatle.createUUID(1), new byte[]{22, 21}, BluetoothGatt.GATT_FAILURE, 1234556);
assertFalse(commandResult2.wasSuccessful());
}
@Test
public void testSuccessful() {
CommandResult result1 = new CommandResult(Neatle.createUUID(1), null, BluetoothGatt.GATT_SUCCESS, 0);
CommandResult result2 = new CommandResult(Neatle.createUUID(2), null, BluetoothGatt.GATT_SUCCESS, 0);
CommandResult result3 = new CommandResult(Neatle.createUUID(3), null, BluetoothGatt.GATT_FAILURE, 0);
operationResults.addResult(result1);
assertTrue(operationResults.wasSuccessful());
operationResults.addResult(result2);
assertTrue(operationResults.wasSuccessful());
operationResults.addResult(result3);
assertFalse(operationResults.wasSuccessful());
}
public static String getGattStatus(int status) {
switch (status) {
case BluetoothGatt.GATT_SUCCESS:
return "GATT_SUCCESS";
case BluetoothGatt.GATT_READ_NOT_PERMITTED:
return "GATT_READ_NOT_PERMITTED";
case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
return "GATT_WRITE_NOT_PERMITTED";
case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
return "GATT_INSUFFICIENT_AUTHENTICATION";
case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
return "GATT_REQUEST_NOT_SUPPORTED";
case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
return "GATT_INSUFFICIENT_ENCRYPTION";
case BluetoothGatt.GATT_INVALID_OFFSET:
return "GATT_INVALID_OFFSET";
case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
return "GATT_INVALID_ATTRIBUTE_LENGTH";
case BluetoothGatt.GATT_FAILURE:
return "GATT_FAILURE";
default:
return "STATE_UNKNOWN: " + status;
}
}
@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
if (offset != 0) {
return BluetoothGatt.GATT_INVALID_OFFSET;
}
// Measurement Interval is a 16bit characteristic
if (value.length != 2) {
return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(value);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
final int newMeasurementIntervalValue = byteBuffer.getShort();
if (!isValidMeasurementIntervalValue(newMeasurementIntervalValue)) {
return BluetoothGatt.GATT_FAILURE;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mMeasurementIntervalCharacteristic.setValue(newMeasurementIntervalValue,
MEASUREMENT_INTERVAL_FORMAT,
/* offset */ 0);
if (mMeasurementIntervalCCCDescriptor.getValue() == BluetoothGattDescriptor.ENABLE_INDICATION_VALUE) {
resetTimer(newMeasurementIntervalValue);
mTextViewNotifications.setText(R.string.notificationsEnabled);
}
}
});
return BluetoothGatt.GATT_SUCCESS;
}
public void readCharacteristic(BluetoothGattServer gattServer, BluetoothDevice device,
int requestId, int offset,
BluetoothGattCharacteristic characteristic) {
// UUID uuid = characteristic.getUuid();
int status = BluetoothGatt.GATT_SUCCESS;
if (isLocked()) {
if (characteristic == mUnlockCharacteristic) {
log("Generating secure unlock challenge");
characteristic.setValue(new byte[16]);
new SecureRandom().nextBytes(characteristic.getValue());
} else {
if (characteristic != mLockStateCharacteristic) {
status = BluetoothGatt.GATT_READ_NOT_PERMITTED;
}
}
}
else if (characteristic == mUnlockCharacteristic) {
status = BluetoothGatt.GATT_READ_NOT_PERMITTED;
} else if (characteristic == mPublicEcdhKeyCharacteristic) {
log("ECDH Public Key was requested");
if (0 == offset) {
characteristic.setValue(null == mEidKeyPair ? new byte[0] : mEidKeyPair.getPublicKey());
}
} else if (characteristic == mAdvSlotDataCharacteristic) {
log("Advertisement slot data requested");
characteristic.setValue(mConfigCallback.getAdvertisedData());
} else if (characteristic == mEidIdentityKeyCharacteristic) {
log("Identity Key was requested");
byte[] identityKey = mConfigCallback.getEidIdentityKey();
if (null == identityKey) {
status = BluetoothGatt.GATT_FAILURE;
}
else {
characteristic.setValue(aes_transform(true, identityKey, 0, 16));
}
}
gattServer.sendResponse(device, requestId, status, offset,
status == BluetoothGatt.GATT_SUCCESS ? Arrays.copyOfRange(characteristic.getValue(), offset, characteristic.getValue().length) : null);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
// Notify connection failure if service discovery failed.
if (status == BluetoothGatt.GATT_FAILURE) {
connectFailure();
return;
}
// Save reference to each UART characteristic.
tx = gatt.getService(UART_UUID).getCharacteristic(TX_UUID);
rx = gatt.getService(UART_UUID).getCharacteristic(RX_UUID);
// Save reference to each DIS characteristic.
disManuf = gatt.getService(DIS_UUID).getCharacteristic(DIS_MANUF_UUID);
disModel = gatt.getService(DIS_UUID).getCharacteristic(DIS_MODEL_UUID);
disHWRev = gatt.getService(DIS_UUID).getCharacteristic(DIS_HWREV_UUID);
disSWRev = gatt.getService(DIS_UUID).getCharacteristic(DIS_SWREV_UUID);
// Add device information characteristics to the read queue
// These need to be queued because we have to wait for the response to the first
// read request before a second one can be processed (which makes you wonder why they
// implemented this with async logic to begin with???)
readQueue.offer(disManuf);
readQueue.offer(disModel);
readQueue.offer(disHWRev);
readQueue.offer(disSWRev);
// Request a dummy read to get the device information queue going
gatt.readCharacteristic(disManuf);
// Setup notifications on RX characteristic changes (i.e. data received).
// First call setCharacteristicNotification to enable notification.
if (!gatt.setCharacteristicNotification(rx, true)) {
// Stop if the characteristic notification setup failed.
connectFailure();
return;
}
// Next update the RX characteristic's client descriptor to enable notifications.
BluetoothGattDescriptor desc = rx.getDescriptor(CLIENT_UUID);
if (desc == null) {
// Stop if the RX characteristic has no client descriptor.
connectFailure();
return;
}
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
if (!gatt.writeDescriptor(desc)) {
// Stop if the client descriptor could not be written.
connectFailure();
return;
}
// Notify of connection completion.
notifyOnConnected(this);
}