首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Flutter Blue发送字符串

使用Flutter Blue发送字符串
EN

Stack Overflow用户
提问于 2019-09-23 19:40:32
回答 1查看 3.6K关注 0票数 1

我正在尝试在flutter中制作一个应用程序,其中包括向设备发送字符串,我已经在原生安卓中进行了编码,但Flutter_blue似乎提供了将列表写入设备的功能

下面是我用于演示的代码

代码语言:javascript
复制
 _discoverServices(BluetoothDevice device) async {
await device.connect();
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
  print("${service.uuid}");
  List<BluetoothCharacteristic> blueChar = service.characteristics;
  blueChar.forEach((f){
    print("Characteristice =  ${f.uuid}");
    if(f.uuid.toString().compareTo("00000052-0000-1000-8000-00805f9b34fb")==0)
      {
        bluetoothCharacteristic = f;
        print(true);
      }

  });




});

bluetoothCharacteristic.write([0x11]);//this needs to be a string rather than an int Array

}

这是我到目前为止已经尝试过的,但它需要进一步的发展,因为我不能映射像38这样的双位数字,如果有人可以帮助这一点

代码语言:javascript
复制
 _discoverServices(BluetoothDevice device) async {
await device.connect();
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
  print("${service.uuid}");
  List<BluetoothCharacteristic> blueChar = service.characteristics;
  blueChar.forEach((f){
    print("Characteristice =  ${f.uuid}");
    if(f.uuid.toString().compareTo("00000052-0000-1000-8000-00805f9b34fb")==0)
      {
        bluetoothCharacteristic = f;
        print(true);
      }

  });

});
String str = "av,38,STALL,~";
for(int i =0;i<=str.length-1;i++)
  {
    print(str[i]);
    charData.add(str[i]);

  }
print(charData);

var alphaMap = {
  "0":48,
  "1":49,
  "2":50,
  "3":51,
  "4":52,
  "5":53,
  "6":54,
  "7":55,
  "8":56,
  "9":57,
  "a": 0x61,
  "b": 0x62,
  "c":0x63,
  "d":0x64,
  "e":0x65,
  "f":0x66,
  "g":0x67,
  "h":0x68,
  "i":0x69,
  "j":0x6a,
  "k":0x6b,
  "l":0x6c,
  "m":0x6d,
  "n":0x6e,
  "o":0x6f,
  "p":0x70,
  "q":0x71,
  "r":0x72,
  "s":0x73,
  "t":0x74,
  "u":0x75,
  "v":0x76,
  "w":0x77,
  "x":0x78,
  "y":0x79,
  "z":0x7a,
  ",":0x2c,
  "A":0x41,
  "B":0x42,
  "C":0x43,
  "D":0x44,
  "E":0x45,
  "F":0x46,
  "G":0x47,
  "H":0x48,
  "I":0x49,
  "J":0x4a,
  "K":0x4b,
  "L":0x4c,
  "M":0x4d,
  "N":0x4e,
  "O":0x4f,
  "P":0x50,
  "Q":0x51,
  "R":0x52,
  "S":0x53,
  "T":0x54,
  "U":0x55,
  "V":0x56,
  "W":0x57,
  "X":0x58,
  "Y":0x59,
  "Z":0x5a,
  "~":0x7e,
};

for(String s in charData)
  {
    data.add(alphaMap[s]);
  }

print(data);

await bluetoothCharacteristic.write(data);


device.disconnect();

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-27 17:33:43

解决方式

代码语言:javascript
复制
import 'dart:convert';
await bluetoothCharacteristic.write(utf8.encode(str));//str is the string u need 
      to send 

而不是

等待bluetoothCharacteristic.write(数据);

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58061686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档