微信小程序怎么实现蓝牙连接?(代码示例)
来源:小白
发布时间:2018-11-14 09:52:37
阅读量:1791
微信小程序如何实现蓝牙连接?本篇文章给大家带来的内容是介绍微信小程序实现蓝牙连接的方法(步骤)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
最近的项目需要使用小程序的蓝牙功能与硬件设备进行连接相互传送数据指令,联调过程中发现一些问题,于是想着记录下来,方便以后查看!
1、初始化蓝牙设备
一般使用蓝牙功能肯定是想连接某一个蓝牙设备,所以需要知道这个蓝牙设备的名称,一般来说都是扫描二维码连接,那么当你扫描这个设备二维码的时候,就需要去初始化你手机上的蓝牙模块了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | initBlue: function (){
var that = this ;
wx.openBluetoothAdapter({
success: function (res) {
wx.showToast({
title: '初始化成功' ,
icon: 'success' ,
duration: 800
})
that.findBlue();
},
fail: function (res) {
wx.showToast({
title: '请开启蓝牙' ,
icon: 'fails' ,
duration: 1000
})
}
})
},
|
2、搜索蓝牙设备
手机蓝牙初始化成功之后,就会去搜索周边的蓝牙设备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | findBlue(){
var that = this
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false ,
interval: 0,
success: function (res) {
wx.showLoading({
title: '正在搜索设备' ,
})
that.getBlue()
}
})
},
|
3、获取蓝牙设备信息
搜索蓝牙设备之后,需要获取搜索到的蓝牙设备信息,微信小程序提供了两个方法可以获取搜索到的蓝牙设备信息,分别是:
wx.onBluetoothDeviceFound:监听寻找到新设备的事件 ,表示只要找到一个新的蓝牙设备就会调用一次该方法。
wx.getBluetoothDevices:获取在蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备
看两个方法的介绍我们知道他们的区别,但是不了解他们的区别会造成什么样的问题?
第一次我使用的是wx.onBluetoothDeviceFound方法进行联调,发现一切正常,由于调试的时候就只有一台设备,发现第二次重新扫码这个蓝牙设备的时候,找不到这个设备了,因为对这个方法来说,这不是一个新的设备,以前连接上过;或者当你因为某些原因蓝牙传送数据指令的时候出错了需要重新连接,再次连接的时候也找不到当前设备,还是同样的原因,因为当前设备对这个方法来说不是一个新设备
所以后来我就用了wx.getBluetoothDevices方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
getBlue(){
var that = this
wx.getBluetoothDevices({
success: function (res) {
wx.hideLoading();
for ( var i = 0; i < res.devices.length; i++){
if (res.devices[i].name == that.data.inputValue || res.devices[i].localName == that.data.inputValue){
that.setData({
deviceId: res.devices[i].deviceId,
consoleLog: "设备:" + res.devices[i].deviceId,
})
that.connetBlue(res.devices[i].deviceId);
return ;
}
}
},
fail: function (){
console.log( "搜索蓝牙设备失败" )
}
})
},
|
4、连接蓝牙设备
通过上一个步骤找到这个蓝牙之后,通过蓝牙设备的id进行蓝牙连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
connetBlue(deviceId){
var that = this ;
wx.createBLEConnection({
deviceId: deviceId,
success: function (res) {
wx.showToast({
title: '连接成功' ,
icon: 'fails' ,
duration: 800
})
console.log( "连接蓝牙成功!" )
wx.stopBluetoothDevicesDiscovery({
success: function (res) {
console.log( '连接蓝牙成功之后关闭蓝牙搜索' );
}
})
that.getServiceId()
}
})
},
|
5、获取服务uuid
连接上需要的蓝牙设备之后,获取这个蓝牙设备的服务uuid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | getServiceId(){
var that = this
wx.getBLEDeviceServices({
deviceId: that.data.deviceId,
success: function (res) {
var model = res.services[0]
that.setData({
services: model.uuid
})
that.getCharacteId()
}
})
},
|
6、通过id查看蓝牙设备的特征值
如果一个蓝牙设备需要进行数据的写入以及数据传输,就必须具有某些特征值,所以通过上面步骤获取的id可以查看当前蓝牙设备的特征值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | getCharacteId(){
var that = this
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,
serviceId: that.data.services,
success: function (res) {
for ( var i = 0; i < res.characteristics.length; i++) {
var model = res.characteristics[i]
if (model.properties.notify == true ) {
that.setData({
notifyId: model.uuid
})
that.startNotice(model.uuid)
}
if (model.properties.write == true ){
that.setData({
writeId: model.uuid
})
}
}
}
})
},
|
7、从后台服务器获取的指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | startNotice(uuid){
var that = this ;
wx.notifyBLECharacteristicValueChange({
state: true ,
deviceId: that.data.deviceId,
serviceId: that.data.services,
characteristicId: uuid,
success: function (res) {
wx.onBLECharacteristicValueChange( function (res) {
var nonceId = that.ab2hex(res.value)
wx.request({
method: "POST" ,
data: {
xx:nonceId
},
url: url,
success: (res) => {
that.sendMy(that.string2buffer(res.data.data.ciphertext))
}
})
}
})
},
|
8、将从后台服务获取的指令写入到蓝牙设备当中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | sendMy(buffer){
var that = this
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.services,
characteristicId: that.data.writeId,
value: buffer,
success: function (res) {
console.log( "写入成功" )
},
fail: function () {
console.log( '写入失败' )
},
complete: function (){
console.log( "调用结束" );
}
})
},
|
注:下面是需要使用到的两个格式相互转换的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | string2buffer(str) {
let val = ""
if (!str) return ;
let length = str.length;
let index = 0;
let array = []
while (index < length){
array.push(str.substring(index,index+2));
index = index + 2;
}
val = array.join( "," );
return new Uint8Array(val.match(/[\da-f]{2}/gi).map( function (h) {
return parseInt(h, 16)
})).buffer
},
ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ( '00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join( '' );
},
|
注:以上是蓝牙连接的全部流程,但是我们在实际使用中肯定不会这么顺畅,而且蓝牙发送指令的设备都会有一个特性,就是当前蓝牙设备有人连接上之后,其他人是搜索不到这个蓝牙设备的,所以你需要考虑在某些个特殊情况,代码里需要主动断开蓝牙连接把设备释放出来供其他用户使用,还有就是将指令写入蓝牙设备的时候很容易出问题,所以要写个回调去多次写入,保证成功性!
总结:以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。。