下面列出了android.bluetooth.BluetoothGatt#GATT_SUCCESS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
print("发现服务...");
if (BluetoothGatt.GATT_SUCCESS == status) {
List<BluetoothGattService> gattServices = gatt.getServices();
if (null == gattServices || gattServices.size() == 0) {
return;
}
for (BluetoothGattService gattService : gattServices) {
List<BluetoothGattCharacteristic> characteristics = gattService.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
String uuid = characteristic.getUuid().toString();
print("UUID Cha:" + uuid);
if (UUID_BT5_DATA.equalsIgnoreCase(uuid)) {
mBluetoothGatt.setCharacteristicNotification(characteristic, true);
print("开始监听...");
}
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public final void onPhyUpdate(@NonNull final BluetoothGatt gatt,
@PhyValue final int txPhy, @PhyValue final int rxPhy,
final int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
log(Log.INFO, "PHY updated (TX: " + ParserUtils.phyToString(txPhy) +
", RX: " + ParserUtils.phyToString(rxPhy) + ")");
if (request instanceof PhyRequest) {
((PhyRequest) request).notifyPhyChanged(gatt.getDevice(), txPhy, rxPhy);
request.notifySuccess(gatt.getDevice());
}
} else {
log(Log.WARN, "PHY updated failed with status " + status);
if (request instanceof PhyRequest) {
request.notifyFail(gatt.getDevice(), status);
awaitingRequest = null;
}
postCallback(c -> c.onError(gatt.getDevice(), ERROR_PHY_UPDATE, status));
}
// PHY update may be requested by the other side, or the Android, without explicitly
// requesting it. Proceed with the queue only when update was requested.
if (checkCondition() || request instanceof PhyRequest) {
nextRequest(true);
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, final int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
String address = gatt.getDevice().getAddress();
final BleRssiCallback callback = mRssiCallbackMap.get(address);
final BleDevice device = getBleDeviceFromMap(address, mConnectCallbackMap);
mHandler.post(new Runnable() {
@Override
public void run() {
if (callback != null) {
callback.onRssi(rssi, device);
}
}
});
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (originCallback != null) {
easyBle.getExecutorService().execute(() -> originCallback.onCharacteristicRead(gatt, characteristic, status));
}
if (currentRequest != null) {
if (currentRequest.type == RequestType.READ_CHARACTERISTIC) {
if (status == BluetoothGatt.GATT_SUCCESS) {
notifyCharacteristicRead(currentRequest, characteristic.getValue());
} else {
handleGattStatusFailed();
}
executeNextRequest();
}
}
}
@Override
public int writeCharacteristic(BluetoothGattCharacteristic characteristic, int offset, byte[] value) {
if (offset != 0) {
return BluetoothGatt.GATT_INVALID_OFFSET;
}
// Heart Rate control point is a 8bit characteristic
if (value.length != 1) {
return BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH;
}
if ((value[0] & 1) == 1) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mHeartRateMeasurementCharacteristic.setValue(INITIAL_EXPENDED_ENERGY,
EXPENDED_ENERGY_FORMAT, /* offset */ 2);
mEditTextEnergyExpended.setText(Integer.toString(INITIAL_EXPENDED_ENERGY));
}
});
}
return BluetoothGatt.GATT_SUCCESS;
}
@Override
public final void onDescriptorWrite(final BluetoothGatt gatt, final BluetoothGattDescriptor descriptor, final int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
// The value has been written. Notify the profile and proceed with the initialization queue.
profile.onDescriptorWrite(gatt, descriptor);
operationInProgress = false;
nextRequest();
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
if (gatt.getDevice().getBondState() != BluetoothDevice.BOND_NONE) {
// This should never happen but it used to: http://stackoverflow.com/a/20093695/2115352
DebugLogger.w(TAG, ERROR_AUTH_ERROR_WHILE_BONDED);
onError(gatt.getDevice(), ERROR_AUTH_ERROR_WHILE_BONDED, status);
}
} else {
DebugLogger.e(TAG, "onDescriptorWrite error " + status);
onError(gatt.getDevice(), ERROR_WRITE_DESCRIPTOR, status);
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
if (gatt == null || gatt.getDevice() == null)return;
UUID uuid = descriptor.getCharacteristic().getUuid();
BleLog.d(TAG, "write descriptor uuid:" + uuid);
synchronized (locker) {
T bleDevice = getBleDeviceInternal(gatt.getDevice().getAddress());
if (status == BluetoothGatt.GATT_SUCCESS) {
if (null != descWrapperCallback){
descWrapperCallback.onDescWriteSuccess(bleDevice, descriptor);
}
if (notifyCharacteristics.size() > 0 && notifyIndex < notifyCharacteristics.size()) {
BleLog.d(TAG, "set characteristic notification, notify_index is "+notifyIndex);
setCharacteristicNotification(gatt.getDevice().getAddress(), true);
} else {
BleLog.d(TAG, "set characteristic notification is completed");
if (notifyWrapperCallback != null) {
if (Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)
|| Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.ENABLE_INDICATION_VALUE)){
notifyWrapperCallback.onNotifySuccess(bleDevice);
}else if (Arrays.equals(descriptor.getValue(), BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE)){
notifyWrapperCallback.onNotifyCanceled(bleDevice);
}
}
}
}else {
if (null != descWrapperCallback){
descWrapperCallback.onDescWriteFailed(bleDevice, status);
}
}
}
}
@Override
public void onReadRemoteRssi(int rssi, int status) {
stopRequestTiming();
if (status == BluetoothGatt.GATT_SUCCESS) {
putIntExtra(Constants.EXTRA_RSSI, rssi);
onRequestCompleted(Code.REQUEST_SUCCESS);
} else {
onRequestCompleted(Code.REQUEST_FAILED);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public synchronized void onDescriptorWrite(BluetoothGatt gatt, final BluetoothGattDescriptor descriptor,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "onDescriptorWrite: Wrote GATT Descriptor successfully.");
descriptor_callback_failures = 0;
} else {
Log.d(TAG, "onDescriptorWrite: Error writing GATT Descriptor: " + status);
if (descriptor_callback_failures == 0) {
descriptor_callback_failures++;
// TODO not sure if we want this retry here..
try {
if (!mBluetoothGatt.writeDescriptor(descriptor)) {
Log.d(TAG, "Failed to write descriptor in on descriptor write retry");
unBondBlucon();
} else {
UserError.Log.d(TAG, "Tried to write descriptor again inside onDescriptorWrite");
}
} catch (Exception e) {
UserError.Log.e(TAG, "Exception during callback retry: " + e);
}
} else {
unBondBlucon();
}
}
descriptor_time = 0;
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.w(TAG, "mBluetoothGatt = " + mBluetoothGatt );
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG_LOG, "onServicesDiscovered received: " + status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(UUID.fromString(service_ancs));
if (service == null) {
Log.d(TAG_LOG, "cant find service");
} else {
Log.d(TAG_LOG, "find service");
Log.d(TAG_LOG, String.valueOf(bluetooth_gatt.getServices()));
// subscribe data source characteristic
BluetoothGattCharacteristic data_characteristic = service.getCharacteristic(UUID.fromString(characteristics_data_source));
if (data_characteristic == null) {
Log.d(TAG_LOG, "cant find data source chara");
} else {
Log.d(TAG_LOG, "find data source chara :: " + data_characteristic.getUuid());
Log.d(TAG_LOG, "set notify:: " + data_characteristic.getUuid());
bluetooth_gatt.setCharacteristicNotification(data_characteristic, true);
BluetoothGattDescriptor descriptor = data_characteristic.getDescriptor(
UUID.fromString(descriptor_config));
if(descriptor == null){
Log.d(TAG_LOG, " ** cant find desc :: " + descriptor.getUuid());
}else{
Log.d(TAG_LOG, " ** find desc :: " + descriptor.getUuid());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetooth_gatt.writeDescriptor(descriptor);
if(api_level >= 21) {
stop_le_scanner();
}else {
bluetooth_adapter.stopLeScan(le_scan_callback);
}
}
}
}
}
}
@Override
public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
// Notify waiting thread
logi("onServicesDiscovered() :: Start");
if (status == BluetoothGatt.GATT_SUCCESS) {
logi("onServicesDiscovered() :: Services discovered");
mConnectionState = STATE_CONNECTED_AND_READY;
} else {
loge("onServicesDiscovered() :: Service discovery error: " + status);
mError = ERROR_CONNECTION_MASK | status;
}
synchronized (mLock) {
mLock.notifyAll();
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicWrite(BluetoothGattCharacteristic characteristic, int status, byte[] value) {
stopRequestTiming();
if (status == BluetoothGatt.GATT_SUCCESS) {
onRequestCompleted(Code.REQUEST_SUCCESS);
} else {
onRequestCompleted(Code.REQUEST_FAILED);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onRxDataReceived(@NonNull byte[] data, @Nullable String identifier, int status) {
if (status != BluetoothGatt.GATT_SUCCESS) {
Log.w(TAG, "onRxDataReceived error:" + status);
return;
}
UartPacket uartPacket = new UartPacket(identifier, UartPacket.TRANSFERMODE_RX, data);
// Mqtt publish to RX
if (mMqttManager != null) {
if (MqttSettings.isPublishEnabled(mContext)) {
final String topic = MqttSettings.getPublishTopic(mContext, MqttSettings.kPublishFeed_RX);
if (topic != null) {
final int qos = MqttSettings.getPublishQos(mContext, MqttSettings.kPublishFeed_RX);
mMqttManager.publish(topic, uartPacket.getData(), qos);
}
}
}
try {
mPacketsSemaphore.acquire();
} catch (InterruptedException e) {
Log.w(TAG, "InterruptedException: " + e.toString());
}
mReceivedBytes += data.length;
if (mIsPacketCacheEnabled) {
mPackets.add(uartPacket);
}
// Send data to delegate
Listener listener = mWeakListener.get();
if (listener != null) {
mMainHandler.post(() -> listener.onUartPacket(uartPacket));
}
mPacketsSemaphore.release();
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "Services Discovered: " + status);
authenticateConnection(gatt);
}
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean isSuccess() {
return this.status == BluetoothGatt.GATT_SUCCESS;
}