openAdapter(){
let that = this
uni.closeBluetoothAdapter({
success(res) {
// 初始化蓝牙
uni.openBluetoothAdapter({ //调用微信小程序api 打开蓝牙适配器接口
success: function(res) {
console.log(res,'111')
uni.hideLoading();
that .connetBlue()
},
fail: function(res) { //如果手机上的蓝牙没有打开,可以提醒用户
console.log(res,'222')
if (res.errCode === 10001) {
if(res.errMsg == 'openBluetoothAdapter:fail system permission denied'){
uni.showToast({
title: that.$t('PleaseEnableBluetoothPermission'),
icon: 'none'
});
}else{
uni.showToast({
title: that.$t('PleaseTurnOnBluetooth'),
icon: 'none'
});
}
}
},
})
}
})
},
connetBlue() {
var that = this;
// that.setTime()
uni.startBluetoothDevicesDiscovery({
allowDuplicatesKey: false,
interval: 0,
success: function(res) {
that.found();
},
fail: function(res) {
console.log(res)
}
})
},
found() {
let deviceId;
var that = this;
uni.onBluetoothDeviceFound(devices => {
// uni.showLoading({
// title: that.$t('Loading')
// });
if (devices.devices[0].name == that.IMEI) {
uni.stopBluetoothDevicesDiscovery({
success: function(res) {
console.log('关闭蓝牙搜索');
}
});
// uni.hideLoading()
deviceId = devices.devices[0].deviceId
uni.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId, //设备id
success: function(res) {
console.log("连接蓝牙成功")
// uni.showToast({
// title: that.$t('SuccessfullyConnectedToBluetooth'),
// icon: 'success',
// duration: 1000
// })
// that.deviceId = deviceId;
uni.setStorageSync('deviceId', deviceId)
that.getServiceId();
},
fail: function(res) {
console.log(res)
}
});
}
});
},
getServiceId() {
var that = this
// that.setTime()
console.log(123456)
that.progress_txt = this.$t('Unlocking')
that.deviceId = uni.getStorageSync('deviceId')
uni.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: that.deviceId,
success: function(res) {
console.log(res.services)
//需要什么服务就用对应的services
that.services = res.services[0].uuid //因设备而议:该特征值只支持读
that.getCharacteId()
// that.drawCircle(2/60)
},
fail: function(res) {
console.log(res)
}
});
},
getCharacteId() {
var that = this
// that.setTime()
uni.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: that.deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: that.services,
success: function(res) {
console.log(res)
for (var i = 0; i < res.characteristics.length; i++) { //2个值
var model = res.characteristics[i]
if (model.uuid == '0783B03E-8535-B5A0-7140-A304D2495CB8') {
that.notifyUuid = model.uuid
// that.startNotice()
}
if (model.uuid == '0783B03E-8535-B5A0-7140-A304D2495CBA') {
that.characteristic = model.uuid
}
}
that.sendMy()
// that.drawCircle(1)
},
fail: function(res) {
console.log(res)
}
});
},
//buffer允许写入的uuid
sendMy() {
let that = this
// that.setTime()
// var buff = new Uint8Array(arr).buffer;
var arr = [0x41, 0x41, 0x30, 0x33, 0x0D, 0x0A]
var buff = new Uint8Array(arr).buffer;
uni.writeBLECharacteristicValue({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: that.deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: that.services,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: that.characteristic, //第二步写入的特征值
// 这里的value是ArrayBuffer类型
value: buff,
success: function(res) {
//此时设备已接收到你写入的数据
console.log("写入成功")
that.startNotice()
that.beginTime = that.dateFormat()
console.log(that.beginTime)
// that.drawCircle(1.5)
},
fail: function(err) {
console.log(err)
if (err.errCode == 10006) {
uni.showToast({
title: this.$t('PleaseReconnectTheDevice'),
icon: 'none',
duration: 1000
});
}
}
});
},
startNotice() {
var that = this;
// that.setTime()
uni.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
deviceId: that.deviceId,
serviceId: that.services,
characteristicId: that.notifyUuid, //第一步 开启监听 notityid 第二步发送指令 write
success(res) {
console.log('监听成功', res)
// that.time = setInterval(function(){
// that.count++
// },1000)
//接收蓝牙返回消息
uni.onBLECharacteristicValueChange((sjRes) => {
// 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,
//所以需要通过一个方法转换成字符串
clearInterval(that.timer); // 在这里取消定时器
that.timer = null; // 将定时器标识符设置为 null
that.drawCircle(2)
that.progress_txt = that.$t('UnlockingComplete')
var nonceId = that.ab2hex(sjRes.value) //10.0
var nonceId1 = that.hexCharCodeToStr(nonceId)
// console.log(sjRes)
console.log('返回值:' + nonceId1)
// uni.hideLoading();
uni.closeBLEConnection({
deviceId: that.deviceId,
success(res) {
console.log('已断开连接')
}
});
that.endTime = that.dateFormat()
console.log(that.endTime)
that.flag = true
that.unlockOKByBle()
});
},
fail(err) {
console.log('监听失败', err)
},
complete() {
// clearInterval(that.time)
setTimeout(() => {
console.log(that.flag, 1111)
if (that.finishTime - that.startTime >= 70 || that.flag == false) {
uni.showToast({
title: that.$t('UnlockingTimesOut'),
icon: 'none',
duration: 1000
})
uni.closeBluetoothAdapter({
success(res) {
console.log('关闭蓝牙模块')
}
});
}
}, 30000)
}
});
},
ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
},
hexCharCodeToStr(hexCharCodeStr) {
var trimedStr = hexCharCodeStr.trim();
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
var len = rawStr.length;
if (len % 2 !== 0) {
alert("存在非法字符!");
return "";
}
var curCharCode;
var resultStr = [];
for (var i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16);
resultStr.push(String.fromCharCode(curCharCode));
}
return resultStr.join("");
}
- 发布:2023-10-08 09:46
- 更新:2023-10-08 09:46
- 阅读:165
产品分类: uniapp/小程序/微信
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows11 版本22H2(OS内部版本 22621.2283)
HBuilderX类型: 正式
HBuilderX版本号: 3.8.11
第三方开发者工具版本号: 1.06.2306020
基础库版本号: 3.1.0
项目创建方式: HBuilderX
示例代码:
操作步骤:
1.点击扫码,扫描锁的二维码解析数据成功后,点击蓝牙开锁
2.第一次开锁成功,返回应有的数据后,将锁关闭
3.再次点击扫码,扫描锁的二维码解析,点击开锁,开锁过程中写入数据成功,但监听数据返回失败,开锁失败
1.点击扫码,扫描锁的二维码解析数据成功后,点击蓝牙开锁
2.第一次开锁成功,返回应有的数据后,将锁关闭
3.再次点击扫码,扫描锁的二维码解析,点击开锁,开锁过程中写入数据成功,但监听数据返回失败,开锁失败
预期结果:
第二次点击开锁仍开锁成功
第二次点击开锁仍开锁成功
实际结果:
第二次点击开锁失败,锁打不开
第二次点击开锁失败,锁打不开
bug描述:
蓝牙开锁成功一次后,第二次开锁失败
0 个回复