RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
你可能遇到了下面的问题
关闭右侧工具栏
微信小程序蓝牙开发源码系列一
  • 作者:
  • 发表时间:2021-12-22 22:42
  • 来源:未知
第一步 打开蓝牙连接

openBluetoothAdapter() {
let _this = this;
// 初始化蓝牙模块
uni.showLoading({
title: '正在连接设备',
mask: true
});
uni.openBluetoothAdapter({
mode: 'central',
success: (res) => {
console.log('初始化蓝牙成功:' + res);
this.$data.isOpenBle = true;
console.log(this.$data.isOpenBle);
// 初始化完毕开始搜索
this.startBluetoothDeviceDiscovery()
 
},
fail: (e) => {
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
uni.showToast({
icon: "none",
title: "初始化蓝牙失败!",
mask: false,
duration: 3000
});
}
})
 
},




第二步
/**
* 监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接,设备丢失,连接异常断开等等
*/
onBLEConnectionStateChange() {
let count = 0;
uni.onBLEConnectionStateChange(res => {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`蓝牙连接状态 -------------------------->`);
console.log(JSON.stringify(res));
if (!res.connected) {
if (this.isStop) return;
console.log('断开低功耗蓝牙成功:');
 
uni.showToast({
icon: "none",
title: "蓝牙已经断开!",
mask: false,
duration: 3000
});
 
//在这里尝试重连
//this.createBLEConnection();
//关闭连接
this.closeBluetoothAdapter();
}
});
},


 
上一篇:mac配置node最佳方案     下一篇:没有了