android - 使用 Flutter Blue 发送字符串

标签 android ios flutter

我正在尝试制作一个 flutter 应用程序,其中包括向设备发送字符串,我已经在原生 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 

代替

等待蓝牙特性.write(数据);

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

相关文章:

java - 让Java中的方法返回在内部类中初始化的变量

firebase - E/StorageUtil(6584) : error getting token java. util.concurrent.ExecutionException : b. a.d.p.d.a:请先登录,然后再尝试获取 token

flutter - flutter如何让textfield动态显示多行?

android - 更新到 Android Studio 4.1 时出现 java.lang.AssertionError

android - 除了 "Hello"的东西,还有什么好的 Phonegap 教程吗?

java - ArrayList set(position,object) 方法 RecyclerView 中的 IndexOutOfBound 异常

ios - 错误:执行 gem ... (Errno::EPERM) 操作不允许 -/usr/bin/pod in Xcode 8 swift 3

iphone - IOS中如何将数据从一个 View 传递到另一个 View Controller ?

ios - 设置 UIButton 的 backgroundImage 时不调整大小,但定位

flutter - 返回页面时记住底部导航栏的状态