简体   繁体   English

为什么我没有从我的 BLE 设备发送到我的 xamarin 表单应用程序的字节 [] 中获得正确的值?

[英]Why am I not getting the correct value from my byte [] that is being sent by my BLE-device to my xamarin forms app?

I am sending this from my BLE device:我从我的 BLE 设备发送这个:

BTLEserial.print ("one");
BTLEserial.print (",");
BTLEserial.print ("two"); 

So what I am sending is: "one, two" and i am now trying to get this value but with my current code I get 54 and 44 instead and I do not quite know why.所以我发送的是:“一,二”,我现在正在尝试获取这个值,但是使用我当前的代码,我得到了5444 ,我不太明白为什么。

I use this plugin: https://github.com/xabre/xamarin-bluetooth-le (Plugin.BLE)我使用这个插件: https : //github.com/xabre/xamarin-bluetooth-le (Plugin.BLE)

This is how I read the data:这是我读取数据的方式:

var adapter = CrossBluetoothLE.Current.Adapter;

await adapter.ConnectToDeviceAsync(mydevice);

var service = await mydevice.GetServiceAsync(Guid.Parse("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
var services = await mydevice.GetServicesAsync();

var RXcharacteristics = await service.GetCharacteristicAsync(Guid.Parse("6e400003-b5a3-f393-e0a9-e50e24dcca9e"));
var characteristics = await service.GetCharacteristicsAsync();

int whatResult = 0;
string valueone;
string valuetwo;

RXcharacteristics.ValueUpdated += (sender, e) =>
{
    var result = e.Characteristic.Value;

    foreach (var items in result)
    {
        if (whatResult == 0)
        {
            valueone = Convert.ToString(items);
            System.Diagnostics.Debug.Writeline(valueone);
            whatResult++;
        }
        else {
            valuetwo = Convert.ToString(items);
            System.Diagnostics.Debug.Writeline(valuetwo);
            whatResult = 0;
        }
    }

}; 

await RXcharacteristics.StartUpdatesAsync();

How come I cannot get the correct data from the result I am receiving?为什么我无法从我收到的结果中获得正确的数据? I also tried with var result = e.Characteristic.StringValue;我也试过var result = e.Characteristic.StringValue; but with the same result.但结果相同。

I googled and came across a person with the same issue: https://github.com/xabre/xamarin-bluetooth-le/issues/88我用谷歌搜索并遇到了一个有同样问题的人: https : //github.com/xabre/xamarin-bluetooth-le/issues/88

He for example said this, which showcase that my device has CanUpdate as true.例如,他这样说,这表明我的设备将CanUpdate为 true。 And as I said above I succesfully get data from my code but I do not get the correct values.正如我上面所说,我成功地从我的代码中获取了数据,但我没有得到正确的值。

The RXCharacteristic has CanWrite = false, CanRead = false, CanUpdate = true

If I use this app:如果我使用这个应用程序:

https://github.com/adafruit/Bluefruit_LE_Connect_v2 https://github.com/adafruit/Bluefruit_LE_Connect_v2

That is open source (coded with swift) I can succesfully get the correct value那是开源的(用swift编码)我可以成功获得正确的值

The github documentation for this library indicates that the Value is a byte array 这个库github 文档表明 Value 是一个字节数组

public override byte[] Value => _nativeCharacteristic.Value?.ToArray();

Which would mean that instead of using a foreach loop over each byte you should attempt to use a text encoding instead 1这意味着不是在每个字节上使用 foreach 循环,您应该尝试使用文本编码而不是1

var result = e.Characteristic.Value;
var str = System.Text.Encoding.UTF8.GetString(result,0,result.Length);

1. Andy0708, Fri Jan 06 2017, Oksana, "How convert byte array to string [duplicate]", Jul 25 '12 at 16:39, https://stackoverflow.com/a/11654825/1026459 1. Andy0708,2017 年 1 月 6 日星期五,Oksana,“如何将字节数组转换为字符串 [重复]”,2012 年 7 月 25 日 16:39, https ://stackoverflow.com/a/11654825/1026459

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法将 xamarin.ios 应用程序部署到我的设备? - Why am I unable to deploy a xamarin.ios app to my device? 为什么我在运行 Xamarin 应用程序时收到 JINativeWrapper.g.cs 文件未找到错误? - Why am I getting JINativeWrapper.g.cs file not found error when running my Xamarin app? 为什么无法配对BLE设备? - Why can't I pair my BLE device? 为什么在反编译我的应用程序时出现错误? - Why am I getting an error while decompiling my app? 为什么我的圈子值为零? - Why am I getting a value of zero for my circle? 我的调试语句正确吗? 该类使用是否正确? 我目前没有收到错误,但也没有任何输出 - Are my debug statements correct? Is the class being used correctly? I am not currently getting errors but also am getting no output 为什么我在字节数组的开头有一个额外的字符(点或子弹点)? - Why am I getting an extra character (a dot or bullet point) at the beginning of my byte array? 为什么我无法从我的 Xamarin.Forms UWP 应用程序访问下载文件夹,得到 System.UnauthorizedAccessException? - Why can’t I access the DownloadsFolder from my Xamarin.Forms UWP application, getting a System.UnauthorizedAccessException? 为什么我的应用程序在从 Xamarin.Forms.GoogleMaps 启动 map 时关闭? - Why does my app closes when iniating map from Xamarin.Forms.GoogleMaps? 为什么我的 Xamarin.Forms 应用程序在从 Web API 将数据插入到 SQLite 数据库后崩溃? - Why does my Xamarin.Forms app crash after inserting data to a SQLite database from a web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM