如何从CBCharacteristics获取数据?

问题描述 投票:-2回答:1

enter image description here

有人可以帮我从值字段中提取数据吗?

我只想显示字节数据。读取方法

            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();
        }
    }
ios xamarin xamarin.ios bluetooth-lowenergy
1个回答
0
投票

您可以将其转换为字符串

var value = characteristic.Value;
NSString dataValue = NSString.FromData(value, NSStringEncoding.UTF8);
© www.soinside.com 2019 - 2024. All rights reserved.