c# - 为什么我的 BLE 设备数据是来自配置索引的 FFFFFFF?
问题描述
基本上,我应该从蓝牙 LE 护肤设备中检索一个值。在设备拥有的服务中,设备有一个配置服务,有两个特点:一个是读写数据,另一个是读写配置值。
我有显示所有 UUID、它们的特征、属性、值等的文档。配置索引也都列在文件中,并在设备中预编程。
目标:这就是我卡住的地方。其中一个配置索引的值为12。我必须将配置索引写入特征,然后作为回报,程序会读取配置索引对应的数据,并在终端上打印数据。我发送了一个值 0C(十六进制的 12),并以 FFFFFFFFFFFFFFFFFFFF 结束。它必须是不同的值。
我怎样才能实现这个目标?
这是我到目前为止的代码:
while (true)
{
if (device == null)
{
Thread.Sleep(200);
}
else
{
Console.WriteLine("Press any key to pair the device.");
Console.ReadKey();
Console.WriteLine();
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
Console.WriteLine("Attempting to pair with device");
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var services = result.Services;
foreach (var service in services)
{
if (service.Uuid.ToString() == configSvc)
{
GattCharacteristicsResult charactiristicResult = await service.GetCharacteristicsAsync();
if (charactiristicResult.Status == GattCommunicationStatus.Success)
{
var characteristics = charactiristicResult.Characteristics;
foreach (var characteristic in characteristics)
{
GattReadResult rResult = await characteristic.ReadValueAsync();
GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
if (properties.HasFlag(GattCharacteristicProperties.Write))
{
var writer = new DataWriter();
GattCommunicationStatus wResult = await characteristic.WriteValueAsync(writer.DetachBuffer());
if (characteristic.Uuid.ToString() == configIdxCt)
{
int psCalVal = 12;
//var startCommand = Encoding.ASCII.GetBytes(psCalVal.ToString());
//Console.WriteLine(startCommand[0]);
writer.WriteByte(0xc0);
if (wResult == GattCommunicationStatus.Success)
{
Console.WriteLine("message sent successfully");
}
else
{
Console.WriteLine("Error encountered on writing to characteristic!");
}
}
}
if(properties.HasFlag(GattCharacteristicProperties.Read))
{
if (rResult.Status == GattCommunicationStatus.Success)
{
if (characteristic.Uuid.ToString() == configValCt)
{
var reader = DataReader.FromBuffer(rResult.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
//Console.WriteLine(input[0]);
Console.WriteLine("Data: ");
string psCArrayWDash = BitConverter.ToString(input) + " ";
string psCArray = psCArrayWDash.Replace("-", "").ToLower();
Console.WriteLine(psCArray);
}
if (characteristic.Uuid.ToString() == measurementCt)
{
Console.WriteLine("Read");
var reader = DataReader.FromBuffer(rResult.Value);
byte[] input = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(input);
Console.WriteLine("Data: ");
for (int i = 0; i < input.Length; i++)
Console.WriteLine(input[i].ToString() + " ");
}
}
else
Console.WriteLine("ERROR");
}
}
}
}
}
}
else
Console.WriteLine("Please, make sure the device is connected, and try again.");
}
}
deviceWatcher.Stop();