uniapp通过蓝牙传输数据 (安卓和ios)

文章讨论了在使用Uniapp开发应用时,如何根据平台(iOS和Android)特性,优化数据传输。安卓需用低功耗蓝牙循环写入导致时间过长,而iOS支持大容量数据传输。作者选择在iOS上用低功耗蓝牙,Android则切换到经典蓝牙以提高效率。
摘要由CSDN通过智能技术生成

使用uniapp自带的低功耗蓝牙传数据,安卓最好不能超过20个字节(ios可以不限制字节大小),我传的数据较长,有几千个字节,ios倒是能一下就传过去,挺省事的。主要是安卓,非要用uniapp低功耗蓝牙传的话需要循环调用writeBLECharacteristicValue写入数据,要二十几秒才能传完,时间太长了。所以优化只能选择经典蓝牙。
我试了插件市场上面的很多经典蓝牙,选择了Android经典蓝牙插件,(ios不支持经典蓝牙,所以如果是ios系统就用低功耗蓝牙,安卓系统就用经典蓝牙)

				handleBlue(){
					this.platform = uni.getSystemInfoSync().platform
					if (this.platform == 'ios') {
				 		// ios
				 		this.handleIosBlue()
				 	} else {
				 		// 安卓
				 		this.handleIosBlue()
				 	}
				},
				 // 打开ios ble蓝牙
				handleIosBlue() {
				   uni.openBluetoothAdapter({
					success: (res) => {
						uni.startBluetoothDevicesDiscovery({
							success: (res) => {
								console.log('搜索设备---', res)
								uni.onBluetoothDeviceFound((devices) => {
									if (devices.devices[0].name) {
										if (this.blueList.findIndex((v) => v.deviceId == 						devices.devices[0].deviceId) == -1) {
										this.blueList = this.blueList.concat(devices.devices[0])
									}
								  }
								})
							},
						})
					},
					fail: function(msg) {
						uni.$u.toast('请先打开蓝牙')
					}
				 })
			   },
			   // 打开安卓经典蓝牙
				handleAndroidBlue() {
					this.blueList = []
					blueModule.openBT((ret) => {
						blueModule.searchBT((ret1) => {
							const temp = ret1.data
							if (temp && temp.name) {
								if (this.blueList.findIndex((v) => v.address == temp.address) == -1) {
									this.blueList = this.blueList.concat(temp)
								}
							}
						});
					});
				},
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值