mBluetoothGatt.getService(uuid)返回null

beg*_*ner 5 service android bluetooth bluetooth-lowenergy

在我的应用程序中,我正在传递助听器服务的UUID号,如google的BLE示例中那样,即0000a00-0000-1000-8000-00805f9b34fb

但是getservice返回null表示BluetoothGatt不支持该服务。为什么会这样,有人可以帮助我吗?

小智 5

您必须首先根据文档发现给定设备的所有服务。

此功能要求已为给定设备完成服务发现。 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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

或者你可以只运行一个搜索

for (BluetoothGattService gattService : gattServices) {
    Log.i(TAG, "Service UUID Found: " + gattService.getUuid().toString());
}
Run Code Online (Sandbox Code Playgroud)