mBluetoothGatt.getService(uuid)返回null [英] mBluetoothGatt.getService(uuid) returns null
问题描述
在我的应用中,我正在传递助听器服务的UUID编号(如google的BLE示例所示),即0000a00-0000-1000-8000-00805f9b34fb
In my app , i am passng the UUID number of the hearing aid service as in the BLE sample from google i.e. 0000a00-0000-1000-8000-00805f9b34fb
但是getservice返回null表示BluetoothGatt不支持该服务. 为什么会这样,有人可以帮助我吗?
But the getservice returns null means that the service is not supported by the BluetoothGatt . Why is this happening , can anybody please help me .
推荐答案
您必须首先根据文档查找给定设备的所有服务.
You have to first discover all services for the given device per documentation.
此功能要求已完成给定设备的服务发现. http://developer.android.com/参考/android/bluetooth/BluetoothGatt.html#getService(java.util.UUID)
This function requires that service discovery has been completed for the given device. http://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#getService(java.util.UUID)
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService mBluetoothGattService = mBluetoothGatt.getService(UUID.fromString(serviceUUID));
if (mBluetoothGattService != null) {
Log.i(TAG, "Service characteristic UUID found: " + mBluetoothGattService.getUuid().toString());
} else {
Log.i(TAG, "Service characteristic not found for UUID: " + serviceUUID);
}
}
或者您也可以进行搜索
for (BluetoothGattService gattService : gattServices) {
Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
}
这篇关于mBluetoothGatt.getService(uuid)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!