初始化模块
原生模块使用方式(uniapp 已弃用云端插件,本地仍可使用)
// 导入模块
var vinBluetooth = uni.requireNativePlugin("VinBluetooth");
// 模块初始化
vinBluetooth.init({},(res)=>{
// res.code:代表结果 0成功
// res.msg:回调结果信息
// res.data:不同api,不同数据
});
UTS模块使用方式
// 导入模块
import * as vinBluetooth from '@/uni_modules/vin-bluetooth'
// 模块初始化
vinBluetooth.init({},(res)=>{
// res.code:代表结果 0成功
// res.msg:回调结果信息
// res.data:不同api,不同数据
});
API列表
注:回调都会包含一个对象code:代表结果 0成功
msg:回调结果信息
data:不同api,不同数据
- 初始化蓝牙模块
vinBluetooth.init({
// 可选参数 是否在扫描时过滤重复的设备(重复设备将不在通过回调传输)默认:true 可选参数
isFilterScanRepeat:true
},(res)=>{
console.log(res)
});
- 释放蓝牙模块 (在程序退出,或用不到模块时)
vinBluetooth.destroy((res)=>{
console.log(res)
});
- 打开蓝牙适配器
vinBluetooth.open((res)=>{
console.log(res)
});
- 关闭蓝牙适配器
vinBluetooth.close((res)=>{
console.log(res)
});
- 开始扫描经典蓝牙设备
vinBluetooth.startScan({
// 可选参数 超时时间,单位毫秒 默认:10000
timeout:10000
},(res)=>{
console.log(res)
});
- 停止扫描经典蓝牙设备
vinBluetooth.stopScan((res)=>{
console.log(res)
});
- 获取已配对的经典蓝牙设备
vinBluetooth.bondedDevices((res)=>{
console.log(res)
// res.data 为设备信息列表
});
- 配对经典蓝牙设备
vinBluetooth.pairDevice("设备地址",(res)=>{
console.log(res)
});
- 取消配对经典蓝牙设备
vinBluetooth.unPairDevice("设备地址",(res)=>{
console.log(res)
});
- 开始扫描低功耗蓝牙设备
vinBluetooth.startScanBle({
// 可选参数 超时时间,单位毫秒 默认:10000
timeout:10000
},(res)=>{
console.log(res)
});
- 停止扫描低功耗蓝牙设备
vinBluetooth.stopScanBle((res)=>{
console.log(res)
});
- 连接低功耗蓝牙设备
vinBluetooth.connectBle({
// 必填参数 设备地址
address:""
},(res)=>{
console.log(res)
});
- 断开低功耗蓝牙设备
vinBluetooth.disconnectBle("设备地址",(res)=>{
console.log(res)
});
- 获取已连接的低功耗蓝牙设备
vinBluetooth.connectingBleDevices((res)=>{
console.log(res)
// res.data 为设备信息列表
});
- 设置低功耗蓝牙设备的MTU
vinBluetooth.setMtu("设备地址","mtu值",(res)=>{
console.log(res)
});
- 发送字节数据到低功耗蓝牙设备
vinBluetooth.sendBleByteData("设备地址","服务uuid","特征uuid","字节数组 number[]",(res)=>{
console.log(res)
});
- 发送base64数据到低功耗蓝牙设备
vinBluetooth.sendBleByteData("设备地址","服务uuid","特征uuid","base64数据",(res)=>{
console.log(res)
});
- 发送16进制数据到低功耗蓝牙设备
vinBluetooth.sendBleByteData("设备地址","服务uuid","特征uuid","16进制(hex)数据",(res)=>{
console.log(res)
});
- 发送数据到低功耗蓝牙设备
vinBluetooth.sendBleByteData({
// 设备地址
address:"",
// 服务uuid
serviceUuid:"",
// 特征uuid
characteristicUuid:"",
// 发送的类型 0:base64 1:16进制 2:字节数组
type:0,
// 发送的数据
data:any
},(res)=>{
console.log(res)
});
- 监听低功耗蓝牙设备的数据
vinBluetooth.listenBleData({
// 设备地址
address:"",
// 服务uuid
serviceUuid:"",
// 特征uuid
characteristicUuid:"",
// 可选参数 通信uuid,默认取特征下的第一个
descriptorUuid:""
},(res)=>{
console.log(res)
});
- 注册蓝牙模块通知事件
vinBluetooth.register("事件名称",(res)=>{
console.log(res)
},(res)=>{});
// 参数2:注册的事件回调
// 参数3:注册结果,成功|失败
可注册的事件包含:
- error:所有的异常错误,都将通过此事件通知
- opening:蓝牙正在打开事件
- open:蓝牙打开成功事件
- closing:蓝牙正在关闭事件
- close:蓝牙关闭成功事件
- startScan:开始扫描经典蓝牙事件
- scan:扫描到经典蓝牙事件,将通过此事件进行通知设备信息
- stopScan:停止扫描经典蓝牙事件
- bonding:经典蓝牙配对中事件
- bonded:经典蓝牙配对成功事件
- rejectBonded:经典蓝牙拒绝配对事件
- unBonded:经典蓝牙取消配对事件
- startBleScan:开始扫描Ble蓝牙事件
- bleScan:扫描到Ble蓝牙事件,将通过此事件进行通知设备信息
- stopBleScan:停止扫描Ble蓝牙事件
- bleConnecting:Ble蓝牙连接中事件
- bleConnect:Ble蓝牙连接成功事件
- bleDisconnecting:Ble蓝牙断开中事件
- bleDisconnect:Ble蓝牙断开连接成功事件
- bleMtuChanged:Ble蓝牙设置Mtu结果事件
- bleServicesDiscovered:Ble蓝牙服务发现事件
- bleReceiveData:Ble蓝牙接受数据事件
注:bleConnecting,bleConnect,bleDisconnecting,bleDisconnect,bleMtuChanged,bleServicesDiscovered,bleReceiveData事件均返回所有事件,如需订阅某设备的事件可在后面组品设备地址,
如:bleConnectingA4:90:CE:12:92:3D 注:如使用uts方式,需要使用 registerOtherEvent 来注册事件
- 取消注册蓝牙模块通知事件
vinBluetooth.unRegister("事件名称",(res)=>{
console.log(res)
});