wx.getBluetoothDevices获取不到已连接蓝牙设备
发布于 6 年前 作者 na28 17391 次浏览 来自 问答
  • 当前 Bug 的表现(可附上截图):wx.getBluetoothDevices无法获取本机已连接设备,在安卓下无法获取,ios下可以获取。
  • 预期表现:可获取本机连接蓝牙设备。

可使用代码片段引用代码:wechatide://minicode/JsbAdsmb7P3Y

bug复现准备:

    1.引用代码片段:wechatide://minicode/JsbAdsmb7P3Y

    2.连接一个低功耗蓝牙设备

    3.使用安卓手机开启真机调试

    4.结果:wx.getBluetoothDevices,无法获取到该蓝牙设备。(注:官方文档说明__获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。__)

    5.断开蓝牙设备连接

    6.再次开启真机调试,可以搜索到该蓝牙设备

2 回复

index.js

const app = getApp()

Page({

data: {

list: []

},

startConnect() {

// 开始初始化蓝牙

wx.openBluetoothAdapter({

success: (res) => {

console.log(’-----蓝牙初始化成功-----’)

this.getBluetoothAdapterState()

},

fail: () => {

console.log(’--------蓝牙初始化失败--------’)

}

})

wx.onBluetoothAdapterStateChange(({ available }) => {

// 监听蓝牙状态

if (available) {

this.getBluetoothAdapterState()

}

})

},

getBluetoothAdapterState() {

// 获取本机蓝牙适配器状态

wx.getBluetoothAdapterState({

success: ({ discovering, available }) => {

if (available) {

this.startBluetoothDevicesDiscovery()

} else {

wx.showToast({

title: ‘设备未开启蓝牙连接’,

icon: ‘none’,

duration: 2000

})

}

}

})

},

startBluetoothDevicesDiscovery() {

// 开始搜寻附近的蓝牙外围设备

wx.showLoading({

‘title’: ‘正在搜索蓝牙…’

})

wx.startBluetoothDevicesDiscovery({

services: [],

success: ({ isDiscovering }) => {

if (isDiscovering) {

this.onBluetoothDeviceFound()

} else {

this.getBluetoothAdapterState()

}

},

fail: (error) => {

console.log(error, ‘startBluetoothDevicesDiscovery error info’)

}

})

},

onBluetoothDeviceFound() {

// 监听寻找到新设备的事件

wx.onBluetoothDeviceFound(({ devices }) => {

let item = devices[0],

{ name } = devices[0];

if (item && name != ‘’) {

this.getBluetoothDevices()

}

})

},

getBluetoothDevices(){

// 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。

wx.getBluetoothDevices({

success: ({ devices})=>{

let list = devices.filter((item)=>{

return item.name != ‘未知设备’

})

this.setData({

list: list

})

console.log(list,‘全部设备列表’)

},

fail:()=>{

}

})

},

onLoad: function () {

this.startConnect()

},

})

index.wxml

<view class=“intro”>

<view wx:for="{{list}}" wx:key="{{item.name}}">

{{index}}:{{item.name}}

</view>

</view>

安卓几,,在getBluetooth有扫描到设备吗??   方便贴代码吗?,

回到顶部