将UUID与GattService List进行比较

时间:2017-10-11 13:22:25

标签: android bluetooth-lowenergy uuid characteristics

我使用过Android的BluetoothLeGatt示例,现在我想根据我连接的设备的UUID加载xml文件。

我尝试将UUID与以下代码进行比较。

mBluetoothLeService.getSupportedGattServices().equals("0003CBBB-0000-1000-8000-00805F9B0131")

使用功能:

public List<BluetoothGattService> getSupportedGattServices() {
    if (mBluetoothGatt == null) return null;

    return mBluetoothGatt.getServices();
}

我做错了什么?

以下是错误代码:

FATAL EXCEPTION: main
                                                                               Process: com.example.android.bluetoothlegatt, PID: 1314
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluetoothlegatt/com.example.android.bluetoothlegatt.DeviceControlActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.android.bluetoothlegatt.BluetoothLeService.getSupportedGattServices()' on a null object reference
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.android.bluetoothlegatt.BluetoothLeService.getSupportedGattServices()' on a null object reference
                                                                                   at com.example.android.bluetoothlegatt.DeviceControlActivity.onCreate(DeviceControlActivity.java:483)
                                                                                   at android.app.Activity.performCreate(Activity.java:6912)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358

1 个答案:

答案 0 :(得分:1)

据我了解你的问题:

List<BluetoothGattService> services = mBluetoothLeService.getSupportedGattServices();
for (int i = 0; i < services.size(); i++) {
        BluetoothGattService gattService = services.get(i);
 if(gattService.getUuid().toString().equalsIgnoreCase("0003CBBB-0000-1000-8000-00805F9B0131"))
        Log.e("SERVICES : ", "" + gattService.getUuid());
        }
    }