android BluetoothGatt.writeCharacteristic()写入返回false

http://stackoverflow.com/questions/23879869/bluetoothgatt-writecharacteristic-always-returns-false
服务和写入特性在每次需要写入的时候重新获取,只有BluetoothGatt反复使用:

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);
好的,下面是一个安卓向BLE蓝牙设备写入文本的完整用例: 1. 首先,在你的Android应用中,需要添加BLE蓝牙权限和蓝牙设备扫描权限。 ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> ``` 2. 在你的Activity中,你需要定义一些变量,例如: ```java private BluetoothAdapter bluetoothAdapter; private BluetoothGatt bluetoothGatt; private BluetoothGattCharacteristic characteristic; private String deviceAddress = "你的蓝牙设备地址"; private String serviceUUID = "你的蓝牙服务UUID"; private String characteristicUUID = "你的蓝牙特征UUID"; ``` 3. 接下来,你需要初始化BluetoothAdapter和BluetoothGatt,扫描并连接到蓝牙设备: ```java bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); bluetoothGatt = device.connectGatt(this, false, gattCallback); ``` 4. 在连接成功后,你需要在BluetoothGattCallback中寻找你的蓝牙服务和特征: ```java private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { gatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 断开连接 } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(UUID.fromString(serviceUUID)); if (service != null) { characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID)); } } } }; ``` 5. 在找到你的特征后,你可以向蓝牙设备写入文本: ```java String text = "这是要写入的文本"; byte[] value = text.getBytes(Charset.forName("UTF-8")); characteristic.setValue(value); bluetoothGatt.writeCharacteristic(characteristic); ``` 6. 最后,你需要在BluetoothGattCallback中处理写入结果: ```java @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) { // 写入成功 } else { // 写入失败 } } ``` 以上就是安卓向BLE蓝牙设备写入文本的完整用例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值