|
|
@ -84,17 +84,14 @@ class Bluetooth { |
|
|
mask: true, |
|
|
mask: true, |
|
|
title: '设别连接中,请稍候...' |
|
|
title: '设别连接中,请稍候...' |
|
|
}) |
|
|
}) |
|
|
console.log(deviceId); |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
uni.createBLEConnection({ |
|
|
uni.createBLEConnection({ |
|
|
deviceId, |
|
|
deviceId, |
|
|
timeout: 5000, |
|
|
timeout: 5000, |
|
|
success: (res) => { |
|
|
success: (res) => { |
|
|
console.log(res) |
|
|
|
|
|
resolve(res) |
|
|
resolve(res) |
|
|
}, |
|
|
}, |
|
|
fail: err => { |
|
|
fail: err => { |
|
|
console.log(err) |
|
|
|
|
|
if (err.errMsg == "createBLEConnection:fail already connect") { |
|
|
if (err.errMsg == "createBLEConnection:fail already connect") { |
|
|
resolve(err) |
|
|
resolve(err) |
|
|
return true; |
|
|
return true; |
|
|
@ -165,12 +162,9 @@ class Bluetooth { |
|
|
uni.setStorageSync('writeId', self.writeId); |
|
|
uni.setStorageSync('writeId', self.writeId); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
console.log("notifyId:" + self.notifyId) |
|
|
|
|
|
console.log("writeId:" + self.writeId) |
|
|
|
|
|
if (!self.writeId || !self.notifyId) { |
|
|
if (!self.writeId || !self.notifyId) { |
|
|
self.num++ |
|
|
self.num++ |
|
|
if (self.num == service.length) { |
|
|
if (self.num == service.length) { |
|
|
console.log('找不到该读写的特征值') |
|
|
|
|
|
reject('找不到该读写的特征值'); |
|
|
reject('找不到该读写的特征值'); |
|
|
return false; |
|
|
return false; |
|
|
} else { |
|
|
} else { |
|
|
@ -209,24 +203,17 @@ class Bluetooth { |
|
|
let deviceId = this.deviceId; |
|
|
let deviceId = this.deviceId; |
|
|
let serviceId = this.serviceId; |
|
|
let serviceId = this.serviceId; |
|
|
let characteristicId = this.notifyId; |
|
|
let characteristicId = this.notifyId; |
|
|
console.log('----notifyBLECharacteristicValue----') |
|
|
|
|
|
console.log(characteristicId) |
|
|
|
|
|
console.log(serviceId) |
|
|
|
|
|
console.log(deviceId) |
|
|
|
|
|
uni.notifyBLECharacteristicValueChange({ |
|
|
uni.notifyBLECharacteristicValueChange({ |
|
|
state: true, // 启用 notify 功能
|
|
|
state: true, // 启用 notify 功能
|
|
|
deviceId, |
|
|
deviceId, |
|
|
serviceId, |
|
|
serviceId, |
|
|
characteristicId, |
|
|
characteristicId, |
|
|
success(res) { |
|
|
success(res) { |
|
|
console.log(res); |
|
|
|
|
|
uni.onBLECharacteristicValueChange(function(res) { |
|
|
uni.onBLECharacteristicValueChange(function(res) { |
|
|
console.log(res); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}, |
|
|
|
|
|
fail(res) { |
|
|
|
|
|
console.log('notifyBLECharacteristicValueChange failed:' + res.errMsg); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
@ -235,11 +222,7 @@ class Bluetooth { |
|
|
let deviceId = this.deviceId; |
|
|
let deviceId = this.deviceId; |
|
|
let serviceId = this.serviceId; |
|
|
let serviceId = this.serviceId; |
|
|
let characteristicId = this.writeId; |
|
|
let characteristicId = this.writeId; |
|
|
console.log('----writeBLECharacteristicValue----') |
|
|
|
|
|
console.log(characteristicId) |
|
|
|
|
|
console.log(serviceId) |
|
|
|
|
|
console.log(deviceId) |
|
|
|
|
|
console.log("this: " + JSON.stringify(this)); |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
uni.writeBLECharacteristicValue({ |
|
|
uni.writeBLECharacteristicValue({ |
|
|
deviceId, |
|
|
deviceId, |
|
|
@ -247,11 +230,9 @@ class Bluetooth { |
|
|
characteristicId, |
|
|
characteristicId, |
|
|
value: buffer, |
|
|
value: buffer, |
|
|
success(res) { |
|
|
success(res) { |
|
|
console.log('message发送成功', JSON.stringify(res)); |
|
|
|
|
|
resolve(res); |
|
|
resolve(res); |
|
|
}, |
|
|
}, |
|
|
fail(err) { |
|
|
fail(err) { |
|
|
console.log('message发送失败', JSON.stringify(err)); |
|
|
|
|
|
reject(err); |
|
|
reject(err); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
@ -261,7 +242,7 @@ class Bluetooth { |
|
|
closeBluetoothAdapter() { |
|
|
closeBluetoothAdapter() { |
|
|
uni.closeBluetoothAdapter({ |
|
|
uni.closeBluetoothAdapter({ |
|
|
success: res => { |
|
|
success: res => { |
|
|
console.log(res) |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
@ -274,17 +255,14 @@ class Bluetooth { |
|
|
this.serviceId = this.serviceId || uni.getStorageSync("serviceId"); |
|
|
this.serviceId = this.serviceId || uni.getStorageSync("serviceId"); |
|
|
|
|
|
|
|
|
let result1 = await this.createBLEConnection(); |
|
|
let result1 = await this.createBLEConnection(); |
|
|
console.log("createBLEConnection: " + JSON.stringify(result1)); |
|
|
|
|
|
|
|
|
|
|
|
let result2 = await this.getBLEDeviceServices(); |
|
|
let result2 = await this.getBLEDeviceServices(); |
|
|
console.log("getBLEDeviceServices: " + JSON.stringify(result2)); |
|
|
|
|
|
|
|
|
|
|
|
let result3 = await this.getBLEDeviceCharacteristics(); |
|
|
let result3 = await this.getBLEDeviceCharacteristics(); |
|
|
console.log("getBLEDeviceCharacteristics: " + JSON.stringify(result3)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
console.log("err: " + JSON.stringify(err)); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
})(); |
|
|
})(); |
|
|
|