我正在编写一个Android应用程序,使用BLE与Arduino交谈。我能够扫描设备,连接到目标,发现服务,获取特征,并读取可读的内容。但是,当我尝试编写可写特征时,该方法始终返回false。当我调试到android.bluetooth代码时,会发生以下序列: characteristic.getService()。getDevice总是返回null, 这会导致writeCharacteristic失败。
非常感谢任何帮助!
答案 0 :(得分:17)
请按相应的顺序检查您的对象。每次我需要写入BLE设备时,我只保留BluetoothGatt对象并创建BluetoothGattService和BluetoothGattCharacteristic。
byte[] data_to_write; // Your data
BluetoothManager mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SREVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
BluetoothDevice mDevice = mBluetoothAdapter.getRemoteDevice(....);
BluetoothGatt mBG = mDevice.connectGatt(....);
BluetoothGattService mSVC = mBG.getService(service_uuid);
BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(characteristic_uuid);
mCH.setValue(data_to_write);
mBG.writeCharacteristic(mCH);
答案 1 :(得分:0)
有些字符是不可写的。 上面的代码应该使用一些副作用。 (不一样的字符或......)