use*_*020 10 android bluetooth-lowenergy
我正在编写一个Android应用程序,使用BLE与Arduino交谈.我能够扫描设备,连接到目标,发现服务,获取特征,并读取可读的内容.但是,当我尝试编写可写特征时,该方法始终返回false.当我调试到android.bluetooth代码时,会发生以下序列:characteristic.getService().getDevice总是返回null,这会导致writeCharacteristic失败.
任何帮助是极大的赞赏!
小智 18
请按相应的顺序检查您的对象.每次我需要写入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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12842 次 |
| 最近记录: |