项目中用到了蓝牙,只是通过计算设备间距离处理业务逻辑,传输数据可做扩展
整体流程:
1、 onload中初始化蓝牙设备uni.openBluetoothAdapter,调用成功后获取本机蓝牙适配器状态uni.getBluetoothAdapterState
2、 点击事件,开始搜索蓝牙设备uni.startBluetoothDevicesDiscovery,异步处理定位失败问题并停止搜索蓝牙设备 uni.stopBluetoothDevicesDiscovery
3、开始搜寻附近的蓝牙外围设备uni.startBluetoothDevicesDiscovery
4、监听寻找到新设备的事件uni.onBluetoothDeviceFound
5、获取在蓝牙模块生效期间所有已发现的蓝牙设备uni.getBluetoothDevices
6、对获取到的所有设备信息for循环筛选对应的设备
对应代码:
1、 初始化蓝牙设备和获取本机蓝牙适配器状态
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success: e => {
console.log('初始化蓝牙成功:' + e.errMsg);
console.log(JSON.stringify(e));
this.getBluetoothAdapterState();
},
fail: e => {
console.log(e)
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
uni.showModal({
title: '提示!',
content: '初始化蓝牙失败,请打开本机蓝牙!',
showCancel: false,
confirmText: "确定",
success: (res) => {
if (res.confirm) {
}
}
});
if (e.errCode !== 0) {
//initTypes(e.errCode, e.errMsg);
}
}
});
},
getBluetoothAdapterState() {
uni.getBluetoothAdapterState({
success: res => {
console.log(JSON.stringify(res));
},
fail: e => {
console.log('获取本机蓝牙适配器状态失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
//initTypes(e.errCode);
}
}
});
},
2、 点击事件,开始搜索蓝牙设备,异步处理定位失败问题并停止搜索蓝牙设备
prepareok: function() { // wxf 190907 去掉that
this.dogpailoadingtxt = "定位中,请稍后";
this.isblueok = false;
this.startBluetoothDevicesDiscovery(true);
this.bluetimestoper = setTimeout(() => {
if (!this.isblueok) {
this.togglePopup('');
uni.showModal({
title: '提示!',
content: '定位失败,请退出重试',
showCancel: false,
confirmText: "确定",
success: (res) => {
if (res.confirm) {
}
},
});
}
this.stopBluetoothDevicesDiscovery();
}, this.timeoutOfBlueScanFailure30s);
// location me and show dog position after blue check ok
//this.doLocationMe();
// start a timer for start status check
this.startchecktimer = setInterval(() => {
if (this.isblueok) {
this.dogpailoadingtxt = "定位成功";
if (this.startchecktimer) {
clearInterval(this.startchecktimer);
}
uni.showToast({
title: '定位成功开始吧',
duration: 3000
});
}
}, 2000);
},
3、开始搜寻附近的蓝牙外围设备
startBluetoothDevicesDiscovery(needlocation) {
discovering = true; // wxf 190907 增加
if (this.platform == "android") {
} else { // for ios reopen blue adapter
uni.openBluetoothAdapter();
}
this.loading = true;
uni.startBluetoothDevicesDiscovery({
success: e => {
console.log('开始搜索蓝牙设备:' + e.errMsg);
this.onBluetoothDeviceFound(needlocation);
},
fail: e => {
console.log('搜索蓝牙设备失败,错误码:' + e.errCode);
if (e.errCode !== 0) {
//initTypes(e.errCode);
}
}
});
},
4、 监听寻找到新设备的事件
onBluetoothDeviceFound(needlocation) {
uni.onBluetoothDeviceFound(devices => {
console.log('开始监听寻找到新设备的事件');
console.log(devices)
this.getBluetoothDevices(needlocation);
});
},
5、 获取在蓝牙模块生效期间所有已发现的蓝牙设备
getBluetoothDevices(needlocation) {
uni.getBluetoothDevices({
success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg);
let devices = res.devices;
this.dogBluePaisCheck(devices, needlocation);
},
fail: e => {
console.log('获取蓝牙设备错误,错误码:' + e.errCode);
if (e.errCode !== 0) {
}
}
});
},
6、对获取到的所有设备信息for循环筛选对应的设备
checkRSSIFailure: function(pRSSI) {
// dBm <60, <70, <80, <90, <100
let A_Value = 80;
let n_Value = 4.18;
let _rssi = pRSSI; //pRSSI.replace('dBm','');
this.rssi = pRSSI;
_rssi = Math.abs(_rssi);
let power = (_rssi - A_Value) / (10 * n_Value);
let dis = Math.pow(10, power);
this.rssidis = dis;
console.info(" checkRSSIFailure : " + pRSSI + " ; dis :" + dis + "; dismax:" + this.rssidismax);
if (this.rssidis < this.rssidismax) {
return false;
} else {
return true;
}
},
dogBluePaisCheck: function(devices, needlocation) { // wxf 190907 去掉that
this.isblueok = false;
let pblueid = this.blueid;
for (var a = 0; a < devices.length; a++) {
var device = devices[a];
var advertisData = arrayBufferToHexString(device.advertisData);
var RSSI = device.RSSI;
var macarr = pblueid.toUpperCase().split("");
var mac_split = "";
for (var i = 0; i < macarr.length; i++) {
if (i != 0 && i % 2 == 0) {
mac_split = mac_split + ":" + macarr[i];
} else {
mac_split = mac_split + macarr[i];
}
}
//console.log("Device Found==" + device.deviceId + " vs " + pblueid + " vs " + mac_split);
if (this.platform == "android") {
if ("BIOTAG" == device.name && (device.deviceId == mac_split || advertisData.indexOf(pblueid) != -1)) {
console.log("For Android Device Found==" + device.deviceId + " vs blueid=" + pblueid + " , RSSI=" + RSSI);
if (this.bluetimestoper) {
clearTimeout(this.bluetimestoper);
}
this.isblueok = true;
this.loading = false;
this.bluescancnt = 0;
this.stopBluetoothDevicesDiscovery();
if (needlocation) {
//this.doLocationMe();
}
if (this.checkRSSIFailure(RSSI) && this.bluechecker) {
// 蓝牙扫码正常, rssi过远,提示
//this.tiedogalert();
}
break;
}
} else {
if ("BIOTAG" == device.name || advertisData.indexOf(pblueid) != -1) {
console.log("For iOS/others Device Found==" + device.deviceId + " vs blueid=" + pblueid + " , RSSI=" + RSSI);
//device['info']=JSON.stringify(device);
if (this.bluetimestoper) {
clearTimeout(this.bluetimestoper);
}
this.isblueok = true;
this.loading = false;
this.bluescancnt = 0;
this.stopBluetoothDevicesDiscovery();
if (needlocation) {
//this.doLocationMe();
}
if (this.checkRSSIFailure(RSSI) && this.bluechecker) {
// 蓝牙扫码正常, rssi过远,提示
//this.tiedogalert();
}
break;
}
}
}
},