收藏
回答

微信小程序 BLE 外设模式在安卓上的连接问题:首次连接成功,后续连接被拒绝

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug 蓝牙BLE 微信安卓客户端 8.0.69 3.15.0

问题现象 :

  • 安卓手机 :小程序作为BLE外设时,允许第一次连接,但后续的连接尝试被拒绝
  • 苹果手机 :正常,没有这个问题
  • 对比测试 :安卓手机使用第三方BLE工具进行广播时,可以正常接受重复连接

具体表现 :

  • 首次连接:PC或其他设备可以成功连接到小程序外设
  • 断开后重连:再次尝试连接时被拒绝,连接失败

希望微信小程序团队能关注这个安卓特有的BLE外设连接问题,提供:

  • 更稳定的API实现 :优化安卓端的BLE外设模式实现
  • 更详细的文档 :提供BLE外设模式的最佳实践
  • 连接状态管理 :增加明确的连接状态回调
// 1. 初始化蓝牙(外设模式)
wx.openBluetoothAdapter({
  mode: 'peripheral',
  success: () => {
    // 2. 创建外设服务端
    wx.createBLEPeripheralServer({
      success: (res) => {
        const server = res.server;
        
        // 3. 设置回调
        server.onCharacteristicWriteRequest((res) => {
          server.writeCharacteristicValue({
            serviceId: res.serviceId,
            characteristicId: res.characteristicId,
            value: new ArrayBuffer(0),
            needNotify: false,
            deviceId: res.deviceId
          });
        });
        
        server.onCharacteristicReadRequest((res) => {
          server.writeCharacteristicValue({
            serviceId: res.serviceId,
            characteristicId: res.characteristicId,
            value: new ArrayBuffer(0),
            needNotify: false,
            deviceId: res.deviceId
          });
        });
        
        // 4. 添加服务
        server.addService({
          service: {
            uuid: 'FFF0', // 16位短UUID
            characteristics: [
              { 
                uuid: 'FFF1', 
                properties: { read: true, write: true, notify: true },
                permission: { readable: true, writeable: true },
                value: new ArrayBuffer(0)
              },
              { 
                uuid: 'FFF2', 
                properties: { read: true, write: true },
                permission: { readable: true, writeable: true },
                value: new ArrayBuffer(0)
              }
            ]
          },
          success: () => {
            // 5. 开始广播
            server.startAdvertising({
              advertiseRequest: {
                connectable: true,
                deviceName: 'Portal-Simple',
                serviceUuids: ['FFF0']
              },
              powerLevel: 'high'
            });
          }
        });
      }
    });
  }
});


最后一次编辑于  03-13
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    03-13
    有用
    回复 1
    • God
      God
      03-13
      在荣耀、一加手机尝试过,表现一致。
      没有调用停止广播方法
      苹果系统没有问题,那么方法是没问题的
      03-13
      回复
登录 后发表内容