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

HarmonyOS鸿蒙NEXT中调用wx.closeBluetoothAdapter()返回成功,但蓝牙仍连接 在微信小程序中调用 wx.closeBluetoothAdapter 返回成功,蓝牙依旧是连接状态

微信版本:1.0.10

async closeConnect() {
    var yourDeviceId = this.ohosDeviceUuid || this.iosDeviceUuid || this.deviceId
    console.log('设备即将断开', yourDeviceId, this.mac);
    // wx.offBLECharacteristicValueChange()
    // this.offNotifyCharacteristic = true;
    // wx.offBLEConnectionStateChange(this.listener)

    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);
            }
        }
        });
    });
}
提示处理成功,无设备连接,蓝牙状态已关闭,关闭蓝牙适配器正常,重启蓝牙适配器也正常

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

日志如下:https://alliance-communityfile-drcn.dbankcdn.com/FileServer/getFile/cmtybbs/862/343/170/0010086000862343170.20250917121606.20770490408113890898355281060268:50001231000000:2800:16792B09FB9E34E72E69214ACA30537473BDEC4B733806946FDD1A0665EF157D.jpg


更多关于HarmonyOS鸿蒙NEXT中调用wx.closeBluetoothAdapter()返回成功,但蓝牙仍连接的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

开发者您好,这个接口是三方提供的,为了更加高效地解决您的问题,建议您直接联系三方SDK开发者,通过其官方渠道进行沟通。

更多关于HarmonyOS鸿蒙NEXT中调用wx.closeBluetoothAdapter()返回成功,但蓝牙仍连接的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS NEXT中,调用wx.closeBluetoothAdapter()返回成功仅表示蓝牙适配器已关闭,但不会主动断开已建立的蓝牙连接。蓝牙连接状态由设备间链路维持,关闭适配器后,已配对设备可能保持连接状态。需额外调用断开连接API(如wx.closeBLEConnection())终止具体设备连接。系统设计上,适配器管理与连接控制是分离的。

在HarmonyOS NEXT中,wx.closeBluetoothAdapter()返回成功但蓝牙仍显示连接状态,这是系统UI状态更新延迟导致的常见问题。从代码分析,您已经正确实现了断开流程,包括:

  1. 移除所有蓝牙事件监听器
  2. 通过closeBLEConnection断开具体设备连接
  3. 调用closeBluetoothAdapter关闭适配器
  4. 延迟后重新打开适配器强制刷新状态

问题根源在于HarmonyOS系统状态栏的蓝牙连接状态更新机制与小程序API调用之间存在时间差。系统UI可能基于底层蓝牙服务的状态缓存,而非实时同步小程序的控制指令。

当前解决方案是合理的:通过关闭后重新打开蓝牙适配器来强制系统刷新状态。建议在重新打开适配器后,增加getBluetoothAdapterState检查实际状态,确认底层服务已正确响应。

这种状态显示延迟在跨平台蓝牙操作中较为常见,不会影响实际的蓝牙连接状态和后续操作。

回到顶部