1.0.12 • Published 6 years ago

wx-ble v1.0.12

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

wx-bluetooth

可能是Github上迄今为止最适合前端最易用的微信小程序蓝牙实现。 iOS bluetooth_run_screenshot

Features

  • 简单强大的API
  • 使用状态机自动管理状态
  • 支持自动修复连接重新连接
  • 支持connect timeout 连接超时机制
  • 支持初始化多个蓝牙实例
  • 支持found,connect,notify,timout,fail回调
  • 支持wx.method.paramters.success方法的promisify

Usage

  • npm:

    npm install wx-ble

    then

    import Bluetooth from 'wx-ble'
  • 直接下载项目后,在页面引入。建议将该工具方法放在 utils or vendors 目录下,并新建名为bluetooth的目录后,将文件下载到该目录中:

    cd ProjectPath/utils
    git clone https://github.com/vuchan/wx-bluetooth.git

    then

    import Bluebooth from 'yourRelativePath/bluetooth/index.js'

Example

注: 如果keepAlive配置项为true的话,需要手动在 sendData 方法成功then后写上 return this.trigger('success', true) 以触发 finish

const bluebooth = new Bluetooth({   // configOptions 参考下方的API
  debug: false,
  timeout: 5,
  keepAlive: true,
  // 必须配置 `connectOptions` 中的 `deviceName` 和 `services` 以匹配你想匹配的蓝牙设备
  connectOptions: {
    interval: 0,
    services: [''], // your device services array
    allowDuplicatesKey: false,
    deviceName: '', // device name
    // characteristicId: ''
  },
  onConnect: function () {
    // 如果 keepAlive 为`true`的话,需要自己手动在 sendData 成功后执行 `return this.trigger('success', true)` 以触发 `finish` 状态以进入关闭蓝牙连接和蓝牙适配器操作
    this.sendData('01').then(res => this.sendData('02')).then(res => this.sendData('03')).then(res => this.trigger('success'))
  }
})

实现

通过triggerCommands触发success or failure进入成功或失败状态。

  1. 初始化-init
  2. 搜索-search
  3. 连接-connect
  4. 通信-transfer
  5. 结束-finish

API

config 配置项:

option nametypeparameterdefault valuedescription
autoConnectBooleanfalse初始化实例时完成后自动根据 connenctOptions 连接蓝牙,否者手动调用实例的 start 方法
debugBooleantrue打开console,debug程序内部状态的变化
timeoutNumberfalses(秒)为单位。在蓝牙的连接过程中,若在该 timeout 秒时间内无法连接,则进入 timeout 回调。如果为0 or false,则关闭该项。
keepAliveBooleanfalse保持蓝牙通讯的连接
autoFixConnectBooleantrue蓝牙断开后,自动修复蓝牙连接
maxReconnectTimesNumber5最大重连次数
onFoundFunctiondevices[]当蓝牙发现新设备时,接收第一个参数 devices 获取所有设备
onConnectFunction连接成功后的回调,进行传输数据等操作
onNotifyFunctionvalue收到蓝牙传输过来的值的回调,通过参数value查看该值
onTimeoutFunctionerror连接超时的回调函数(连接超时函数若不自定义会自动进入onFail函数)
onFailFunctionerror再重连maxReconnectTimes后,会调用连接失败后的回调
connectOptionsObject见下面connectOptions APIconnectOptions 是一个对象,用来设置连接蓝牙的配置项蓝牙是否能够连接,跟此配置项有莫大关系

connectOptions 配置项:

property nametypedefault valuedescripiondetails
intervalNumber0上报设备的间隔,默认为0,意思是找到新设备立即上报,否则根据传入的间隔上报更多
servicesArray[]蓝牙设备主 service 的 uuid 列表更多
allowDuplicatesKeyBooleanfalse是否允许重复上报同一设备, 如果允许重复上报,则onDeviceFound 方法会多次上报同一设备,但是 RSSI 值会有不同更多
deviceNameString''通过该项匹配当前设备,如果设备名称包含该字段,则认为匹配
characteristicIdString''该项目前未使用

实例方法

method nameparameterdefault valuereturndescription
start手动开启该实例对象的蓝牙连接
sendDatadata''{Promise}向已连接的蓝牙设备发送数据(该方法必须在蓝牙连接成功后调用)
resetState清空和重置内部状态,关闭蓝牙连接,如果不再存在蓝牙实例,则关闭蓝牙适配器

TODOs

  1. timeout
  2. 欢迎issue
  3. 欢迎pull/request
  4. 如果API有不甚清晰的地方,请issue
  5. If it is useful for u, please give me a star to make me has more passion to update it, Thanks!

ChangeLog

2018-1-30

  1. finished timeout logic.
  2. fixed resetState function.

2018-2-2

  1. Modified the code of example.

2018-2-5

  1. Updated the TODOs of this doc.
  2. Mofified API doc.

2018-2-26

  1. add a new config option autoConnect
  2. add new callback function onFound

2018-4-08

  1. fixed the timeout method will be auto set off at init state bluetoothStateHandler function.
  2. add a fallback feature to the start function.

License

MIT

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago