uniapp连接蓝牙电子秤
uniapp连接蓝牙电子秤(一)可查阅文档微信uniapp一,蓝牙电子秤最近公司有这么个需求,电子秤和小程序进行对接,实现实时监听电子秤的数据,对于这块的需求它的限制有些高,必须是低功耗蓝牙而且还是4.0的。价格上还偏高,但是没办法买了几台进行测试,来挑战下第一次整处处都是坑首先初始化蓝牙模块(切记要把faill也写上,有时候他不走success,就以为他不触发,其实他在faill里)let th
·
uniapp连接蓝牙电子秤(一)
最近公司有这么个需求,电子秤和小程序进行对接,实现实时监听电子秤的数据,对于这块的需求它的限制有些高,必须是低功耗蓝牙而且还是4.0的。价格上还偏高,但是没办法买了几台进行测试,来挑战下第一次整处处都是坑
首先初始化蓝牙模块(切记要把faill也写上,有时候他不走success,就以为他不触发,其实他在faill里)
let that = this
wx.openBluetoothAdapter({
success(res) {
console.log("初始化蓝牙模块")
..........自己的逻辑
}
})
初始化蓝牙之后就可以去搜索监听新的设备判断适配器能不能用
先初始化下蓝牙,然后去监听新设备,监听到之后存储起来放到数组中获取他的deviceId和name,
seach_searchBle() {
var that = this
console.log("initBule")
uni.openBluetoothAdapter({
success(res) {
console.log("初始化蓝牙模块")
console.log(res)
that.seach_onDevice()
uni.getBluetoothAdapterState({
success: function(res) {
console.log('触发了一次')
console.log(res)
if (res.available) {
if (res.discovering) {
that.stopFindBule()
}
//搜索蓝牙
//开始搜寻附近的蓝牙外围设备
console.log("开始搜寻附近的蓝牙外围设备")
uni.startBluetoothDevicesDiscovery({
success(res) {
uni.showToast({
title: '扫码设备成功',
icon: 'none'
})
}
})
} else {
console.log('本机蓝牙不可用')
}
},
})
}
})
},
seach_onDevice() {
console.log("监听寻找到新设备的事件---------------")
var that = this
//监听寻找到新设备的事件
uni.onBluetoothDeviceFound(function(devices) {
console.log('--------------new-----------------------' + JSON.stringify(devices))
var re = JSON.parse(JSON.stringify(devices))
let name = re.devices[0].name
let deviceId = re.devices[0].deviceId
that.devices.push({
name: name,
deviceId: deviceId,
services: [],
RSSI: re.devices[0].RSSI
})
})
},
之后我们就可以通过储存的列表里的设备进行对接蓝牙的操作我们获取到了它的name和deviceId就去对他进行连接
let that = this
wx.createBLEConnection({
deviceId: uni.getStorageSync('ble').deviceId,
success(res) {
if (res.errMsg == "createBLEConnection:ok") {
uni.showLoading({
title: '连接蓝牙中',
})
// that.connId = uni.getStorageSync('currDev').deviceId;
// that.currDev = item
setTimeout(function() {
that.lianjielanya(uni.getStorageSync('ble').deviceId)
}, 2000)
} else {
//uni.removeStorageSync('currDev')
console.log(res)
}
},
fail(res) {
console.log(res)
}
})
创建成功之后就可以对他进行连接操作,是不是很简单,就剩最后一步输出蓝牙给我们发送的数据
wx.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
success(res) {
console.log(res)
let serviceId = ""
for (var s = 0; s < res.services.length; s++) {
console.log(res.services[s].uuid)
let serviceId = res.services[s].uuid
wx.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId: serviceId,
success(res) {
var re = JSON.parse(JSON.stringify(res))
console.log('deviceId = [' + deviceId + '] serviceId = [' + serviceId + ']')
for (var c = 0; c < re.characteristics.length; c++) {
if (re.characteristics[c].properties.write == true) {
let uuid = re.characteristics[c].uuid
console.log(' deviceId = [' + deviceId + '] serviceId = [' + serviceId + '] characteristics=[' +
uuid)
for (var index in that.devices) {
if (that.devices[index].deviceId == deviceId) {
that.devices[index].services.push({
serviceId: serviceId,
characteristicId: uuid
})
break
}
}
console.log(JSON.stringify(that.devices))
setTimeout(() => {
wx.hideLoading()
}, 2000)
uni.showToast({
title: '连接成功',
icon: 'none'
})
// uni.navigateBack()
}
}
that.notifyBLECharacteristicValueChange(deviceId)
var lanta = true
}
})
}
},
fail(res) {
console.log(res)
},
})
读取蓝牙给的数据操作,注意这里的serviceId 和characteristicId
读数据操作是统一的所以写死,还有传过来的数据还得需要进行处理格式转换,里面有写
notifyBLECharacteristicValueChange() {
var that = this
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: uni.getStorageSync('ble').deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId: '0000FFE0-0000-1000-8000-00805F9B34FB',
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId: '0000FFE1-0000-1000-8000-00805F9B34FB',
success: (res) => {
console.log(res)
wx.onBLECharacteristicValueChange((res) => {
const buffer = new Uint8Array(res.value);
console.log(buffer)
})
},
fail: (res) => {
console.log('获取失败')
}
})
},
这样就可以实时监听到蓝牙数据,他会一直给你发送数据,是不是很简单,
有不懂的私信我,后面会对需求做优化处理,敬请期待。。。
更多推荐
已为社区贡献2条内容
所有评论(0)