将数据写入BLE设备并读取其响应抖动? [英] Write data to BLE device and read its response flutter?

查看:87
本文介绍了将数据写入BLE设备并读取其响应抖动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将命令写入BLE设备并接收数据时遇到问题.我正在将ASCII编码的字符串写入特征.当其时间将数据解码回时,就会出现问题.接收到的数据与iOS中接收到的数据一样,但是当我尝试解码时,它只是空白.我尝试通过UTF8解码器和ASCII解码器进行解码,但未获得结果.

这就是我发现设备的方式.

  @override无效的initState(){//TODO:实现initStatewidget.flutterBlue.scanResults.listen((List< ScanResult>结果){对于(ScanResult结果中的结果){_addDeviceTolist(result.device);}});widget.flutterBlue.startScan();} 

连接设备:-

  void _connectDevice()异步{widget.flutterBlue.stopScan();打印(_deviceToConnect);尝试 {等待_deviceToConnect.connect();}抓住(e){if(e.code!='already_connected'){抛出e;}} 最后 {_services =等待_deviceToConnect.discoverServices();}_discoverDeviceServices();setState((){_connectedDevice = _deviceToConnect;});} 

发现服务:-

评论TAG-1&TAG-2行将打印值.我尝试了ASCII和UTF8解码.

在滚动浏览各种问题时,我发现要读取所有特征的值.

  void _discoverDeviceServices()异步{用于(_services中的BluetoothService服务){用于(service.characteristics中的BluetoothCharacteristic特性){var value =等待特性.read();打印(值);//print(AsciiDecoder().convert(value));/*** TAG-1 ***///print(utf8.decode(value));/*** TAG-2 ***/如果(characteristic.properties.write){如果(characteristic.properties.notify){_rx_Write_Characteristic =特性;_sendCommandToDevice();}}}}} 

将命令写入设备:-

  void _sendCommandToDevice()异步{最终命令="AT Z \ r";最后的convertCommand = AsciiEncoder().convert(command);等待_rx_Write_Characteristic.write(convertedCommand);_connectedDevice.disconnect();} 

请帮助我指出在读取写入BLE设备的数据的响应时出错了,以及如何精确地写入和读取值?

谢谢!

解决方案

可能是

13 \ r 转义表示回车符的字符

I am facing issues with writing the commands to BLE device and receiving the data. I am writing ASCII encoded string to the Characteristic. The problem arises when its time to decode the data back. Exact data is received as it's received in iOS but when I try to decode it just gets blank. I tried decoding via UTF8 decoder and ASCII decoder but no result was obtained.

This is how I discover the devices.

@override
  void initState() {
    // TODO: implement initState
    widget.flutterBlue.scanResults.listen((List<ScanResult> results) {
      for (ScanResult result in results) {
        _addDeviceTolist(result.device);
      }
    });
    widget.flutterBlue.startScan();
  }

Connect Device:-

void _connectDevice() async {
    widget.flutterBlue.stopScan();
    print(_deviceToConnect);
    try {
      await _deviceToConnect.connect();
    } catch (e) {
      if (e.code != 'already_connected') {
        throw e;
      }
    } finally {
      _services = await _deviceToConnect.discoverServices();
    }
    _discoverDeviceServices();
    setState(() {
      _connectedDevice = _deviceToConnect;
    });
  }

Discovering the services:-

When commenting TAG-1 & TAG-2 lines the values get printed. I tried both ASCII and UTF8 decoding.

While scrolling through various issues I found out to read values from all the characteristics.

void _discoverDeviceServices() async {
    for (BluetoothService service in _services){
      for (BluetoothCharacteristic characteristic in service.characteristics){
        var value = await characteristic.read();
        print(value);
        // print(AsciiDecoder().convert(value)); /*** TAG-1***/
        // print(utf8.decode(value)); /*** TAG-2***/
        if (characteristic.properties.write){
          if (characteristic.properties.notify){
            _rx_Write_Characteristic = characteristic;
            _sendCommandToDevice();
          }
        }
      }
    }
  }

Writing commands to device:-

void _sendCommandToDevice() async {
    final command = "AT Z\r";
    final convertedCommand = AsciiEncoder().convert(command);
    await _rx_Write_Characteristic.write(convertedCommand);
    _connectedDevice.disconnect();
  }

Please help me out pointing where I got wrong in reading the response of the data written to BLE device and how exactly writing and reading of values is to be done?

Thanks!

解决方案

It is probably String.fromCharCode you are looking for.

It seemed to give the right answer when I tested it with your values on https://dartpad.dev/

The 13s are \r escape characters that represent carriage return

这篇关于将数据写入BLE设备并读取其响应抖动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