gpt4 book ai didi

android - 多次调用 startLeScan 方法不能正确扫描 BLE 设备

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:13 26 4
gpt4 key购买 nike

我试图在我的 Android 应用程序中多次调用 startLeScan 方法,它第一次扫描 BLE 设备,从第二次开始它无法扫描设备。我在我的应用程序中进行了循环扫描,每 10 秒停止一次扫描并重新开始。这里有一些日志显示了我的应用程序的行为方式。

05-20 00:45:31.146  15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.147 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.160 15841-15852/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=133 clientIf=0
05-20 00:45:31.161 15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.165 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.169 15841-15909/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=7
05-20 00:45:31.171 15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.175 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.179 15841-15853/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=8
05-20 00:45:31.181 15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.185 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.189 15841-18615/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=6
05-20 00:45:31.191 15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.192 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.202 15841-15852/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=133 clientIf=0
05-20 00:45:31.203 15841-15841/XXXXX D/BluetoothAdapter﹕ stopLeScan()
05-20 00:45:31.207 15841-15841/XXXXX D/BluetoothAdapter﹕ startLeScan(): null
05-20 00:45:31.211 15841-15909/XXXXX D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=10

我的 BluetoothLEScannerForMR2.java 中有这些函数调用,当我多次调用 startScan 方法时,我得到了上述日志。

protected void startScan() {
if (getBluetoothAdapter() != null) {
if (getBluetoothAdapter().isEnabled()) {
getBluetoothAdapter().startLeScan(mLeScanCallback);
mScanStopTime = new Date().getTime() + mScanPeriod;

mScanning = true;

handleIntervalScanning();
}
}
}

protected void stopScan() {
if (getBluetoothAdapter() != null) {
if (getBluetoothAdapter().isEnabled()) {
getBluetoothAdapter().stopLeScan(mLeScanCallback);
mScanning = false;
}
}
}

private void handleIntervalScanning() {
long msUntilNextStop = mScanStopTime - (new Date().getTime());

if (msUntilNextStop > mScanPeriod)
msUntilNextStop = mScanPeriod;

if (msUntilNextStop > 0) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
handleIntervalScanning();
}
}, msUntilNextStop);
} else {
if (getBluetoothAdapter() != null) {
if (getBluetoothAdapter().isEnabled()) {
if (mScanning) {
getBluetoothAdapter().stopLeScan(mLeScanCallback);

startScan();
}
}
}
}
}

最佳答案

这是一篇旧帖子,但我想我会发表评论以防其他人需要帮助。在您的代码中,您获得了 BlueToothAdapter 的一个实例并调用了 startscan 方法……但是您没有保留该适配器实例。然后在您的 stopscan 方法中,您创建一个蓝牙适配器的新实例并调用它的停止扫描。我认为您需要在“获取”适配器时将适配器保存到内存变量,然后使用适配器的相同实例(通过内存变量)停止扫描。

关于android - 多次调用 startLeScan 方法不能正确扫描 BLE 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30335534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com