|
|
|
@ -120,7 +120,7 @@ |
|
|
|
let that = this |
|
|
|
uni.showLoading({ |
|
|
|
mask: true, |
|
|
|
title: '加载中...' |
|
|
|
title: '打印中...' |
|
|
|
}); |
|
|
|
uni.getSystemInfo({ |
|
|
|
success(res) { |
|
|
|
@ -133,13 +133,26 @@ |
|
|
|
success: (res2) => { |
|
|
|
that.sousuo() |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('初始化失败', res) |
|
|
|
this.isShowSearch = true |
|
|
|
uni.hideLoading(); |
|
|
|
fail: (res) => { |
|
|
|
console.log('初始化失败', res) |
|
|
|
if(res.errCode == '10001'){ |
|
|
|
console.log('蓝牙未打开', res) |
|
|
|
// that.tui.toast('请打开蓝牙后,再进行连接') |
|
|
|
uni.showToast({ |
|
|
|
duration: 2000, |
|
|
|
title: "请打开蓝牙后,再进行连接", |
|
|
|
icon: 'none', |
|
|
|
mask:true |
|
|
|
}) |
|
|
|
}else{ |
|
|
|
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') |
|
|
|
that.isShowSearch = true |
|
|
|
} |
|
|
|
setTimeout(res=>{ |
|
|
|
uni.hideLoading(); |
|
|
|
},2000) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
}, |
|
|
|
// 开始搜索附近的蓝牙外围设备 |
|
|
|
sousuo() { |
|
|
|
@ -148,18 +161,20 @@ |
|
|
|
success(res3) { |
|
|
|
that.jiantingshebei() |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
that.tui.toast('获取设备失败', err) |
|
|
|
fail(err) { |
|
|
|
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') |
|
|
|
wx.closeBLEConnection({ |
|
|
|
deviceId: uni.getStorageSync('deviceId'), |
|
|
|
success(res) { |
|
|
|
|
|
|
|
console.log(res) |
|
|
|
} |
|
|
|
}) |
|
|
|
uni.removeStorageSync('deviceId'); |
|
|
|
that.isShowSearch = true |
|
|
|
that.sousuo() |
|
|
|
uni.hideLoading(); |
|
|
|
that.sousuo() |
|
|
|
setTimeout(res=>{ |
|
|
|
uni.hideLoading(); |
|
|
|
},2000) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
@ -179,7 +194,7 @@ |
|
|
|
that.lianejieshebei(uni.getStorageSync('deviceId')) |
|
|
|
} else { |
|
|
|
uni.hideLoading(); |
|
|
|
this.isShowSearch = true |
|
|
|
that.isShowSearch = true |
|
|
|
// 监听扫描到新设备事件 |
|
|
|
wx.onBluetoothDeviceFound((res4) => { //333 |
|
|
|
res4.devices.forEach((device) => { |
|
|
|
@ -193,7 +208,11 @@ |
|
|
|
|
|
|
|
}, |
|
|
|
lianejieshebei(deviceId) { |
|
|
|
let that = this; |
|
|
|
let that = this; |
|
|
|
uni.showLoading({ |
|
|
|
mask: true, |
|
|
|
title: '加载中...' |
|
|
|
}); |
|
|
|
// 连接设备 |
|
|
|
wx.createBLEConnection({ //555 |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
@ -201,8 +220,8 @@ |
|
|
|
that.isShowSearch = false |
|
|
|
that.huoqufuwu(deviceId) |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('连接设备失败', res) |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') |
|
|
|
if (uni.getStorageSync('deviceId')) { |
|
|
|
that.huoqufuwu(uni.getStorageSync('deviceId')) |
|
|
|
} else { |
|
|
|
@ -216,8 +235,9 @@ |
|
|
|
that.sousuo() |
|
|
|
that.isShowSearch = true |
|
|
|
} |
|
|
|
|
|
|
|
uni.hideLoading(); |
|
|
|
setTimeout(res=>{ |
|
|
|
uni.hideLoading(); |
|
|
|
},2000) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
@ -227,12 +247,10 @@ |
|
|
|
wx.getBLEDeviceServices({ //666 |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
success: (res5) => { |
|
|
|
|
|
|
|
that.duxietezhengzhi(deviceId, res5) |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
|
|
|
|
that.tui.toast('获取服务失败', res) |
|
|
|
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') |
|
|
|
wx.closeBLEConnection({ |
|
|
|
deviceId: uni.getStorageSync('deviceId'), |
|
|
|
success(res1) { |
|
|
|
@ -240,7 +258,9 @@ |
|
|
|
} |
|
|
|
}) |
|
|
|
that.isShowSearch = true |
|
|
|
uni.hideLoading(); |
|
|
|
setTimeout(res=>{ |
|
|
|
uni.hideLoading(); |
|
|
|
},2000) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
@ -251,10 +271,11 @@ |
|
|
|
deviceId: deviceId, // 搜索到设备的 deviceId |
|
|
|
serviceId: res5.services[0].uuid, // 上一步中找到的某个服务 |
|
|
|
success: (res6) => { |
|
|
|
|
|
|
|
console.log('characteristics',res6.characteristics) |
|
|
|
for (let i = 0; i < res6.characteristics.length; i++) { |
|
|
|
let item = res6.characteristics[i] |
|
|
|
if (item.properties.write) { // 该特征值可写 |
|
|
|
let item = res6.characteristics[i] |
|
|
|
that.startNoticeBle(deviceId,res5.services[0].uuid,res6.characteristics[2].uuid) |
|
|
|
if (item.properties.write) { // 该特征值可写 |
|
|
|
|
|
|
|
uni.setStorageSync('deviceId', deviceId); |
|
|
|
uni.setStorageSync('serviceId', res5.services[0].uuid); |
|
|
|
@ -265,7 +286,7 @@ |
|
|
|
} |
|
|
|
}, |
|
|
|
fail: (res) => { |
|
|
|
that.tui.toast('读写特征值失败', res) |
|
|
|
that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入') |
|
|
|
wx.closeBLEConnection({ |
|
|
|
deviceId: uni.getStorageSync('deviceId'), |
|
|
|
success(res) { |
|
|
|
@ -273,9 +294,55 @@ |
|
|
|
} |
|
|
|
}) |
|
|
|
that.isShowSearch = true |
|
|
|
uni.hideLoading(); |
|
|
|
setTimeout(res=>{ |
|
|
|
uni.hideLoading(); |
|
|
|
},2000) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
// 开启蓝牙数据监听 |
|
|
|
startNoticeBle(deviceId,serviceId,characteristicId) { |
|
|
|
let _this = this |
|
|
|
uni.notifyBLECharacteristicValueChange({ |
|
|
|
state: true, // 启用 notify 功能 |
|
|
|
deviceId: deviceId, |
|
|
|
serviceId: serviceId, |
|
|
|
characteristicId: characteristicId, |
|
|
|
success(res) { |
|
|
|
console.log("监听成功===", res.errMsg, 'deviceId===', deviceId) |
|
|
|
_this.GetDataFromBle(); |
|
|
|
}, |
|
|
|
fail: function(err) { |
|
|
|
console.log('开启监听失败', err) |
|
|
|
} |
|
|
|
}) |
|
|
|
}, |
|
|
|
// 设备返回的数据接收 |
|
|
|
GetDataFromBle() { |
|
|
|
var _this = this; |
|
|
|
uni.onBLECharacteristicValueChange((res) => { |
|
|
|
// 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,所以需要通过一个方法转换成字符串 |
|
|
|
console.log('返回的十六进制', _this.ab2hex(res.value)) |
|
|
|
_this.bleData = _this.ab2hex(res.value) |
|
|
|
uni.hideLoading() |
|
|
|
uni.closeBLEConnection({ |
|
|
|
deviceId: uni.getStorageSync('deviceId'), |
|
|
|
success(res) { |
|
|
|
console.log('断开') |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
ab2hex(buffer) { |
|
|
|
const hexArr = Array.prototype.map.call( |
|
|
|
new Uint8Array(buffer), |
|
|
|
function(bit) { |
|
|
|
return ('00' + bit.toString(16)).slice(-2) |
|
|
|
} |
|
|
|
) |
|
|
|
return hexArr.join('') |
|
|
|
}, |
|
|
|
xierushuju() { |
|
|
|
let that = this; |
|
|
|
@ -312,12 +379,10 @@ |
|
|
|
serviceId: uni.getStorageSync('serviceId'), |
|
|
|
characteristicId: uni.getStorageSync('characteristicId'), |
|
|
|
value: buffer, |
|
|
|
success(res) { |
|
|
|
|
|
|
|
success(res) { |
|
|
|
|
|
|
|
}, |
|
|
|
fail(res) { |
|
|
|
|
|
|
|
wx.closeBLEConnection({ |
|
|
|
deviceId: uni.getStorageSync('deviceId'), |
|
|
|
success(res) { |
|
|
|
@ -325,14 +390,14 @@ |
|
|
|
} |
|
|
|
}) |
|
|
|
if (uni.getStorageSync('deviceId')) { |
|
|
|
|
|
|
|
that.xierushuju() |
|
|
|
} else { |
|
|
|
that.jiantingshebei() |
|
|
|
} |
|
|
|
} |
|
|
|
uni.hideLoading(); |
|
|
|
} |
|
|
|
}) |
|
|
|
uni.hideLoading(); |
|
|
|
|
|
|
|
}, |
|
|
|
handleSearchClose() { |
|
|
|
this.isShowSearch = false |
|
|
|
|