我正在尝试制作一个包含向设备发送字符串的应用程序,我已经在本地android中对此进行了编码,但是Flutter_blue似乎提供了仅将列表写入设备的功能

这是我用于演示的代码

 _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,如果有人可以请帮助
 _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();

}

最佳答案

解决了

import 'dart:convert';
await bluetoothCharacteristic.write(utf8.encode(str));//str is the string u need
      to send

代替

等待bluetoothCharacteristic.write(数据);

关于android - 使用Flutter Blue发送字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58061686/

10-10 02:06