收藏
回答

鸿蒙next调用wx.closeBluetoothAdapter()返回成功,但蓝牙仍连接

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.closeBLEConnection,wx.closeBluetoothAdapter 鸿蒙客户端 1.0.10 3.8.9
wx.offBLECharacteristicValueChange()
        // 移除连接状态监听
        wx.offBLEConnectionStateChange();
        // 移除设备发现监听
        wx.offBluetoothDeviceFound();


       // await this.closeBLEConnection()
        await this.closeConnectionWithRetry(yourDeviceId).then(() => {
            console.log('处理完成');
        }).catch(err => {
            console.error('最终关闭失败', err);
        });
        await this.closeConnectionWithRetry(this.mac);
        // 设备连接状态 - 小程序端
        wx.getConnectedBluetoothDevices({
            success: (res) => {
              if (res.devices.length > 0) {
                console.log("仍有设备连接:", res.devices);
                // 尝试逐个关闭
                res.devices.forEach(device => {
                  wx.closeBLEConnection({ deviceId: device.deviceId });
                });
              } else {
                console.log("无设备连接,但系统状态仍显示连接 → 鸿蒙系统 UI 未刷新");
              }
            }
        });


        // 最终方案   1. 先关闭蓝牙适配器
        wx.closeBluetoothAdapter({
            success: () => {
                console.log("尝试关闭蓝牙适配器");
                // 2. 延迟 2 秒后重新打开
                setTimeout(() => {
                    wx.openBluetoothAdapter({
                        success: () => {
                            console.log("重新打开蓝牙适配器,强制刷新状态");
                        },
                        fail: (err) => {
                            console.error("重新打开蓝牙失败:", err);
                        }
                    });
                }, 2000);
            },
            fail: (err) => {
                console.error("关闭蓝牙适配器失败:", err);
            }
        });
async closeConnectionWithRetry(deviceId, retryCount = 3) {
        console.log('closeConnectionWithRetry关闭',deviceId, retryCount);
        return new Promise((resolve, reject) => {
            wx.closeBLEConnection({
            deviceId,
            success: (res) => {
                console.log('蓝牙连接已关闭',deviceId);
                // 关键:等待2秒让系统底层处理
                setTimeout(() => {
                    // 可选:再次检查系统状态是否同步
                    wx.getBluetoothAdapterState({
                        success: (stateRes) => {
                            console.log('当前蓝牙适配器状态(小程序获取):', stateRes.discovering ? '开启' : '关闭');
                            // 即使这里显示关闭,系统状态栏可能还是延迟的
                        }
                    });
                    resolve(res);
                }, 2000); // 延迟2000毫秒
            },
            fail: (err) => {
                console.error(`关闭失败(${retryCount}次剩余):`, err.errMsg);
                if (retryCount > 0) {
                setTimeout(() => {
                    this.closeConnectionWithRetry(deviceId, retryCount - 1);
                }, 1000); // 1秒后重试
                }
                if (retryCount > 0) {
                    setTimeout(() => this.closeConnectionWithRetry(deviceId, retryCount - 1), 1000);
                } else {
                    reject(err);
                }
            }
            });
        });
    }


提示处理成功,无设备连接,蓝牙状态已关闭,关闭蓝牙适配器正常,重启蓝牙适配器也正常

但是蓝牙状态依旧是连接中

日志如下:


回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员--Asher
    社区技术运营专员--Asher
    2025-09-18

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2025-09-18
    有用
    回复 1
    • 名字重要吗
      名字重要吗
      2025-09-18
      非常感谢,这是代码片段,里面有搜索设备,连接设备,获取设备Mac地址后,断开蓝牙设备。在调用小程序的断开接口后,蓝牙设备依旧处于连接状态,获取当前连接设备的接口返回是没有连接。目前测试退出微信程序时蓝牙会断开连接(退出小程序的时候蓝牙还在连接中)。
      https://developers.weixin.qq.com/s/6ISp1fmP8b3l
      2025-09-18
      回复
登录 后发表内容