ty-wxble
v1.0.22
Published
微信小程序蓝牙功能封装库
Maintainers
Readme
ty-wxble
Usage
npm:
npm i ty-wxblethen
import TyWxBle from 'TyWxBle'
Example
const tyBluebooth = new TyWxBle({ // configOptions 参考下方的API
debug: false,
services: [''], // your device services array
deviceName: '19ECM001-20801C', // device name
maxReconnectAttempts: 5, // 重连的次数,一秒一次
// 必须配置 `connectOptions` 中的 `deviceName` 和 `services` 以匹配你想匹配的蓝牙设备
// 运动状态的回调
onMotionStatus: function(res) {
const {duration,distance,calories,steps} = res;
},
// 设备状态的回调
onDeviceStatus: function(res) {
const {machineStatus,machineStatusText,speed,gear,rpm,heartRate,power,slope} = res;
}
})
tyBluebooth.start();
// resetState
tyBluebooth.resetState().then(res => {
const {success: true, message: ''} = res;
}).catch(err => {
})API
config 配置项:
| option name | type | parameter | default value | description |
| --------- | :------: | :---------: | :------------ | :---------- |
| debug | Boolean | | true | 打开console,debug程序内部状态的变化 |
| maxReconnectAttempts | Number | | 5 | 蓝牙断开后重连次数;一秒一次 |
| deviceName | String | '' | 通过该项匹配当前设备,如果设备名称包含该字段,则认为匹配 |
| services | Array | [] | 蓝牙设备主 service 的 uuid 列表 | |
| onFound | Function | devices | [] | 当蓝牙发现新设备时,接收第一个参数 devices 获取所有设备 |
| onConnect | Function | | | 连接成功后的回调,进行传输数据等操作 |
| onStateChanged | Function | | | 连接状态变化的回调函数 连接的状态值"disconnected","connecting","connected","disconnecting","error", |
| onNotify | Function | value | | 收到蓝牙传输过来的值的回调,通过参数value查看该值 |
| onFail | Function | error | | 再重连maxReconnectTimes;maxReconnectAttempts后,会调用连接失败后的回调 |
| onMotionStatus | Function | error | | 获取运动数据的回调 |
| onDeviceStatus | Function | error | | 获取设备状态的回调 |
获取设备、运动状态 是通过回调实时获取 具体信息如下
| options name | type | parameter | default value | description | return |
| --------- | :------: | :---------: | :------------ | :---------- | :---------- |
| onMotionStatus | Function | | | 获取运动数据 | duration - 运动时长(秒)、distance - 运动里程(米)、calories - 消耗热量(卡)、steps - 步数|
| onDeviceStatus | Function | | | 获取设备状态 | status {machineStatus表示设备状态} 1:表示关机、2:停止、3:启动); machineStatusText 是设备状态文案包括(关机、停止、启动) speed {number} 当前速度(km/h);rpm {number} 转速(转/分钟); gear {string} 档位(如 "1" ~ "12");power {number} 实时功率(瓦);incline {number} 坡度(百分比,如 15 表示 15%);mode {string} 模式(0-12); modeText {string} 模式(如 "HA", "P1~P12")`; |
example:
实例方法
| method name | parameter | default value | return | description |
| ----------- | :---------: | :-----------: | :--------: | :---------- |
| start | | | | 手动开启该实例对象的蓝牙连接 |
| sendData | data | '' | {Promise} | 向已连接的蓝牙设备发送数据(该方法必须在蓝牙连接成功后调用) |
| resetState| | | {Promise} | 清空和重置内部状态,关闭蓝牙连接,如果不再存在蓝牙实例,则关闭蓝牙适配器, Promise 的返回值{success: true or false, message: ''} success 为true 则表示内部重置流程结束,可以做其他操作,如start|
设备控制操作文档
| options name | type | parameter | default value | description |
| --------- | :------: | :---------: | :------------ | :---------- |
| powerOn | Function | | | 设备开机 |
| powerOff | Function | | | 设备关机 |
| startUp | Function | | | 设备启动 |
| stop | Function | | | 设备停止 |
| turn | Function | | forward | 设备转向 |
| speedUp | Function | | | 增加速度 |
| speedDown | Function | | | 减少速度 |
| switchMode | Function | | | 模式切换 |
| toggleIndicator | Function | | | 指示灯切换 |
