flutter_wechat_ble 0.0.2

flutter_wechat_ble #

pub package best-flutter

A ble library with wechat api style for flutter.

微信api风格的蓝牙库

安装 #

加入依赖:

flutter_wechat_ble: any

导入

import 'package:flutter_wechat_ble/flutter_wechat_ble.dart';

使用 #

参考下面的文档,因为语言不一样,所以在调用上略有区别:

https://developers.weixin.qq.com/miniprogram/dev/api/wx.readBLECharacteristicValue.html

全局错误码 #

在使用本库的时候,所有的错误都可以使用如下的形式来捕获

try {
  await FlutterWechatBle.someMethod();
} on BleError catch (e) {
   //// 这里捕获了BleError,表示蓝牙操作发生了异常
   print("错误码 ${e.code} 错误信息 ${e.message}");
}

具体的错误码含义如下:

String get message {
    switch (code) {
      case "10000":
        return "openBluetoothAdapter not called yet!";
      case "10001":
        return "Bluetooth is not opened!";
      case "10002":
        return "Cannot find device id ";
      case "10003":
        return "Connection fail";
      case "10004":
        return "Cannot find service";
      case "10005":
        return "CHARACTERISTIC  not found";
      case "10006":
        return "No connection found";
      case "10007":
        return "Property not support";
      case "10008":
        return "System error!";
    }
  }

这里的错误码定义与微信小程序的错误定义一致,也可以参考这里: https://developers.weixin.qq.com/miniprogram/dev/api/wx.readBLECharacteristicValue.html

集成: #

ios需要注意的是在info.plist中增加权限:

<key>NSBluetoothPeripheralUsageDescription</key>
<string>请求访问蓝牙</string>

打开蓝牙 #

await FlutterWechatBle.openBluetoothAdapter();

注意本库并不对权限做出检查。

关闭蓝牙 #

await FlutterWechatBle.closeBluetoothAdapter();

扫描外设: #

await FlutterWechatBle.startBluetoothDevicesDiscovery();

监听外设扫描结果 #

 FlutterWechatBle.onBluetoothDeviceFound((BleDevice device){
    //找到了一个外设
 });

BleDevice为扫描的结果,包含一个设备的描述信息

  /// uuid of the device
  final String deviceId;

  /// device name
  final String name;

  /// RSSI
  final String RSSI;

如果不想监听,则调用

FlutterWechatBle.onBluetoothDeviceFound(null)

将监听函数设置null即可

停止扫描外设 #

await FlutterWechatBle.stopBluetoothDevicesDiscovery();

连接外设 #

await FlutterWechatBle.createBLEConnection(deviceId: device.deviceId)

这里的deviceId为上面的监听函数结果BleDevice的一个属性

断开外设 #

await FlutterWechatBle.closeBLEConnection(deviceId: device.deviceId);

监听外设连接状态 #

 FlutterWechatBle.onBLEConnectionStateChange((String deviceId,bool connected){

 });

停止监听外设连接状态:

FlutterWechatBle.onBLEConnectionStateChange(null);

获取服务 #

List<BleService> services = await FlutterWechatBle.getBLEDeviceServices(deviceId: device.deviceId);

BleService的属性:

 /// uuid of the service
  final String uuid;

  /// always true in android and the `isPrimary` field of the class `CBService` in ios
  final bool isPrimary;

获取服务的特征 #

 Future<List<BleCharacteristic>> characteristics = await FlutterWechatBle.getBLEDeviceCharacteristics(
                                                                   deviceId: device.deviceId, serviceId: service.uuid));

参数分别为:deviceId和serviceId,

serviceId为上面BleService的字段uuid

BleCharacteristic的属性:

  /// uuid of the characteristic
  final String uuid;

  /// support read
  final bool read;

  /// support write
  final bool write;

  /// support notify
  final bool notify;

  /// support indicate
  final bool indicate;

与小程序略有区别,将属性直接放在BleCharacteristic这个类下面,使用更加方便

修改notify值 #


await FlutterWechatBle.notifyBLECharacteristicValueChange(
              deviceId: device.deviceId,
              serviceId: service.uuid,
              characteristicId: characteristic.uuid,
              state: true)

注意这个方法只能用于支持notify的特征调用。

写入值 #

 await FlutterWechatBle.writeBLECharacteristicValue(
        deviceId: device.deviceId,
        serviceId: service.uuid,
        characteristicId: characteristic.uuid,
        value: 写入值的十六进制字符串或者十六进制数组);

这里的value可以为List

https://baike.baidu.com/item/%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6/4162457?fr=aladdin

读取值 #

BleValue value =  await FlutterWechatBle.readBLECharacteristicValue(
                       deviceId: device.deviceId,
                       serviceId: service.uuid,
                       characteristicId: characteristic.uuid
                     )

BleValue的属性:

 final String deviceId;
  final String serviceId;
  final String characteristicId;
  final String value;

这里的value为十六进制字符串,如果要得到十六进制数组,则可以使用

value.bytes

监听值改变 #

 FlutterWechatBle.onBLECharacteristicValueChange((BleValue value){

 });

如果需要停止监听:

FlutterWechatBle.onBLECharacteristicValueChange(null)

设置为null即可

Example #

0.0.2 #

format code

0.0.1 #

android and ios support

example/README.md

flutter_wechat_ble_example #

Demonstrates how to use the flutter_wechat_ble plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_wechat_ble: ^0.0.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_wechat_ble/flutter_wechat_ble.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
32
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
76
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/utils.dart. (-3.45 points)

Analysis of lib/utils.dart reported 7 hints, including:

line 2 col 14: Name non-constant identifiers using lowerCamelCase.

line 3 col 14: Name non-constant identifiers using lowerCamelCase.

line 4 col 14: Name non-constant identifiers using lowerCamelCase.

line 5 col 14: Name non-constant identifiers using lowerCamelCase.

line 6 col 14: Name non-constant identifiers using lowerCamelCase.

Fix lib/flutter_wechat_ble.dart. (-2.48 points)

Analysis of lib/flutter_wechat_ble.dart reported 5 hints:

line 6 col 1: Prefer using /// for doc comments.

line 23 col 3: This function has a return type of 'String', but doesn't end with a return statement.

line 55 col 16: Name non-constant identifiers using lowerCamelCase.

line 59 col 45: Name non-constant identifiers using lowerCamelCase.

line 177 col 10: This function has a return type of 'Future', but doesn't end with a return statement.

Maintenance suggestions

The package description is too short. (-14 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8