{"id":18868414,"url":"https://github.com/best-flutter/flutter_wechat_ble","last_synced_at":"2025-04-14T14:31:52.511Z","repository":{"id":56830527,"uuid":"162439650","full_name":"best-flutter/flutter_wechat_ble","owner":"best-flutter","description":"ble 4.0 with wechat style api for flutter. flutter版微信api风格的低功耗蓝牙","archived":false,"fork":false,"pushed_at":"2020-03-04T10:25:25.000Z","size":790,"stargazers_count":54,"open_issues_count":6,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T03:51:12.989Z","etag":null,"topics":["ble","bluetooth-low-energy","blutooth","flutter","flutter-plugin"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/best-flutter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-19T13:15:03.000Z","updated_at":"2024-07-02T08:05:17.000Z","dependencies_parsed_at":"2022-09-02T05:51:01.219Z","dependency_job_id":null,"html_url":"https://github.com/best-flutter/flutter_wechat_ble","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fflutter_wechat_ble","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fflutter_wechat_ble/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fflutter_wechat_ble/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/best-flutter%2Fflutter_wechat_ble/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/best-flutter","download_url":"https://codeload.github.com/best-flutter/flutter_wechat_ble/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248897223,"owners_count":21179559,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ble","bluetooth-low-energy","blutooth","flutter","flutter-plugin"],"created_at":"2024-11-08T05:13:35.398Z","updated_at":"2025-04-14T14:31:51.426Z","avatar_url":"https://github.com/best-flutter.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# flutter_wechat_ble\n\n![](screen.png)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://pub.dartlang.org/packages/flutter_wechat_ble\"\u003e\n        \u003cimg src=\"https://img.shields.io/pub/v/flutter_wechat_ble.svg\" alt=\"pub package\" /\u003e\n    \u003c/a\u003e\n    \u003ca target=\"_blank\" href=\"https://shang.qq.com/wpa/qunwpa?idkey=a71a2504cda4cc9ace3320f2dc588bdae928abc671e903463caeb71ec9302c2c\"\u003e\u003cimg border=\"0\" src=\"https://pub.idqqimg.com/wpa/images/group.png\" alt=\"best-flutter\" title=\"best-flutter\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nA ble library with wechat api style for flutter.\n\n微信api风格的蓝牙库\n\n\n## 安装\n\n加入依赖:\n\n```\nflutter_wechat_ble: any\n```\n\n导入\n\n```\nimport 'package:flutter_wechat_ble/flutter_wechat_ble.dart';\n```\n\n## 使用\n\n参考下面的文档，因为语言不一样，所以在调用上略有区别:\n\nhttps://developers.weixin.qq.com/miniprogram/dev/api/wx.readBLECharacteristicValue.html\n\n\n#### 全局错误码\n\n在使用本库的时候，所有的错误都可以使用如下的形式来捕获\n\n\n```\ntry {\n  await FlutterWechatBle.someMethod();\n} on BleError catch (e) {\n   //// 这里捕获了BleError，表示蓝牙操作发生了异常\n   print(\"错误码 ${e.code} 错误信息 ${e.message}\");\n}\n```\n\n\n具体的错误码含义如下:\n\n```\nString get message {\n    switch (code) {\n      case \"10000\":\n        return \"openBluetoothAdapter not called yet!\";\n      case \"10001\":\n        return \"Bluetooth is not opened!\";\n      case \"10002\":\n        return \"Cannot find device id \";\n      case \"10003\":\n        return \"Connection fail\";\n      case \"10004\":\n        return \"Cannot find service\";\n      case \"10005\":\n        return \"CHARACTERISTIC  not found\";\n      case \"10006\":\n        return \"No connection found\";\n      case \"10007\":\n        return \"Property not support\";\n      case \"10008\":\n        return \"System error!\";\n    }\n  }\n```\n\n这里的错误码定义与微信小程序的错误定义一致，也可以参考这里:\nhttps://developers.weixin.qq.com/miniprogram/dev/api/wx.readBLECharacteristicValue.html\n\n\n#### 集成:\n\nios需要注意的是在info.plist中增加权限:\n\n```\n\u003ckey\u003eNSBluetoothPeripheralUsageDescription\u003c/key\u003e\n\u003cstring\u003e请求访问蓝牙\u003c/string\u003e\n```\n\n### 查看蓝牙是否可用\n\n```\nstatic FlutterWechatBle.getBluetoothAdapterState()\n```\n\n\n#### 打开蓝牙\n\n```\nawait FlutterWechatBle.openBluetoothAdapter();\n```\n\n注意本库并不对权限做出检查。\n\n#### 关闭蓝牙\n\n```\nawait FlutterWechatBle.closeBluetoothAdapter();\n```\n\n#### 扫描外设:\n\n```\nawait FlutterWechatBle.startBluetoothDevicesDiscovery();\n```\n\n\n#### 监听外设扫描结果\n\n```\n FlutterWechatBle.onBluetoothDeviceFound((BleDevice device){\n    //找到了一个外设\n });\n```\n\nBleDevice为扫描的结果，包含一个设备的描述信息\n\n```\n  /// uuid of the device\n  final String deviceId;\n\n  /// device name\n  final String name;\n\n  /// RSSI\n  final String RSSI;\n```\n\n\n如果不想监听，则调用\n\n```\nFlutterWechatBle.onBluetoothDeviceFound(null)\n```\n将监听函数设置null即可\n\n\n\n#### 获取所有发现的设备\n\n发现的设备将在再次调用startBluetoothDevicesDiscovery后被清空\n\n```\nawait FlutterWechatBle.getBluetoothDevices();\n```\n\n#### 获取所有已经连接的设备\n\n```\nawait FlutterWechatBle.getConnectedBluetoothDevices();\n```\n\n\n#### 停止扫描外设\n\n```\nawait FlutterWechatBle.stopBluetoothDevicesDiscovery();\n\n```\n\n\n#### 连接外设\n\n```\nawait FlutterWechatBle.createBLEConnection(deviceId: device.deviceId)\n```\n\n这里的deviceId为上面的监听函数结果BleDevice的一个属性\n\n#### 断开外设\n\n```\nawait FlutterWechatBle.closeBLEConnection(deviceId: device.deviceId);\n```\n\n#### 监听外设连接状态\n\n\n```\n FlutterWechatBle.onBLEConnectionStateChange((String deviceId,bool connected){\n\n });\n```\n\n停止监听外设连接状态:\n```\nFlutterWechatBle.onBLEConnectionStateChange(null);\n```\n\n#### 获取服务\n\n```\nList\u003cBleService\u003e services = await FlutterWechatBle.getBLEDeviceServices(deviceId: device.deviceId);\n```\n\nBleService的属性:\n```\n /// uuid of the service\n  final String uuid;\n\n  /// always true in android and the `isPrimary` field of the class `CBService` in ios\n  final bool isPrimary;\n```\n\n\n#### 获取服务的特征\n\n```\n Future\u003cList\u003cBleCharacteristic\u003e\u003e characteristics = await FlutterWechatBle.getBLEDeviceCharacteristics(\n                                                                   deviceId: device.deviceId, serviceId: service.uuid));\n```\n参数分别为:deviceId和serviceId,\n\nserviceId为上面BleService的字段uuid\n\nBleCharacteristic的属性:\n\n\n```\n  /// uuid of the characteristic\n  final String uuid;\n\n  /// support read\n  final bool read;\n\n  /// support write\n  final bool write;\n\n  /// support notify\n  final bool notify;\n\n  /// support indicate\n  final bool indicate;\n```\n\n与小程序略有区别，将属性直接放在BleCharacteristic这个类下面，使用更加方便\n\n\n#### 修改notify值\n\n```\n\nawait FlutterWechatBle.notifyBLECharacteristicValueChange(\n              deviceId: device.deviceId,\n              serviceId: service.uuid,\n              characteristicId: characteristic.uuid,\n              state: true)\n\n```\n\n注意这个方法只能用于支持notify的特征调用。\n\n\n#### 写入值\n\n```\n await FlutterWechatBle.writeBLECharacteristicValue(\n        deviceId: device.deviceId,\n        serviceId: service.uuid,\n        characteristicId: characteristic.uuid,\n        value: 写入值的十六进制字符串或者十六进制数组);\n```\n\n这里的value可以为List\u003cint\u003e 或者 十六进制字符串,如果对这些概念不熟悉，可以参考这里：\n\nhttps://baike.baidu.com/item/%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6/4162457?fr=aladdin\n\n\n#### 读取值\n\n```\nBleValue value =  await FlutterWechatBle.readBLECharacteristicValue(\n                       deviceId: device.deviceId,\n                       serviceId: service.uuid,\n                       characteristicId: characteristic.uuid\n                     )\n```\n\nBleValue的属性:\n\n```\n final String deviceId;\n  final String serviceId;\n  final String characteristicId;\n  final String value;\n```\n\n这里的value为十六进制字符串，如果要得到十六进制数组,则可以使用\n\n```\nvalue.bytes\n```\n\n\n#### 监听值改变\n\n```\n FlutterWechatBle.onBLECharacteristicValueChange((BleValue value){\n\n });\n```\n\n如果需要停止监听:\n\n```\nFlutterWechatBle.onBLECharacteristicValueChange(null)\n```\n\n设置为null即可\n\n\n\n\n## 加入BluetoothService封装一问一答式蓝牙设备的操作\n\n### 介绍\n\n一问一答式的通信方式指的是将蓝牙设备当成一台服务器来看，当app发送一个设备支持的指令给设备的时候，设备将会通过通知的方式返回一个响应。\n这个过程，对于大部分蓝牙设备都是通用的。\n\n其中的一问一答式，我们可以将之封装起来，通过设备配置类，封装数据包的组装和处理的方式，大大减少实际的开发工作量。\n\n目前在代码中放了一个实际的设备,具体可以看这里:\n\nhttps://github.com/best-flutter/flutter_wechat_ble/blob/master/example/lib/screens/connect_to_tkb.dart\n\nhttps://github.com/best-flutter/flutter_wechat_ble/blob/master/example/lib/services/TkbDeviceConfig.dart\n\n其中将搜索设备、连接设备、发现服务、发现特征、设置通知、写入和获取数据统一封装起来。将这些复杂的操作转成标准的流程，\n并且可以支持连接多种设备，管理多个设备的连接。并将通知型的消息获取过程，改造成发送消息后可以直接使用await等待数据返回，\n将原来的非线性流程改成如丝滑般柔顺的线性流程。\n\n下面的代码演示:\n\n```\nstatic DeviceConfig config = new TbkDeviceConfig();\n  static BluetoothService bluetoothService =\n      new BluetoothService(configs: [config]);\n\n\n@override\n  void initState() {\n    super.initState();\n\n    this.startup();\n  }\n\n  void startup() async {\n    await setState(() {\n      messages.add(\"searing devices...\");\n    });\n    await bluetoothService.shutdown();\n    bluetoothService.onServiceDeviceFound(onServiceDeviceFound);\n    await bluetoothService.startScan();\n  }\n\n  void onServiceDeviceFound(BluetoothServiceDevice device) async {\n    print(\"device ${device.device} ${device.name}\");\n    await setState(() {\n      messages.add(\"startup devices with name TKB_BLE...${device.name}\");\n    });\n    try {\n      await bluetoothService.stopScan();\n      await bluetoothService.startupDevice(device.deviceId);\n\n      await setState(() {\n        messages.add(\"writing data : 000062\");\n      });\n      print(\"write data\");\n      HexValue value = await device.write(\"000062\");\n      print(\"write data success\");\n      await setState(() {\n        messages.add(\"writing data success, response: ${value.string}\");\n      });\n      print(\"=================\" + value.string);\n    } on BleError catch (e) {\n      print(\n          \"\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e ${e.code} ${e.message}\");\n      setState(() {\n        messages.add(\"Ble error : ${e.code} ${e.message}\");\n      });\n    } catch (e) {\n      print(\"\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e $e\");\n      setState(() {\n        messages.add(\"other error : ${e.code} ${e.message}\");\n      });\n    }\n  }\n\n\n```\n\n\n\n\n\n## Example\n\n\u003cimg src=\"images/1.jpg\" width=\"240\"\u003e  \u003cimg src=\"images/2.jpg\" width=\"240\"\u003e  \u003cimg src=\"images/3.jpg\" width=\"240\"\u003e\n\n\u003cimg src=\"images/4.jpg\" width=\"240\"/\u003e\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbest-flutter%2Fflutter_wechat_ble","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbest-flutter%2Fflutter_wechat_ble","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbest-flutter%2Fflutter_wechat_ble/lists"}