0.前置准备:蓝牙功能授权
使用蓝牙相关api之前需要让用户授权蓝牙的隐私协议,代码如下:
uni.authorize({
scope: 'scope.bluetooth',
success:(res=>{
console.log('蓝牙授权成功,开始使用蓝牙相关api功能')
}),
fail:(res=>{
console.log('用户拒绝授权蓝牙功能,返回')
})
})
1.正式接入蓝牙相关api
1.1:初始化蓝牙模块(uni.openBluetoothAdapter):
所有蓝牙相关API调用必须要在uni.openBluetoothAdapter之后使用,接口调用成功会进入到success里,表示蓝牙模块初始化完成,代码如下:
uni.openBluetoothAdapter({
success(res) {
console.log(res,'初始化蓝牙成功,准备搜索附近蓝牙设备')
},
fail(err) {
console.log(err,'err初始化蓝牙fail')
}
})
1.2:搜索蓝牙三步骤
1.2.1:开始搜索附近蓝牙外围设备(uni.startBluetoothDevicesDiscovery)
1.2.2:寻找目的设备uni.onBluetoothDeviceFound)
1.2.3:停止搜索( uni.stopBluetoothDevicesDiscovery)
在蓝牙初始化成功后调用uni.startBluetoothDevicesDiscovery开启蓝牙搜索功能,搜索成功后需要调用uni.onBluetoothDeviceFound方法去寻找你想要找的设备,uni.onBluetoothDeviceFound方法调用成功后,按照我贴的代码获取蓝牙设备返回的数据段,与你们后端返回的数据进行比较,匹配成功,就调用uni.s

最低0.47元/天 解锁文章
8379

被折叠的 条评论
为什么被折叠?



