我从BLE设备写入和读取时遇到问题。 我已尝试使用" BLE utils"应用程序(在iOS应用程序商店)和这个应用程序,我可以编写和查看RealTerm软件上的结果,我用来检查我的BLE设备上的结果。 但如果我的Android应用程序不适用,并使用此方法: (我有一个按钮用于扫描+发现服务+搜索特征,还有一个按钮用于将字符串发送到具有WRITE属性的特征)
public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic, String text) {
if (MainActivity.myBluetoothAdapter == null || FragmentOne.bluetoothGatt == null) {
Log.w("WRITE", "BluetoothAdapter not initialized");
return false;
}
byte tx = (byte) Integer.parseInt(text);
byte[] ch = new byte[1];
ch[0] = tx;
characteristic.setValue(ch);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
return FragmentOne.bluetoothGatt.writeCharacteristic(characteristic);
}
此方法将调用:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d("Connect","------------- onCharacteristicWrite status: " + status);
// do somethings here.
}
在最后一个中,结果值为0,但我无法看到RealTerm上的值,就像iOS应用程序一样。 我做错了什么?
编辑1: 这是按下发送按钮后的日志。
08-01 10:37:59.122 602-602 / D / BluetoothGatt:writeCharacteristic() - uuid:7fb03169-d527-4ca8-b9f0-c6f4a887c2fa 08-01 10:37:59.152 602-602 / D /结果:Val:true 08-01 10:37:59.152 602-940 / D / BluetoothGatt:onCharacteristicWrite() - Device = 00:07:80:C0:4B:E9 UUID = 7fb03169-d527-4ca8-b9f0-c6f4a887c2fa Status = 0 08-01 10:37:59.152 602-940 / D / Connect:------------- onCharacteristicWrite status:0
编辑2: @zomb
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
Log.i("onServicesDiscovered", services.toString());
List<BluetoothGattCharacteristic> characteristics = null;
for (BluetoothGattService service : services) {
Log.d("SERVICE","Service with address: " + service.getUuid());
characteristics = service.getCharacteristics();
for(BluetoothGattCharacteristic ch : characteristics)
{
Log.d("CHAR","Has characteristic with adress: " + ch.getUuid());
if (((ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) | ( ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) {
// writing characteristic functions
//Here it's where ill save the various Uuid of services and characteristics.
Log.d("WRITECHAR", "WRITE UUID : " + ch.getUuid());
}
else if((ch.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) > 0)
{
//And Here...
Log.d("READCHAR","READ UUID : " + ch.getUuid());
}
}
}
}
经过多次尝试,我将各种服务的不同UUID保存到文件中。特性的影响。