更新记录
1.0.5(2024-09-26)
增加读取数据readValue接口,增加扫描设备manifactureData数据返回。
1.0.4(2024-05-13)
优化升级插件代码。
1.0.3(2023-07-03)
增加扫描特征返回相关特征属性。
查看更多平台兼容性
uni-app
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | - | - | - | - | 5.0 | 12 | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | - | × | × | × | × |
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.0 | 12 | - | × |
BLE低功耗蓝牙UTS插件接口文档
1. 蓝牙UTS插件
<script>
//蓝牙UTS插件
import * as BLE from "@/uni_modules/Lizii-ble-uts";
export default {
}
</script>
2. 设置蓝牙回调
<script>
//蓝牙UTS插件
import * as BLE from "@/uni_modules/Lizii-ble-uts";
export default {
onLoad() {
//设置蓝牙回调
BLE.setDeviceCallback(function(res){
console.log(res);
if (res.type == "onBluetoothEnable") {
var enable = res.data;//蓝牙打开还是关闭对回调
} else if (res.type == "onDiscoveryDevice") {//发现蓝牙回调,需要这里选择蓝牙连接
var name = res.data.name;
var address = res.data.address;
} else if (res.type == "onConnected") {//连接成功回调
//发现服务
BLE.discoverServices(that.address);
} else if (res.type == "onDisconnected") {//连接断开回调
} else if (res.type == "onConnectFailed") {//连接失败回调
} else if (res.type == "onDiscoverServices") {//发现服务回调
//发现特征
BLE.discoverCharacteristics(that.address, that.serviceUUID);
} else if (res.type == "onDiscoverCharacteristics") {//发现特征回调
//设置通知特征
BLE.enableNotifyValue(that.address, that.serviceUUID, that.notifyCharacteristicUUID);
}
});
}
}
</script>
3. 设置数据回调
<script>
//蓝牙UTS插件
import * as BLE from "@/uni_modules/Lizii-ble-uts";
export default {
onLoad() {
//设置蓝牙回调
BLE.setDeviceCallback(function(res){
console.log(res);
});
//设置数据回调
BLE.setDataCallback(function(res){
console.log(res);
if (res.type == "onNotifyValue") {
var address = res.data.address;//蓝牙地址
var service = res.data.service;//服务
var characteristic = res.data.characteristic;//特征
var data = res.data.data;//数据,字节的大小范围是-128~127,小于0需要加上256
} else if (res.type == "onWriteValue") {
} else if (res.type == "onReadValue") {
}
});
}
}
</script>
4. 初始化(必须)
<script>
//蓝牙UTS插件
import * as BLE from "@/uni_modules/Lizii-ble-uts";
export default {
onLoad() {
//设置蓝牙回调
BLE.setDeviceCallback(function(res){
console.log(res);
});
//设置数据回调
BLE.setDataCallback(function(res){
console.log(res);
});
//初始化(必须)
BLE.initWithOption({
needBondWhenConnect:true,//设置设备连接之前是否需要先配对
mtuSize:256
});
}
}
</script>
5. 开始蓝牙扫描
startDiscovery() {
//检查权限并授权
BLE.checkPermissions(function(res){
console.log(res);
if (res.result == true) {
//开始蓝牙扫描
BLE.startDiscovery();
}
});
}
6. 停止蓝牙扫描
stopDiscovery() {
//停止蓝牙扫描
BLE.stopDiscovery();
}
7. 蓝牙连接
connect() {
//蓝牙连接
BLE.connect(this.address);
}
8. 断开连接
disconnect() {
//断开连接
BLE.disconnect(this.address);
}
9. 发现服务
//发现服务
BLE.discoverServices(that.address);
10. 发现特征
//发现特征
BLE.discoverCharacteristics(that.address, that.serviceUUID);
11. 设置通知特征
//设置通知特征
BLE.setNotifyValue(that.address, that.serviceUUID, that.notifyCharacteristicUUID, true);
12. 发送数据
//发送数据
var bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06];//字节的大小范围是-128~127,大于127需要减去256
console.log(bytes);
BLE.writeValue(this.address, this.serviceUUID, this.writeCharacteristicUUID, bytes);
13. 读数据
//读数据
BLE.readValue(this.address, this.serviceUUID, this.writeCharacteristicUUID);