uni-app中uni.getBLEDeviceServices获取的services为空

uni-app中uni.getBLEDeviceServices获取的services为空

开发环境 版本号 项目创建方式
Mac 12.7.4 (21H1123) HBuilderX
产品分类:uniapp/App

PC开发环境操作系统:Mac

PC开发环境操作系统版本号:12.7.4 (21H1123)

HBuilderX类型:正式

HBuilderX版本号:4.08

手机系统:Android

手机系统版本号:Android 13

手机厂商:小米

手机机型:redmi k40

页面类型:vue

vue版本:vue2

打包方式:离线

项目创建方式:HBuilderX

示例代码:

```html
<template>
<view class="content">
<scroll-view scroll-y class="box">
<view class="item" :style="(index+1)!=ble_list.length?'border-bottom:1px solid #aaa':'border-bottom:0'"
v-for="(item,index) in ble_list">
<view class="item_2">
<view class="ble">  
</view>  
</view>  
<view class="item_2">  
<view>  
<text>Id: {{ item.deviceId }}</text>  
</view>  
<view>  
<text>Name: {{ item.name?item.name:item.localName }}</text>  
</view>  
</view>  
<view class="item_2">  
<h2>{{index+1}}</h2>  
</view>  
<view class="item_2">  
<!-- <button  
[@tap](/user/tap)="deviceId==item.deviceId?closeBLEConnect():createBLEConnection(item.deviceId)">{{(deviceId===item.deviceId && deviceId)?'断开':'连接'}}</button> -->  

<text class="text_on" v-if="deviceId!=item.deviceId"  
[@tap](/user/tap)="createBLEConnection(item.deviceId)">连接</text>  
<text class="text_off" v-if="deviceId==item.deviceId"  
[@tap](/user/tap)="closeBLEConnect(item.deviceId)">断开</text>  
</view>  
<br>  
</view>  
</scroll-view><button type="warn" [@click](/user/click)="openBluetoothAdapter">重新搜索附近蓝牙设备</button>  
<view class="send_view">  
<input type="text" placeholder="请输入要发送至蓝牙设备的内容" v-model="to_ble_text"><button type="primary"  
[@tap](/user/tap)="writeBLECharacteristicValue(to_ble_text)">发送</button>  
</view>  

<view class="msg_x">  
蓝牙传来的内容:  
<view class="msg_txt" :style="(index+1)!=ble_list.length?'border-bottom:1px solid #aaa':'border-bottom:0'"  
v-for="(item,index) in on_ble_text">  
<h3>{{index+1}}</h3>  
<p>时间:{{item.time}}</p>  
<p>内容:<strong>{{item.text}}</strong></p>  
</view>  
</view>  

操作步骤: 连接之后就会出现

预期结果: 希望获取到services然后接收到蓝牙的数据

实际结果: services为[],无法接收到蓝牙的数据

bug描述: 蓝牙连接后 uni.getBLEDeviceServices 获取的services始终为空,延迟了50秒都不行,使用官方的测试工具nrf connect工具没有问题,可以获取到6个services


更多关于uni-app中uni.getBLEDeviceServices获取的services为空的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

遇到了同样的问题,用微信小程序的文档同样的方法就能获取到serviceid,uniapp就为空

更多关于uni-app中uni.getBLEDeviceServices获取的services为空的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这个问题关注两年了,还没解决

在 Uni-App 中使用 uni.getBLEDeviceServices 获取蓝牙设备的服务时,如果返回的 services 为空,可能是由以下几个原因导致的。你可以按照以下步骤进行排查和解决:


1. 设备未连接

  • uni.getBLEDeviceServices 需要在设备连接成功后调用。如果设备未连接或连接失败,services 会为空。
  • 解决方法:确保设备已经通过 uni.createBLEConnection 成功连接。可以通过监听 onBLEConnectionStateChange 事件来确认连接状态。
uni.createBLEConnection({
  deviceId: deviceId,
  success: (res) => {
    console.log('设备连接成功', res);
    uni.getBLEDeviceServices({
      deviceId: deviceId,
      success: (servicesRes) => {
        console.log('获取服务成功', servicesRes.services);
      },
      fail: (err) => {
        console.log('获取服务失败', err);
      }
    });
  },
  fail: (err) => {
    console.log('设备连接失败', err);
  }
});

2. 设备不支持服务发现

  • 某些蓝牙设备可能没有公开任何服务,或者设备不支持标准的 BLE 服务发现协议。
  • 解决方法:检查设备的蓝牙协议和文档,确认设备是否提供了可用的服务。

3. 获取服务时机不正确

  • 在设备连接后,蓝牙服务可能不会立即可用,需要等待一段时间才能获取。
  • 解决方法:在连接成功后,延迟一段时间再调用 uni.getBLEDeviceServices
uni.createBLEConnection({
  deviceId: deviceId,
  success: (res) => {
    console.log('设备连接成功', res);
    setTimeout(() => {
      uni.getBLEDeviceServices({
        deviceId: deviceId,
        success: (servicesRes) => {
          console.log('获取服务成功', servicesRes.services);
        },
        fail: (err) => {
          console.log('获取服务失败', err);
        }
      });
    }, 500); // 延迟 500ms
  },
  fail: (err) => {
    console.log('设备连接失败', err);
  }
});

4. 设备 ID 错误

  • deviceId 是调用 uni.getBLEDeviceServices 的必要参数,如果传入的 deviceId 错误或为空,会导致获取服务失败。
  • 解决方法:确保 deviceId 是从 uni.onBluetoothDeviceFounduni.getBluetoothDevices 中获取的正确值。

5. 平台兼容性问题

  • Uni-App 的蓝牙 API 底层依赖于各平台的原生实现,不同平台(如 iOS 和 Android)可能会有不同的行为。
  • 解决方法:检查目标平台的兼容性,并参考官方文档或社区解决方案。

6. 蓝牙权限问题

  • 在某些平台上,可能需要用户授权蓝牙权限才能正常使用蓝牙功能。
  • 解决方法:检查并确保应用已经获取了必要的蓝牙权限。

7. 设备缓存问题

  • 在某些情况下,设备缓存可能导致服务获取失败。
  • 解决方法:尝试关闭蓝牙并重新打开,或者重启设备后再次尝试。

8. 调试和日志

  • 如果以上方法都无法解决问题,可以通过调试和日志进一步分析。
  • 解决方法:在关键步骤中添加日志,确认设备连接、服务获取等流程是否正常执行。
uni.onBLEConnectionStateChange((res) => {
  console.log('蓝牙连接状态变化', res);
});
回到顶部