前言:本文使用的Beacon是智石科技的Smart Beacon,手机的操作系统是Android,其中实现室内定位最重要的参数是Beacon发射的蓝牙信号中的RSSI值。废话不多说,请看下面分解。
1.打开微信小程序开发文档:单击这个链接微信官方文档,找到蓝牙部分的API。
2.使用的API依次是:
1) wx.openBluetoothAdapter():打开手机蓝牙适配器
2) wx.startBluetoothDevicesDiscovery():开始搜索附近的蓝牙设备
3) wx.getBluetoothDevices():获取在蓝牙模块生效期间所有已发现的蓝牙设备
3.实现思路:通过利用以上三个API获取Beacon中的deviceId和RSSI,并以列表的形式展示在微信小程序中的页面中。其中deviceId是用于区分设备的 id,它是Beacon生产商已经设置好的。
4.代码实现:
1)在index.js中写一个获取Beacon信息的函数
/**
* 获取Beacon设备信息
*/
getiBeaconInfo: function () {
var that = this;
//是否正确打开蓝牙
wx.openBluetoothAdapter({
success: function (res) {
console.log("正常打开蓝牙适配器!");
//开始搜索附近的蓝牙设备
wx.startBluetoothDevicesDiscovery({
success: function (res) {
//获取在蓝牙模块生效期间所有已发现的蓝牙设备
wx.getBluetoothDevices({
success: function (res) {
//j是表示获取到的Beacon个数
var j = 0;
//定义一个对象数组来接收Beacon的信息
var arrayIBeaconInfo=[];
//定义一个对象来存储Beacon的信息,其中ids是存储Beacon的deviceId,rssis是存储Beacon的RSSI
var objectIBeaconInfo = { ids: '', rssis:0};
for (var i = 0; i < res.devices.length; i++) {
//获取特定的八个Beaon,其中这些具体的deviceId可以在智石提供的软件BrightBeacon中的获取,在BrightBeacon中,deviceId是对应的MAC
if (res.devices[i].deviceId == 'AC:23:3F:20:D3:81' || res.devices[i].deviceId == 'AC:23:3F:20:D3:7D' || res.devices[i].deviceId == 'AC:23:3F:20:D3:7A' || res.devices[i].deviceId == 'AC:23:3F:20:D3:80' || res.devices[i].deviceId == 'AC:23:3F:20:D3:75' || res.devices[i].deviceId == 'AC:23:3F:20:D3:83' || res.devices[i].deviceId == 'AC:23:3F:20:D3:7F' || res.devices[i].deviceId == 'AC:23:3F:20:D3:84') {
objectIBeaconInfo = { ids: res.devices[i].deviceId, rssis: res.devices[i].RSSI };
//将对象加入到Beacon数组中
arrayIBeaconInfo.push(objectIBeaconInfo);
j++;
}
}
//冒泡算法,将rssi值在arrayIBeaconInfo中从大到小进行排列
//temp为中间值
var temp;
for (var i = 0; i < arrayIBeaconInfo.length; i++) {
for (var j = i + 1; j < arrayIBeaconInfo.length; j++) {
// console.log("正在执行冒泡算法");
if (Math.abs(arrayIBeaconInfo[j].rssis) <= Math.abs(arrayIBeaconInfo[i].rssis)) {
temp = arrayIBeaconInfo[i];
arrayIBeaconInfo[i] = arrayIBeaconInfo[j];
arrayIBeaconInfo[j] = temp;
}
}
}
//将对象存入data中的全局变量Beacon中
that.setData({
Beacon: arrayIBeaconInfo,
})
},
fail: function (res) {
console.log("获取蓝牙设备失败!");
}
})
},
fail: function (res) {
console.log("搜索附近蓝牙失败!");
}
})
},
fail: function (res) {
console.log("没有打开蓝牙适配器");
},
complete: function (res) {
//complete
}
})
},
2)在index.js中的page中设置一个全局变量Beaon数组存储获取的Beaon信息,主要是为在页面中显示这些信息提供方便。
/**
* 页面的初始数据
*/
data: {
//
iBeacon: []
},
3)在index.js中的onload函数中执行getiBeaconInfo函数
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this
that.getiBeaconInfo();
},
4)在index.js中的onReady函数中对手机是否打开蓝牙进行提示,同时因为是室内定位,故我这里是设置每隔1秒重新获取Beacon的信息。
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
var that=this
wx.openBluetoothAdapter({
success: function(res) {
console.log("正确打开蓝牙适配器");
//页面初次渲染完成后每个1s进行刷新
setInterval(that.onLoad, 1000);
},
fail: function (res){
wx.showModal({
title: '提示',
content: '是否打开手机蓝牙?',
showCancel: true,
cancelText: '否',
cancelColor: 'black',
confirmText: '是',
confirmColor: '#44ADE2',
success: function(res) {
if (res.cancel){
wx.showModal({
title: '提示',
content: '请前往手机设置打开蓝牙',
});
//页面初次渲染完成后每个1s进行刷新
setInterval(that.onLoad, 1000);
}
},
})
}
})
},
5)在index.wxml中显示获取的信息,这些CSS可以根据个人喜好自己在index.wxss中设置。
<!--index.wxml-->
<view class="view0">
<!-- 标题 -->
<view class="view1">
<text style="width:100rpx;">蓝牙设备ID</text>
<label class="label1">RSSI值</label>
</view>
<view style="position:absolute;top:92rpx;width:100%;height:100%">
<view class="view2" wx:for="{{Beacon}}" wx:key="*this">
<label class="label2">{{Beacon[index].ids}}</label>
<label class="label3">{{Beacon[index].rssis}}</label>
</view>
</view>
</view>
5.成果实现:
总结:关于以上代码或者步骤如果有大神有疑问或者有更好的方法可以批评指正和留言交流,谢谢您的关注!我把上面的代码已经上传到我的Github中需要的可以单击这个链接获取Beacon的RSSI值自取。