uni-app蓝牙操作

1、打开蓝牙适配器

			openBluetoothAdapter() {
				uni.openBluetoothAdapter({
					success: res => {
						console.log('openBluetoothAdapter success', res);
						this.startBluetoothDevicesDiscovery();
					},
					fail: res => {
						if (res.errCode === 10001) {
							uni.showToast({
								title: '请打开蓝牙',
								icon: 'none'
							});
							uni.onBluetoothAdapterStateChange(res => {
								console.log('onBluetoothAdapterStateChange', res);
								if (res.available) {
									this.startBluetoothDevicesDiscovery();
								}
							});
						}
					}
				});
			},

2、开启蓝牙搜索

			startBluetoothDevicesDiscovery() {
				if (this.discoveryStarted) {
					return;
				}
				this.discoveryStarted = true;
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: true,
					success: res => {
						console.log('startBluetoothDevicesDiscovery success', res);
						this.onBluetoothDeviceFound();
					}
				});
			},

3、监听蓝牙设备列表

			onBluetoothDeviceFound() {
				uni.onBluetoothDeviceFound(res => {
					res.devices.forEach(device => {
						if (device.name.indexOf('蓝牙名称') == -1) {
							return;
						}
						this.createBLEConnection(device);
					});
				});
			},

4、建立连接

			createBLEConnection(device) {
				const deviceId = device.deviceId;
				uni.createBLEConnection({
					deviceId,
					success: res => {
						//隐藏加载框
						this.connect = true;
						this.restart();
						this.deviceId = deviceId;
						this.getBLEDeviceServices(deviceId);
					}
				});
				this.stopBluetoothDevicesDiscovery();
			},

5、关闭蓝牙连接

			closeBLEConnection() {
				uni.closeBLEConnection({
					deviceId: this.deviceId
				});
				this.connect = false;
			},

6、停止蓝牙搜搜

			stopBluetoothDevicesDiscovery() {
				uni.stopBluetoothDevicesDiscovery();
				this.discoveryStarted = false;
			},

7、获取蓝牙设备服务

			getBLEDeviceServices(deviceId) {
				uni.getBLEDeviceServices({
					deviceId,
					success: res => {
						console.log(res);
						for (let i = 0; i < res.services.length; i++) {
							if (res.services[i].isPrimary) {
								this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid);
								return;
							}
						}
					}
				});

			},

8、获取蓝牙特征值

			getBLEDeviceCharacteristics(deviceId, serviceId) {

				uni.getBLEDeviceCharacteristics({
					deviceId,
					serviceId,
					success: res => {
						console.log('getBLEDeviceCharacteristics success', res.characteristics);
						for (let i = 0; i < res.characteristics.length; i++) {
							let item = res.characteristics[i];
							console.log(item.properties)
							if (item.properties.read) {
								uni.readBLECharacteristicValue({
									deviceId,
									serviceId,
									characteristicId: item.uuid
								});
							}
							if (item.properties.write) {
								this.deviceId = deviceId;
								this.serviceId = serviceId;
								this.characteristicId = item.uuid;
							}
							if (item.properties.notify || item.properties.indicate) {
								uni.notifyBLECharacteristicValueChange({
									deviceId,
									serviceId,
									characteristicId: item.uuid,
									state: true
								});
							}
						}
					},
					fail(res) {
						console.error('getBLEDeviceCharacteristics', res);
					}
				});
				uni.onBLECharacteristicValueChange(characteristic => {

                    if(!this.connect){
						return
					}
                    //自己的操作
                    let value = util.ab2hex(characteristic.value);

				});
				uni.onBLEConnectionStateChange(res => {
					// 该方法回调中可以用于处理连接意外断开等异常情况
					console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`);
					this.connect = res.connected;
					if (!this.connect) {
						this.discoveryStarted = false;
						//this.finishStatus = true;
						clearInterval(this.interval);
						this.openBluetoothAdapter();
					}

				});
			},

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值