6***@qq.com
6***@qq.com
  • 发布:2025-10-30 10:47
  • 更新:2026-06-05 17:53
  • 阅读:174

uni.onBluetoothDeviceFound 在手机熄屏后,如何仍然在后台执行?

分类:uni-app

uni.onBluetoothDeviceFound 在手机熄屏后,如何仍然在后台执行?

2025-10-30 10:47 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

可以用 保活插件试试
插件市场: https://ext.dcloud.net.cn/search?q=%E4%BF%9D%E6%B4%BB

l***@163.com

l***@163.com

同问,在不熄屏的情况下,即使把APP切到后台,点击蓝牙搜索也不返回devices。具体就是uni.onBluetoothDeviceFound这个API,而且手机已经是在保活的情况下,手机APP并没有被杀死。下面是实例:
/**

  • 搜索蓝牙
    */
    openSearch(callback) {
    var _viewer = this;
    uni.closeBluetoothAdapter({
    success(res) {
    console.log(res)
    },
    complete: (res) => {
    //开启蓝牙并搜索
    uni.openBluetoothAdapter({
    success(res) {
    uni.startBluetoothDevicesDiscovery({
    'services': ['0000FEE7'],
    'allowDuplicatesKey':true,
    success(res) {
    uni.onBluetoothDeviceFound(function(devices) {
    console.log("devices:", devices)
    devices.devices.forEach(device => {
    if (store.state.isLimit) {
    callback({
    'code': 0,
    'data': device
    });
    } else {
    if (device.name.indexOf(
    "SGM") != -1 || device.name
    .indexOf("WY") != -1) {
    callback({
    'code': 0,
    'data': device
    });
    }
    }
    });
    });
    },
    fail(res) {
    callback({
    'code': 1,
    'message': '蓝牙开启失败,请重试'
    })
    }
    })
    },
    fail(res) {
    callback({
    'code': 1,
    'message': '请开启蓝牙'
    })
    }
    })
    },
    fail(err) {
    console.log("Err:",err)
    }
    })
    },
    @DCloud_App_Array @DCloud_heavensoft

要回复问题请先登录注册