收藏
回答

wx.getBluetoothDevices获取不到已连接蓝牙设备

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.getBluetoothDevices 客户端 6.6.7 6.6.7.1320

- 当前 Bug 的表现(可附上截图):wx.getBluetoothDevices无法获取本机已连接设备,在安卓下无法获取,ios下可以获取。


- 预期表现:可获取本机连接蓝牙设备。


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


bug复现准备:

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

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

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

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

    5.断开蓝牙设备连接

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


最后一次编辑于  2018-10-26
回答关注问题邀请回答
收藏

4 个回答

  • Pow
    Pow
    2023-04-28

    开了定位就有了,焯

    2023-04-28
    有用 1
    回复 1
    • Pow
      Pow
      2023-04-28
      这接口有问题啊,小程序机没有定位啊,这怎么弄
      2023-04-28
      1
      回复
  • 禾店短剧系统
    禾店短剧系统
    2021-06-02

    wx.createBLEConnection(Object object)//先连接蓝牙

    wx.getBLEDeviceServices(Object object)//再去获取服务的uuid

    2021-06-02
    有用 1
    回复
  • Sung
    Sung
    2018-10-26

    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>


    2018-10-26
    有用
    回复
  • ==
    ==
    2018-10-26

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

    2018-10-26
    有用
    回复 14
    • Sung
      Sung
      2018-10-26

      安卓6.0,可以扫描到其他设备,未连接前可以获取该设备,连接之后获取不到。

      2018-10-26
      回复
    • ==
      ==
      2018-10-26回复Sung

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

      let item = devices[0],

      { name } = devices[0];


      if (item && name != '') {

      this.getBluetoothDevices()

      }

      })

      },

      你这样写,它会扫到一个新设备去获取一次,不会很耗性能吗??

      连接之后   其他蓝牙设备能获取  还是  全都获取不到了


      2018-10-26
      回复
    • Sung
      Sung
      2018-10-26回复==

      是的,现在还没考虑这些性能问题,后期会修改。其他蓝牙设备可以获取,只有已连接设备获取不到。未连接时可以获取到该设备,连接蓝牙设备后,getBluetoothDevices 就获取不到了。


      2018-10-26
      回复
    • ==
      ==
      2018-10-26回复Sung

      打开定位了吗??

      2018-10-26
      回复
    • Sung
      Sung
      2018-10-26回复==

      定位?GPS?蓝牙是打开的。

      2018-10-26
      回复
    查看更多(9)
登录 后发表内容