问题:
1.小程序蓝牙搜索能不能搜到手机设备
2.如何判断蓝牙是否打开
3.搜索指定设备
4.开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中,如何连接蓝牙
5.serviceId如何去选择
6.characteristic(特征值)干嘛的,怎么用
7.开启notify
8.如何写入数据
---------------------

原文:https://blog.csdn.net/caohoucheng/article/details/81633822

问题:
最近做了一个涉及到蓝牙模块小程序,做一下总结,为自己的成长做一份记录,如果能帮到大家的话是再好不过的了;

1.小程序蓝牙搜索能不能搜到手机设备
2.如何判断蓝牙是否打开
3.搜索指定设备
4.开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中,
如何连接蓝牙
5.设备服务所有 service(服务) 如何去选择
6.设备characteristic(特征值)干嘛的,怎么用
7.开启notify
8.写入数据

1.小程序蓝牙搜索能不能搜到手机设备
搜不到!!!
小程序蓝牙只支持BLE低功耗蓝牙
什么是低功耗蓝牙设备呢?百度一下,你就知道(^__^) 嘻嘻

2.如何判断蓝牙是否打开
利用wx.openBluetoothAdapter(OBJECT)判断蓝牙是否可用
在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下,调用wx.openBluetoothAdapter会返回错误,表示手机蓝牙功能不可用;

wx.openBluetoothAdapter({
success: function (res) {
console.log(res)
},
fail: function (res) {
wx.showModal({
content: '请开启手机蓝牙后再试'
})
}
})

注意:建议wx.openBluetoothAdapter(OBJECT)和wx.closeBluetoothAdapter(OBJECT)成对使用
wx.closeBluetoothAdapter:关闭蓝牙模块,使其进入未初始化状态。调用该方法将断开所有已建立的链接并释放系统资源;

3.搜索指定设备
wx.startBluetoothDevicesDiscovery(OBJECT)开始搜寻附近的蓝牙外围设备
wx.getBluetoothDevices(OBJECT)获取在小程序蓝牙模块生效期间所有已发现的蓝牙设备
wx.onBluetoothDeviceFound(CALLBACK) 监听寻找到新设备的事件

注意:搜索蓝牙wx.startBluetoothDevicesDiscovery(OBJECT)操作比较耗费系统资源,在搜索并连接到设备后调用 wx.stopBluetoothDevicesDiscovery(OBJECT) 方法停止搜索。

//开始搜索蓝牙
wx.startBluetoothDevicesDiscovery({
success: function (res) {
console.log('search', res)
}
})
//发现设备
wx.getBluetoothDevices({
success: function (res) {
console.log('发现设备', res)
if (res.devices[0]) {
console.log(that.ab2hext(res.devices[0].advertisData))
}
//5s内未搜索到设备,关闭搜索,关闭蓝牙模块
setTimeout(function(){
if (!that.data.deviceId){
wx.hideLoading()
app.showToast('搜索设备超时','none');
//关闭搜索
that.stopBluetoothDevicesDiscovery();
//关闭蓝牙
that.closeBluetoothAdapter();
}
},5000)
}
})
//监听发现设备
wx.onBluetoothDeviceFound(function (devices) {
console.log('发现设备:', devices.devices)
for (let i = 0; i < devices.devices.length; i++) {
//检索指定设备
if (devices.devices[i].name == '设备name') {
that.setData({
deviceId: devices.devices[i].deviceId
})
//关闭搜索
that.stopBluetoothDevicesDiscovery();
console.log('已找到指定设备:', devices.devices[i].deviceId);
}
}
})
ab2hext: function(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}

这段代码是通过设备名name去匹配配对设备,若5s内未搜到指定设备则关闭搜索,关闭蓝牙模块;
设备名是已发现的蓝牙设备device 对象中的name


4.开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中,如何连接蓝牙
搜索我们可以拿到了设备的deviceId,通过deviceId去连接蓝牙
Android 上获取到的deviceId为设备 MAC 地址,iOS 上获取到的deviceId则为设备 uuid,因此deviceId不能硬编码到代码中
那么可能就有机智的小伙伴说了,设置两个变量,一个为设备MAC,一个为设备uuid
在连接设备的之前判断下机型,ios设备deviceId取:设备uuid,android设备deviceId:MAC地址!!!
我原本也是这样想的,因为我们做的这个小程序是扫码连接指定设备(就好像共享单车一样),所以本来是想在二维码中直接放入mac和uuid然后连接的时候去根据机型去取对应值
但是!!!但是!!!但是!!!
在实现过程中发现,ios不同手机搜索到的设备deviceId还是不同的.
所以还是乖乖通过设备name(广播名),去获取deviceId去连接


只怪自己经验不足,还总想走捷径
正确的流程是
初始化蓝牙wx.openBluetoothAdapter(OBJECT)

开始搜索蓝牙 wx.startBluetoothDevicesDiscovery(OBJECT)

所有已发现的蓝牙设备wx.getBluetoothDevices(OBJECT)

监听寻找到新设备的事件wx.onBluetoothDeviceFound(CALLBACK)

连接低功耗蓝牙设备wx.createBLEConnection(OBJECT)

