尝试过滤蓝牙扫描结果时崩溃

时间:2015-09-04 11:05:04

标签: android bluetooth-lowenergy

我正在为Android编写一个玩具应用程序,它使用蓝牙来读取健身带的特征,因此我只对这些乐队感兴趣所以我试图使用此代码过滤我的扫描结果:

private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {

        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (device.getAddress().contains("88:0F:10") | device.getName().equals("MI")) { //For some reason this crashes the app
                        mLeDeviceListAdapter.addDevice(device);
                        mLeDeviceListAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    };

麻烦的是,当我运行此代码时,应用程序立即崩溃,特别是在条件上,没有它,应用程序运行完美。 什么可能导致这次崩溃?

2 个答案:

答案 0 :(得分:2)

以下一种方式重写条件:

if (device.getAddress() != null && device.getAddress().contains("88:0F:10") && device.getName() != null && device.getName().equals("MI")) {
...
}

方法BluetoothDevice.getName()可以返回null。

答案 1 :(得分:1)

或者条件应该有两个管道字符:

  

if(device.getAddress()。contains(" 88:0F:10")|| device.getName()。equals(" MI"))