开发流程
- 打开蓝牙适配器,同时监听蓝牙适配器状态
- 开始搜索蓝牙设备
- 获取搜索到的蓝牙设备
- 连接前首先获取已连接蓝牙设备列表,判断该蓝牙设备是否已经连接,若未连接,则连接蓝牙设备,同时要监听蓝牙连接状态,关闭蓝牙搜索
- 获取已连接蓝牙设备的所有服务
- 根据已连接蓝牙设备服务来获取该设备所有的特征值,并且监听每个特征值的状态
- 对特征值进行读写数据操作
- 关闭蓝牙连接(与蓝牙适配器)
如果已经知道蓝牙设备ID,进行如下步骤
- 打开蓝牙适配器,同时监听蓝牙适配器状态
- 连接前首先获取已连接蓝牙设备列表,判断该蓝牙设备是否已经连接,若未连接,则连接蓝牙设备,同时要监听蓝牙连接状态
- 获取已连接蓝牙设备的所有服务
- 根据已连接蓝牙设备服务来获取该设备所有的特征值,并且监听每个特征值的状态
- 对特征值进行读写数据操作
- 关闭蓝牙连接(与蓝牙适配器)
注意事项
1.进行蓝牙开发调试前首先要打开手机蓝牙和定位。
2.因为小程序蓝牙连接不稳定,所以最好手机首先连接蓝牙设备然后再进行小程序的蓝牙连接。
3.对特征值进行相应属性功能的判断并对应操作,否则会操作失败。
4.一般获取到的特征值前两组服务所对应的特征值是蓝牙系统的,故不可以操作。
5.读写数据采用的是ArrayBuffter,一定要记得做数据转化,蓝牙数据包大小为最多20个字节。
6.发送命令给蓝牙设备,设备响应更新对应特征值的数据,到小程序监听到改变有一定延时。
蓝牙开发js
打开蓝牙适配器
openBluetoothAdapter:function(){
var that=this;
wx.openBluetoothAdapter({//开启蓝牙适配器
success:function(res) {
console.log(res)
that.startBluetoothDevicesDiscovery()
},//success
fail:function(res){//如果失败则监听蓝牙适配器状态
wx.onBluetoothAdapterStateChange(function (res) {
console.log(res)
if (res.available) {//当蓝牙适配器已打开,则开始下一步搜索蓝牙
that.startBluetoothDevicesDiscovery()
}
})//wx.onBluetoothAdapterStateChange
wx.showToast({
title: '请开蓝牙和定位',
icon: 'none'
})//wx.showToast
}//fail
})//wx.openBluetoothAdapter
}//openBluetoothAdapter
开始搜索蓝牙设备,获取设备
推荐使用wx.onBluetoothDeviceFound,此方法是动态监听搜索到的设备
wx.getBluetoothDevices这方法是一次性获取当前能搜索到的设备,所以不会显示一些后面搜索到设备,需要再次或多次调用才能得到相要的设备
startBluetoothDevicesDiscovery:function(){
var that=this;
wx.startBluetoothDevicesDiscovery({
allowDuplicatesKey: true,
success: (res) => {
that.onBluetoothDeviceFound()
},
})
},//startBluetoothDevicesDiscovery
onBluetoothDeviceFound:function(){//推荐使用wx.onBluetoothDeviceFound,此方法是动态监听搜索到的设备
//wx.getBluetoothDevices这方法是一次性获取当前能搜索到的设备,所以不会显示一些后面搜索到设备,需要再次或多次调用才能得到相要的设备
var that=this
wx.onBluetoothDeviceFound(function(res){
for (var i = 0; i < res.devices.length; i++) {//更新搜索到的设备信息
var device = res.devices[i];
if (!device.deviceId) {
break;
}
var tag = true;
for (var j = 0; j < that.data.devices.length; j++) {
if (that.data.devices[j].deviceId == device.deviceId) {//已搜索到的设备更新信息
var d = that.data.devices;
d[j] = device;
tag = false;
break;
}
}//for (var j = 0; j < that.data.devices.length; j++)
if (tag) {//新检测到的设备
var d = that.data.devices;
d[d.length] = device;
}
}//for (var i = 0; i < res.devices.length; i++)
that.setData({
devices: d
})
})//wx.onBluetoothDeviceFound
}//onBluetoothDeviceFound
建立蓝牙连接
checkBLEConnect(deviceId) {
var that = this;
wx.getConnectedBluetoothDevices({//判断蓝牙是否已经连接
success: function (res) {
console.log(res)
var tag = true;
for (var i = 0; i < res.devices.length; i++) {
if (res.devices[i].deviceId == deviceId) {//蓝牙已经连接
that.setData({
deviceId:deviceId
})
that.getBLEDeviceServices(deviceId);
tag = false;
break;
}
}//for (var i = 0; i < res.devices.length; i++)
if (tag) {//蓝牙未连接
that.connectBLE(deviceId)//连接蓝牙
that.stopBluetoothDevicesDiscovery()//停止搜索
}//if (tag)
}//success
})//wx.getConnectedBluetoothDevices
},
connectBLE: function (deviceId) {
var that = this;
wx.createBLEConnection({
deviceId,
success: function(res) {
console.log(res)
that.setData({
deviceId:deviceId
})
that.getBLEDeviceServices(deviceId)
},
fail: function (res) {
console.log(res)
wx.showToast({
title: '蓝牙连接失败',
icon: 'none'
})
}
})//wx.createBLEConnection
wx.onBLEConnectionStateChange(function (res) {//监听蓝牙连接状态
console.log(res)
if (!res.connected) {//如果蓝牙连接断开,重新连接
that.connectBLE(deviceId)
}
});//wx.onBLEConnectionStateChange
},
获取设备所有服务和特征值
getBLEDeviceServices(deviceId) {
var that = this;
wx.getBLEDeviceServices({
deviceId: deviceId,
success: function (res) {
for (let i = 0; i < res.services.length; i++) {
that.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
}
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
var that = this;
wx.getBLEDeviceCharacteristics({//获取设备对应服务特征值
deviceId:deviceId,
serviceId:serviceId,
success: function(res) {
console.log(res)
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]
if (item.properties.read) {//该特征值可读则读该特征值对应的数据
wx.readBLECharacteristicValue({
deviceId:deviceId,
serviceId:serviceId,
characteristicId: item.uuid,
success: function (res) {
console.log(res);
}
})//wx.readBLECharacteristicValue
}//if (item.properties.read)
if (item.properties.write) {//该特征值可写则写数据到该特征值
var array=[i,i,i,i,i,i,i];
var buffer=new Uint8Array(array).buffer;//注意发送数据一定是ArrayBuffer
wx.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: item.uuid,
value: buffer,
success: function (res) {
console.log(deviceId,serviceId,item.uuid,res);
}
})// wx.writeBLECharacteristicValue
}//if (item.properties.write)
if (item.properties.notify || item.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
deviceId:deviceId,
serviceId:serviceId,
characteristicId: item.uuid,
state: true,
success: function (res) {
console.log(res)
}
})//wx.notifyBLECharacteristicValueChange
}//if (item.properties.notify || item.properties.indicate)
}// for (let i = 0; i < res.characteristics.length; i++)
},
fail(res) {
console.error(res)
}
})
// 监听蓝牙设备发送过来的值
wx.onBLECharacteristicValueChange(function(res) {
console.log(res);
var value = new Uint8Array(res.value);//接收到的ArrayBuffer数组可对应ASCII码
console.log(value);
})//wx.onBLECharacteristicValueChange
},
关闭蓝牙连接
closeBLEConnection() {
wx.closeBLEConnection({//关闭蓝牙连接
deviceId: this.data.deviceId
})
},
【参考文档】
微信公众平台小程序API文档