步骤:
1、wx.openBluetoothAdapter//蓝牙初始化
2、 wx.onBluetoothDeviceFound //监听寻找到新设备的事件
3、 wx.startBluetoothDevicesDiscovery //开始搜寻附近的蓝牙外围设备
4、 wx.getBluetoothDevices//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
5、wx.stopBluetoothDevicesDiscovery//停止搜寻附近的蓝牙外围设备。搜索到需要设备时或者连接时候停止搜索
6、wx.createBLEConnection //连接设备
7、 wx.getBLEDeviceServices //获取所有服务列表
8、wx.getBLEDeviceCharacteristics//获取蓝牙设备某个服务中的所有特征值
9、wx.onBLEConnectionStateChange //监听设备连接状态(本人用小米2手环测试经常断)
10、 wx.notifyBLECharacteristicValueChange // 启用低功耗蓝牙设备特征值变化是的notify功能
11、 wx.onBLECharacteristicValueChange//开始监听特征值的变化
12、 wx.writeBLECharacteristicValue//写入特征值
13、wx.readBLECharacteristicValue//读取特征值
以下是demo代码:
onLoad: function (options) {
var that=this
//蓝牙初始化
wx.openBluetoothAdapter({
success: function(res) {
console.log(res)
//监听寻找到新设备的事件
wx.onBluetoothDeviceFound(function (res) {
// console.log(res)
})
},
fail:function(res){
wx.showModal({
title: '提示',
content: '请检查手机蓝牙是否打开',
})
}
})
},
点击搜索设备按钮(lanya):
lanya: function () {
wx.showLoading({
title: '搜索中',
})
setTimeout(function(){
wx.hideLoading()
},1500)
//开始搜寻附近的蓝牙外围设备
wx.startBluetoothDevicesDiscovery({
success: function (res) {
console.log('搜索完成')
//获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
wx.getBluetoothDevices({
success: function (res) {
if(res.devices.length==0){
wx.showModal({
title: '提示',
content: '没有搜索到设备,请重试',
})
wx.navigateTo({
url: '../index/index',
})
}
console.log(res)
//console.log(res.devices[0].deviceId)
// console.log(res.devices[0].name)
console.log(res.devices)
var data=res.devices
wx.navigateTo({
url: '../lanya/lanya?data=' + JSON.stringify(data),
})
//停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。
// wx.stopBluetoothDevicesDiscovery({
// success: function(res) {
// console.log('已经停止搜索')
// },
// })
},
fail:function(res){
console.log('没有找到设备')
}
})
},
fail: function (fa) {
console.log(fa)
}
})
},
onLoad: function (options) {
var Id=[]
var Name=[]
var data = JSON.parse(options.data)
console.log(data)
var that=this
console.log(data)
that.setData({
array:data
})
},
点击连接设备按钮:
sub:function(e){
var deviceId = e.currentTarget.id
app.globalData.deviceId=deviceId
console.log(e.currentTarget.id)
//连接设备
wx.createBLEConnection({
deviceId:deviceId,
success: function(res) {
console.log(res)
//获取服务列表
wx.navigateTo({
url: '../res/res?id='+deviceId,
})
console.log('id:' + deviceId)
},
fail:function(res){
wx.showModal({
title: '提示',
content: '连接超时,请重试',
})
// console.log('id:' + deviceId)
console.log(res)
}
})
},
onLoad: function (options) {
var deviceId=options.id
var that=this
//获取所有服务
wx.getBLEDeviceServices({
deviceId: deviceId,
success: function (res) {
// console.log(res)
console.log(res.services)
that.setData({
array:res.services
})
},
})
},
进入某个服务
sub:function(e){
var that = this
var serviceId=e.target.id
//获取蓝牙设备某个服务中的所有特征值
wx.getBLEDeviceCharacteristics({
deviceId:app.globalData.deviceId,
serviceId:serviceId,
success: function(res) {
console.log(res)
that.data.characteristics=res.characteristics//获取characteristic
wx.navigateTo({
url: '../notify/notify?ct=' + JSON.stringify(that.data.characteristics) + '&serviceId=' + serviceId,
})
},
fail:function(res){
console.log(res)
}
})
//监听设备连接状态
wx.onBLEConnectionStateChange(function (res) {
console.log(res)
if (res.connected == false) {
wx.showModal({
title: '提示',
content: '设备连接已断开',
})
}
})
},
onLoad: function (options) {
var ct = JSON.parse(options.ct)
serviceId=options.serviceId
var that=this
that.setData({
array:ct,
serviceId:serviceId,
})
},
进入某个特征值
sub:function(e){
var that = this
var cht = e.target.id
var deviceId = app.globalData.deviceId
// 启用低功耗蓝牙设备特征值变化是的notify功能
wx.notifyBLECharacteristicValueChange({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: cht,
state: true,
success: function (res) {
//开始监听
wx.onBLECharacteristicValueChange(function (characteristics) {
console.log(characteristics.value)
var a = characteristics.value
var int8array = new Int8Array(a);
console.log("监听到特征值更新:" + int8array[0])
// var hex = ab2str(a);
// console.log("返回的值: ".hex)
})
console.log(res)
wx.navigateTo({
url: '../write/write?cht=' + cht + '&serviceId=' + serviceId + '&deviceId=' + deviceId,
})
},
fail:function(res){
console.log(res)
},
})
},
**```
onLoad: function (options) {
var that=this
var cht = options.cht
var deviceId=options.deviceId
var serviceId = options.serviceId
that.setData({
deviceId:deviceId,
cht:cht,
serviceId:serviceId
})
},
向该特征值写入数据:**
formSubmit:function(e){
var that = this
var value=e.detail.value.psw
console.log(value)
//写入数据
wx.writeBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cht,
value: that.str2ab(value),
success: function (res) {
console.log(res)
},
fail:function(res){
console.log(res)
}
});
wx.readBLECharacteristicValue({
deviceId: that.data.deviceId,
serviceId: that.data.serviceId,
characteristicId: that.data.cht,
success: function(e) {
console.log(e)
},
})
},
重点写入的字符串要转换成Arraybuffer
// 字符串转为ArrayBuffer对象,参数为字符串
str2ab: function(str) {
var buf = new ArrayBuffer(str.length * 2); // 每个字符占用2个字节
var bufView = new Uint16Array(buf);
for(var i = 0, strLen=str.length; i
**监听的特征值变化回调是ArrayBuffer也要转换为字符串,可是我直接写下标就能获取,写转换获得就是undefined
function ab2str(arrayBuffer) {
return String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
// let unit8Arr = new Uint8Array(arrayBuffer);
// let encodedString = String.fromCharCode.apply(null, unit8Arr),
// decodedString = decodeURIComponent(escape((encodedString)));//没有这一步中文会乱码
// return decodedString;
}