获取蓝牙设备所有 service(服务) wx.getBLEDeviceServices(OBJECT)

获取蓝牙设备某个服务中的所有 characteristic(特征值)wx.getBLEDeviceCharacteristics(OBJECT)

启用低功耗蓝牙设备特征值变化时的 notify 功能wx.notifyBLECharacteristicValueChange(OBJECT)

写入wx.writeBLECharacteristicValue(OBJECT)

在搜索到设备后通过拿到的设备的deviceId去连接设备

wx.createBLEConnection({
deviceId: that.data.deviceId,//搜索设备获得的蓝牙设备 id
success: function (res) {
console.log('连接蓝牙:', res.errMsg);
},
fail: function (res) {
app.showToast('连接超时,请重试或更换车辆', 'none');
that.closeBluetoothAdapter();
}
})

5.serviceId如何去选择
连接成功以后就可以去获取设备的服务列表,我这边拿的是FEE7的服务ID

wx.getBLEDeviceServices({
deviceId: that.data.deviceId,//搜索设备获得的蓝牙设备 id
success: function (res) {
let service_id = "";
for(let i = 0;i<res.services.length;i++){
if(services[i].uuid.toUpperCase().indexOf("FEE7") != -1){
service_id = services[i].uuid;
break;
}
}
console.log('fee7-service_id:', that.data.service_id);
},
fail(res){
console.log(res);
}
})

6.characteristic(特征值)干嘛的,怎么用
服务特征值是干嘛的:每个服务都包含了一组特征值用来描述服务的一些属性,获取是否可读,是否可写,是否可以开启notify通知等,当你跟蓝牙通信时需要这些特征值ID来传递数据。
服务特征值怎么用:

//获取特征值
wx.getBLEDeviceCharacteristics({
deviceId: that.data.deviceId,//搜索设备获得的蓝牙设备 id
serviceId: that.data.service_id,//服务ID
success: function (res) {
console.log('device特征值:', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) {
let charc = res.characteristics[i];
if (charc.properties.indicate) {
that.setData({indicate_id: charc.uuid});
console.log('indicate_id:', that.data.indicate_id);
}
if (charc.properties.write) {
that.setData({write_id: charc.uuid});
console.log('写write_id:', that.data.write_id);
}
if (charc.properties.read) {
that.setData({read_id: charc.uuid});
console.log('读read_id:', that.data.read_id);
}
}
}
});

筛选出你所需要的服务特征值
在得到对应特征值后可以在执行相关操作时使用
例如:
开启notify:必须设备的特征值支持notify或者indicate才可以成功调用
支不支持notify或者indicate就是我们上面筛选出来的对应值

if (charc.properties.indicate) {
that.setData({indicate_id: charc.uuid});
console.log('indicate_id:', that.data.indicate_id);
}

7.开启notify
开启notify后可以监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification

//开启notify
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.data.deviceId,//蓝牙设备id
serviceId: that.data.service_id,//服务id
characteristicId: that.data.indicate_id,//服务特征值indicate
success: function (res) {
console.log('开启notify', res.errMsg)
//监听低功耗蓝牙设备的特征值变化
wx.onBLECharacteristicValueChange(function (res) {
console.log('特征值变化', that.arrayBufferToHexString(res.value));
})
//写入数据

}
});

8.如何写入数据
如何写入数据呢,通过获取到的write特征值write_id
注意:必须设备的特征值支持write才可以成功调用

let buffer = that.hexStringToArrayBuffer(ArrayBuffer);
//写入数据
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,//设备deviceId
serviceId: that.data.service_id,//设备service_id
characteristicId: that.data.write_id,//设备write特征值
value: buffer,//写入数据
success: function (res) {
console.log('发送数据:', res.errMsg)
}
});
hexStringToArrayBuffer:function (str) {
if(!str) {
return new ArrayBuffer(0);
}
var buffer = new ArrayBuffer(str.length);
let dataView = new DataView(buffer)
let ind = 0;
for (var i = 0, len = str.length; i < len; i += 2) {
let code = parseInt(str.substr(i, 2), 16)
dataView.setUint8(ind, code)
ind++
}
return buffer;
}
总结:有几点特别需要注意,快拿出小本本
1.IOS里面蓝牙状态变化以后不能马上开始搜索,否则会搜索不到设备,必须要等待2秒以上
2.开启notify以后并不能马上发送消息,蓝牙设备有个准备的过程,需要在setTimeout中延迟1秒以上才能发送,否则会发送失败

setTimeout(function () {
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.service_id,
characteristicId: that.data.write_id,
value: buffer,
success: function (res) {
console.log('发送数据:', res.errMsg)
}
});
}, 1100);

3.搜索到设备后记得释放资源stopBluetoothDevicesDiscovery
4.不需要使用蓝牙的时候一定要关闭蓝牙.wx.openBluetoothAdapter(OBJECT)和wx.closeBluetoothAdapter(OBJECT)成对使用
---------------------
作者:Cc_JoJo
来源:CSDN
原文:https://blog.csdn.net/caohoucheng/article/details/81633822
版权声明:本文为博主原创文章,转载请附上博文链接!

posted on 2019-03-11 17:18  ZOYOO  阅读(2579)  评论(1编辑  收藏  举报