| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 | 
							- class Bluetooth {
 
- 	constructor() {
 
- 		this.isOpenBle = false;
 
- 		this.deviceId = "";
 
- 		this.serviceId = "";
 
- 		this.writeId = "";
 
- 		this.notifyId = "";
 
- 		this.openBluetoothAdapter();
 
- 	}
 
- 	showToast(title) {
 
- 		uni.showToast({
 
- 			title: title,
 
- 			icon: 'none',
 
- 			'duration': 2000
 
- 		});
 
- 	}
 
- 	openBluetoothAdapter() {
 
- 		return new Promise((resolve, reject) => {
 
- 			// #ifdef APP-PLUS
 
- 			plus.bluetooth.openBluetoothAdapter({
 
- 				success: res => {
 
- 					this.isOpenBle = true;
 
- 					// this.showToast("初始化蓝牙模块成功");
 
- 					resolve(res);
 
- 				},
 
- 				fail: err => {
 
- 					this.showToast(`初始化蓝牙模块失败` + JSON.stringify(err));
 
- 					reject(err);
 
- 				},
 
- 			});
 
- 			// #endif
 
- 		});
 
- 	}
 
- 	startBluetoothDevicesDiscovery() {
 
- 		if (!this.isOpenBle) {
 
- 			this.showToast(`初始化蓝牙模块失败`)
 
- 			return;
 
- 		}
 
- 		let self = this;
 
- 		uni.showLoading({
 
- 			title: '蓝牙搜索中'
 
- 		})
 
- 		return new Promise((resolve, reject) => {
 
- 			setTimeout(() => {
 
- 				plus.bluetooth.startBluetoothDevicesDiscovery({
 
- 					success: res => {
 
- 						resolve(res)
 
- 					},
 
- 					fail: res => {
 
- 						self.showToast(`搜索设备失败` + JSON.stringify(err));
 
- 						reject(err);
 
- 					}
 
- 				})
 
- 			}, 300);
 
- 		});
 
- 	}
 
- 	stopBluetoothDevicesDiscovery() {
 
- 		let self = this;
 
- 		return new Promise((resolve, reject) => {
 
- 			plus.bluetooth.stopBluetoothDevicesDiscovery({
 
- 				success: e => {
 
- 					uni.hideLoading();
 
- 				},
 
- 				fail: e => {
 
- 					uni.hideLoading();
 
- 					self.showToast(`停止搜索蓝牙设备失败` + JSON.stringify(err));
 
- 				}
 
- 			})
 
- 		});
 
- 	}
 
- 	createBLEConnection() {
 
- 		//设备deviceId
 
- 		let deviceId = this.deviceId;
 
- 		let self = this;
 
- 		uni.showLoading({
 
- 			mask: true,
 
- 			title: '设别连接中,请稍候...'
 
- 		})
 
- 		// console.log(this.deviceId);
 
- 		return new Promise((resolve, reject) => {
 
- 			plus.bluetooth.createBLEConnection({
 
- 				deviceId,
 
- 				success: (res) => {
 
- 					// console.log("res:createBLEConnection " + JSON.stringify(res));
 
- 					resolve(res)
 
- 				},
 
- 				fail: err => {
 
- 					uni.hideLoading();
 
- 					if (err?.code == -1) {
 
- 						// {"code":-1,"message":"already connect"}
 
- 						// self.showToast(`此设备已连接,请勿重复操作`);
 
- 					} else {
 
- 						self.showToast(`停止搜索蓝牙设备失败` + JSON.stringify(err));
 
- 					}
 
- 					reject(err);
 
- 				}
 
- 			})
 
- 		});
 
- 	}
 
- 	//获取蓝牙设备所有服务(service)
 
- 	getBLEDeviceServices() {
 
- 		let _serviceList = [];
 
- 		let deviceId = this.deviceId;
 
- 		let self = this;
 
- 		return new Promise((resolve, reject) => {
 
- 			setTimeout(() => {
 
- 				plus.bluetooth.getBLEDeviceServices({
 
- 					deviceId,
 
- 					success: res => {
 
- 						// 解决安卓低功耗蓝牙写入code 10007问题
 
- 						uni.setBLEMTU({
 
- 							deviceId: deviceId,
 
- 							// mtu: 180,
 
- 							mtu: 510,
 
- 							success(res) {
 
- 								uni.showToast({
 
- 									title: "设置蓝牙传输最大值成功",
 
- 									duration: 2000,
 
- 									icon: "none"
 
- 								})
 
- 								console.log("设置最大值成功")
 
- 							}
 
- 						})
 
- 						for (let service of res.services) {
 
- 							if (service.isPrimary) {
 
- 								_serviceList.push(service);
 
- 							}
 
- 						}
 
- 						uni.hideLoading();
 
- 						console.log("_serviceList: " + JSON.stringify(_serviceList));
 
- 						resolve(_serviceList)
 
- 					},
 
- 					fail: err => {
 
- 						uni.hideLoading();
 
- 						self.showToast(`获取设备Services` + JSON.stringify(err));
 
- 						reject(err);
 
- 					},
 
- 				})
 
- 			}, 500);
 
- 		});
 
- 	}
 
- 	//获取蓝牙设备某个服务中所有特征值(characteristic)
 
- 	getBLEDeviceCharacteristics() {
 
- 		let deviceId = this.deviceId;
 
- 		let serviceId = this.serviceId;
 
- 		let self = this;
 
- 		return new Promise((resolve, reject) => {
 
- 			plus.bluetooth.getBLEDeviceCharacteristics({
 
- 				deviceId,
 
- 				serviceId,
 
- 				success: res => {
 
- 					for (let _obj of res.characteristics) {
 
- 						//获取notify
 
- 						if (_obj.properties.notify) {
 
- 							self.notifyId = _obj.uuid;
 
- 							uni.setStorageSync('notifyId', self.notifyId);
 
- 						}
 
- 						//获取writeId
 
- 						if (_obj.properties.write) {
 
- 							self.writeId = _obj.uuid;
 
- 							uni.setStorageSync('writeId', self.writeId);
 
- 						}
 
- 					}
 
- 					//console.log("res:getBLEDeviceCharacteristics " + JSON.stringify(res));
 
- 					let result = {
 
- 						'notifyId': self.notifyId,
 
- 						'writeId': self.writeId
 
- 					};
 
- 					if (self.notifyId == "" || self.writeId == "") {
 
- 						self.showToast(`此服务不可用,请选择其它服务`);
 
- 					} else {
 
- 						self.showToast(`获取服务中所有特征值OK,${JSON.stringify(result)}`);
 
- 					}
 
- 					resolve(result)
 
- 				},
 
- 				fail: err => {
 
- 					uni.showToast({
 
- 						title: `getBLEDeviceCharacteristics` + JSON.stringify(err),
 
- 						duration: 2000,
 
- 						icon: "none"
 
- 					})
 
- 					// self.showToast(`getBLEDeviceCharacteristics` + JSON.stringify(err));
 
- 					reject(err);
 
- 				}
 
- 			})
 
- 		});
 
- 	}
 
- 	//断开联链接
 
- 	closeBLEConnection() {
 
- 		let deviceId = this.deviceId;
 
- 		plus.bluetooth.closeBLEConnection({
 
- 			deviceId,
 
- 			success(res) {
 
- 				console.log(res)
 
- 			}
 
- 		})
 
- 	}
 
- 	notifyBLECharacteristicValue() {
 
- 		let deviceId = this.deviceId;
 
- 		let serviceId = this.serviceId;
 
- 		let characteristicId = this.notifyId;
 
- 		plus.bluetooth.notifyBLECharacteristicValueChange({
 
- 			state: true, // 启用 notify 功能
 
- 			deviceId,
 
- 			serviceId,
 
- 			characteristicId,
 
- 			success(res) {
 
- 				plus.bluetooth.onBLECharacteristicValueChange(function(res) {
 
- 				});
 
- 			},
 
- 			fail(res) {
 
- 				console.log('notifyBLECharacteristicValueChange failed:' + res.errMsg);
 
- 			}
 
- 		});
 
- 	}
 
- 	writeBLECharacteristicValue(buffer) {
 
- 		let deviceId = this.deviceId;
 
- 		let serviceId = this.serviceId;
 
- 		let characteristicId = this.writeId;
 
- 		console.log("this: " + JSON.stringify(this));
 
- 		return new Promise((resolve, reject) => {
 
- 			setTimeout(() => {
 
- 				plus.bluetooth.writeBLECharacteristicValue({
 
- 					deviceId,
 
- 					serviceId,
 
- 					characteristicId,
 
- 					value: buffer,
 
- 					success(res) {
 
- 						console.log('message发送成功', JSON.stringify(res));
 
- 						resolve(res);
 
- 					},
 
- 					fail(err) {
 
- 						console.log('message发送失败', JSON.stringify(err));
 
- 						reject(err);
 
- 					}
 
- 				});
 
- 			}, 300)
 
- 		});
 
- 	}
 
- 	closeBluetoothAdapter() {
 
- 		plus.bluetooth.closeBluetoothAdapter({
 
- 			success: res => {
 
- 				// console.log(res)
 
- 			}
 
- 		});
 
- 	}
 
- 	//若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,无需进行搜索操作。
 
- 	reconnect() {
 
- 		(async () => {
 
- 			try {
 
- 				this.deviceId = this.deviceId || uni.getStorageSync("deviceId");
 
- 				this.serviceId = this.serviceId || uni.getStorageSync("serviceId");
 
- 				let result1 = await this.createBLEConnection();
 
- 				console.log("createBLEConnection: " + JSON.stringify(result1));
 
- 				let result2 = await this.getBLEDeviceServices();
 
- 				console.log("getBLEDeviceServices: " + JSON.stringify(result2));
 
- 				let result3 = await this.getBLEDeviceCharacteristics();
 
- 				console.log("getBLEDeviceCharacteristics: " + JSON.stringify(result3));
 
- 				// this.writeId = uni.getStorageSync("writeId");
 
- 				// this.notifyId = uni.getStorageSync("notifyId");
 
- 			} catch (err) {
 
- 				console.log("err: " + JSON.stringify(err));
 
- 			}
 
- 		})();
 
- 	}
 
- }
 
- export default Bluetooth;
 
 
  |