Android 执行时BluetoothAdapter.getRemoteDevice(MAC)中存在Null对象,但在使用断点调试时不存在

Android 执行时BluetoothAdapter.getRemoteDevice(MAC)中存在Null对象,但在使用断点调试时不存在,android,bluetooth,Android,Bluetooth,我正在尝试修改蓝牙服务特性。 我有一个可以工作的代码,但只有当我用断点调试它时 我知道蓝牙设备MAC,所以我认为bluetoothAdapter.GetRemoteDevice(MAC)是获取该设备的最简单方法。我已经知道这个方法返回一个设备,即使它找不到它。但为什么只有当我用断点调试它时它才工作呢 以下是我的代码片段: btAdapter = getBtAdapter(); btDevice = btAdapter.getRemoteDevice(macDev1); btSocket = bt

我正在尝试修改蓝牙服务特性。 我有一个可以工作的代码,但只有当我用断点调试它时

我知道蓝牙设备MAC,所以我认为bluetoothAdapter.GetRemoteDevice(MAC)是获取该设备的最简单方法。我已经知道这个方法返回一个设备,即使它找不到它。但为什么只有当我用断点调试它时它才工作呢

以下是我的代码片段:

btAdapter = getBtAdapter();
btDevice = btAdapter.getRemoteDevice(macDev1);
btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.randomUUID());

Method m = btDevice.getClass().getMethod("createRfcommSocket",new Class[] {int.class});
btSocket = (BluetoothSocket) m.invoke(btDevice, 1);
btGatt = btDevice.connectGatt(this, false, btGattCallback);
btGattServ = btGatt.getService(servUuid); //Servicio
btGattCharast = btGattServ.getCharacteristic(gattCharUuid); //This line is the one that fails.
最后一行是失败的。但我认为这是因为getRemoteDevice没有找到该设备,并且返回了一个随机的设备。错误是这样的:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)' on a null object reference
GATTCHARUUID是以前声明的变量,因此不能为空