记录uniapp 连接蓝牙外设踩过的一些坑

本文记录了使用uniapp连接蓝牙设备时遇到的两个主要问题:一是延迟获取serverID来解决无法获取serverUUid和蓝牙特征uuid的问题;二是调试过程中发现蓝牙设备返回的数据为二进制,通过ab2hex方法将其转换为十六进制字符串进行打印。通过这些经验分享,有助于开发者更好地理解和处理uniapp蓝牙连接及数据解析的难题。
摘要由CSDN通过智能技术生成

第一个坑就是连接蓝牙设备之后无法获取到serverUUid和蓝牙特征uuid 的坑

解决办法加个定时器延迟获取serverID 就可以获取

第二个坑就是调试的时候一直是按照我们前端思路去调试回调的数据value一直都是空的

就一直认为为什么没有回调数据实际蓝牙设备返回的是二进制数据根本无法打印出来

将二进制数据转换一下打印即可

            ab2hex(buffer) {
              const hexArr = Array.prototype.map.call(
                new Uint8Array(buffer),
                function (bit) {
                  return ('00' + bit.toString(16)).slice(-2)
                }
              )
              return hexArr.join('')
            },

下面是连接蓝牙设备的全部代码

<template>
	<view class="content-box">
		<top-bar></top-bar>
		<view class="data-box">
			<view class="img-box flex-wrap justify-content-center">
				<image src="/static/images/lianjie.png" mode=""></image>
			</view>
			<view class="ft-48 ft-weight-900 text-align-center m-t-52">
				PAIRING
			</view>
			<view class="ft-32 width-100 text-align-center">
				<view class="width-100">Please long press on theGREENHILL Robot</view> 
				<view class="width-100">button to turn it on. Then pressonce again</view> 
				<view class="width-100 m-t-48">the button to pair yourRobot</view>
			</view>
			<view class="width-100 text-align-center" style=""></view>
			<view class="width-100 text-align-center m-t-52">
				<view class="iconfont icon-xiala2-01 ft-color-grey"></view>
			</view>
			<view class="width-100 text-align-center ft-36 ft-weight-700 m-t-52">
				MANUAL PAIRING
			</view>
			<view class="pd-f-r-40 width-100 flex-nowrap align-items-center m-t-24">
				<view>SEARCHING PIQ ROBOT</view>
				<view class="m-lt-auto iconfont icon-jiazai ft-color-grey"></view>
			</view>
			<view class="ft-32 pd-f-r-40 m-t-24">
				Ensure yourGREENHILL is turned ON and
				thatyour phone Bluetooth is ON. No need
				to enterany code for pairing, just select
				your Robot inthe list below.
			</view>
			<view></view>
			<view style="height: 84upx;line-height: 60upx;" class="pd-f-r-40 border-bt12 ft-28 ft-color-grey">
				Discovery equipment
			</view>
			<view class="pd-f-r-36">
				<view class="">
					蓝牙设备服务uuid
				</view>
				<view v-for="item in serverList" :key="item.uuid" class="">
					<view class="ft-color-red">
						 uuid : {
  {item.uui
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值