//蓝牙识别
configBlueTooth() {
let _this = this
this.getBluetoothAdapterState().then(res => {
if (res === 'ok') {
//开启设备搜索
this.startBluetoothDevicesDiscovery().then(res1 => {
if (res1 === 'ok') {
this.getBluetooth()
}
})
}else {
//开启蓝牙模块
_this.openBluetoothAdapter().then(res2 => {
if (res2 === 'ok') {
//开启设备搜索
this.startBluetoothDevicesDiscovery().then(res1 => {
if (res1 === 'ok') {
this.getBluetooth()
}
})
}else {
// console.log('蓝牙初始化失败')
}
})
}
})
},
//获取蓝牙设备
getBluetooth() {
let _this = this
//监听寻找到新设备的事件
uni.onBluetoothDeviceFound(function (devices) {
if(devices.devices[0].name){
_this.blueTooth.push(devices.devices[0])
console.log(_this.blueTooth)
}
})
},
async openBluetoothAdapter(){
let _this = this
let result = new Promise( (resolve, reject) => {
uni.openBluetoothAdapter({
success(res) {
resolve('ok')
},
fail(res) {
resolve(res)
},
})
})
return result
},
async getBluetoothAdapterState(){
let _this = this
let result = new Promise( (resolve, reject) => {
uni.getBluetoothAdapterState({
success(res) {
resolve('ok')
},
fail(res) {
resolve(res)
},
})
})
return result
},
async startBluetoothDevicesDiscovery(){
let _this = this
let result = new Promise( (resolve, reject) => {
uni.startBluetoothDevicesDiscovery({
success(res) {
resolve('ok')
},
fail(res) {
resolve(res)
},
})
})
return result
},
2 个回复
DCloud_iOS_WZT
提供一个示例吧
DCloud_iOS_WZT
startBluetoothDevicesDiscovery 允许重复获取试一下
5***@qq.com (作者)
可以了,谢谢
2023-12-19 09:27
Azikou
回复 5***@qq.com: 咋整的,指教下,我也不行,就正常的获取广播信息,然后拿不到serviceData
2024-01-05 10:43
9***@qq.com
回复 Azikou: 你好你解决这个问题了吗?我这ios也拿不到serviceData
2024-05-10 14:57