我正在使用 BLE 来发送和接收数据。
我可以将数据写入BLE
,但不会收到回应"OnCharacteristicChanged"
我在写入OnCharacteristicWrite
时会收到bluetoothgatt WriteCharacteristic
的回复。
请帮我修理一下。或建议我最佳做法或示例代码。 提前谢谢。
这是我的代码。
onDiscovered service
{
string ser = "0003CDD0-0000-1000-8000-00805f9b0131";
string charId = "0003CDD2-0000-1000-8000-00805f9b0131";
bluetoothGatt = gatt;
//bleMTU = bluetoothGatt.RequestMtu(400);
mService = bluetoothGatt.GetService(UUID.FromString(ser));
charbt = mService.GetCharacteristic(UUID.FromString(charId));
bluetoothGatt.SetCharacteristicNotification(charbt, true);
}
Void writetosocket
{
byte[] smallPacket = new byte[numberOfBytes];
charbt.SetValue(smallPacket);
charbt.WriteType = BluetoothGattCharacteristic.WriteTypeNoResponse;
bool check = bluetoothGatt.WriteCharacteristic(charbt);
}
答案 0 :(得分:0)
您似乎缺少订阅ReadCharacteristic。以下是我在自定义GattCallback中实现该部分的方法:
class GattCallback : BluetoothGattCallback
{
public BluetoothGattCharacteristic ReadCharacteristic { get; set; }
public BluetoothGattCharacteristic WriteCharacteristic { get; set; }
public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
{
var service = gatt.Services.FirstOrDefault(s => s.Uuid.ToString().Equals("0003CDD0-0000-1000-8000-00805f9b0131"));
WriteCharacteristic = service.Characteristics[0]; //Find your WriteCharacteristic
ReadCharacteristic = service.Characteristics[1]; //Find your ReadCharacteristic
gatt.SetCharacteristicNotification(ReadCharacteristic, true);
//Subscribe on ReadCharacteristic for communication
var readCharacteristicDiscriptor = ReadCharacteristic.Descriptors[0];
readCharacteristicDiscriptor.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
gatt.WriteDescriptor(readCharacteristicDiscriptor);
}
}