BLEManager.js 5.32 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
var device_QHC_UUID = '0000ffb0-0000-1000-8000-00805f9b34fb' // 蓝牙电子秤uuid
var weighing_scale_type = '0BFF2608AC01' // 体重秤id
var weighing_scale_close = 'ac01fe010000cccb'// 体重秤灯熄灭
var weighing_scale_measure_end = 'ca'// 体重秤灯熄灭
var devList = []
var notOpenBle = false

function openBluetoothAdapter(success,fail) {

getBLEData(success, fail)
wx.onBluetoothAdapterStateChange(function (res) {
// callback
if (res.available == false) {
notOpenBle = true
fail('设备链接已断开')
} else {
console.log('蓝牙连接开启')
if(notOpenBle){
getBLEData(success, fail)
}
}
})
}

function getBLEData(success, fail) {
wx.openBluetoothAdapter({
success: function (res) {
// success
notOpenBle = false
wx.getBluetoothAdapterState({
success: function (res) {
console.log('蓝牙适配器状态', res)
if (res.discovering == true){
console.log('正在搜索')
findingDevice(0, function (dev) {
success(dev)
})
} else {
searchDevice(success, fail)
}
}
})
console.log('初始化蓝牙适配器')
},
fail: function (res) {
console.log(res)
if (res.errMsg.indexOf("already opened") != -1){
notOpenBle = false
wx.getBluetoothAdapterState({
success: function (res) {
if (res.discovering == true) {
console.log('正在搜索')
findingDevice(0, function (dev) {
success(dev)
})
} else {
console.log('开始搜索')
searchDevice(success, fail)
}
}
})
console.log('初始化蓝牙适配器')
return
}
notOpenBle = true
fail('请检查蓝牙是否打开')
}
})
}

function searchDevice(success, fail){
// 开始扫描
// var uuid = device_QHC_UUID
// var system = wx.getSystemInfoSync()
// if (system.platform == 'android'){
// var str = new String(system.system)
// console.log('系统版本', parseInt(str))
// }
wx.startBluetoothDevicesDiscovery({
// services: [device_QHC_UUID],
allowDuplicatesKey: true,
success: function (res) {
console.log('开始扫描',res)
findingDevice(0, function (dev) {
success(dev)
})
},
fail: function (res) {
console.log('扫描报错',res)
console.log(res)
}
})
}

// 搜索到设备
function findingDevice(filter,valueChange){
var response = {};
response.status = '设备已连接,请站上体重秤'
valueChange(response)
console.log('开启搜索监听')
wx.onBluetoothDeviceFound(function(res) {
// console.log('扫描到',res)
var same = false
var currentDev ;
if(res.deviceId){ // android
currentDev = res
} else if (res.devices){ // ios
currentDev = res.devices[0]
}
// 解析广播数据
var data;
if (wx.getSystemInfoSync().version == '6.5.7'){
data = wx.base64ToArrayBuffer(currentDev.advertisData)
} else {
data = currentDev.advertisData
}
var str = buf2hex(data).toLowerCase()
if (res.deviceId) { // android
str = buf2hex(currentDev.advertisData).toLowerCase()
}
var index = str.indexOf(weighing_scale_type.toLowerCase())
// console.log(str)
if (index == -1){ // 过滤电子秤
// console.log('不是电子秤')
return
}
if (str.indexOf(weighing_scale_close) != -1) {
console.log('设备休眠')
return
}
for(var i = 0;i < devList.length;i++){
var item = devList[i]
if(item.deviceId == currentDev.deviceId){
same = true
var dataStr = str.substr(index + weighing_scale_type.length - 4,16)
// console.log('测量数据', dataStr)
var weightH = dataStr.substr(4,2)
var weightL = dataStr.substr(6,2)

var weightInt = parseInt(weightH.substr(1, 1) + weightL, 16) / 10
// 转成str加上小数点
var wStr = weightInt.toString()
response.weight = weightInt
if (wStr.indexOf('.') == -1){
response.weight = wStr + '.0'
}
console.log('测量数据', weightInt)
if (dataStr.toLowerCase().indexOf(weighing_scale_measure_end) != -1) {
response.status = '测量完成'
if(weightInt > 150){
return
}
valueChange(response)
return
}
// console.log('返回测量数据', dataStr)
response.status = '设备已连接,测量中'
}
}
if(!same){
devList.push(currentDev)
response.devList = devList
response.status = '搜索到设备,正在连接'
}
response.devList = devList
valueChange(response)
})
}

function buf2hex(buffer) { // buffer is an ArrayBuffer
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}

function closeBLE(){
wx.stopBluetoothDevicesDiscovery({
complete: function (res) {
console.log('停止搜索', res)
},
})
wx.closeBluetoothAdapter({
complete: function (res) {
console.log('关闭蓝牙',res)
},
})
}

module.exports = {
openBluetoothAdapter: openBluetoothAdapter,
findingDevice : findingDevice,
closeBLE: closeBLE
}