有人可以帮我从值字段中提取数据吗?
我只想显示字节数据。读取方法
public async Task<NSData> ReadValue(CBPeripheral peripheral, CBCharacteristic characteristic)
{
BTProgressHUD.Show("Loading...", 1000, ProgressHUD.MaskType.Gradient);
var taskCompletion = new TaskCompletionSource<bool>();
var task = taskCompletion.Task;
EventHandler<CBCharacteristicEventArgs> handler = (s, e) =>
{
if (e.Characteristic.UUID?.Uuid == characteristic.UUID?.Uuid)
{
// e.Characteristic.Value = characteristic.Value;
taskCompletion.SetResult(true);
}
};
try
{
peripheral.UpdatedCharacterteristicValue += handler;
System.Threading.Thread.Sleep(900);
peripheral.ReadValue(characteristic);
BTProgressHUD.Dismiss();
await Task.Delay(200);
return characteristic.Value;
}
finally
{
//以获取更新的特征值。
peripheral.UpdatedCharacterteristicValue -= handler;
BTProgressHUD.Dismiss();
}
}
您可以将其转换为字符串
var value = characteristic.Value;
NSString dataValue = NSString.FromData(value, NSStringEncoding.UTF8);