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

时间:2015-05-19 20:28:45

标签: android bluetooth-lowenergy

我试图在我的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();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是一篇很老的帖子,但我认为如果其他人需要帮助我会发表评论。在您的代码中,您正在获取BlueToothAdapter的实例并调用startscan方法......但您不会持久保存该适配器实例。然后在您的停止扫描方法中,您创建一个蓝牙适配器的新实例并调用其停止扫描。我认为你需要将适配器保存到内存变量中才能获得"得到"适配器然后使用相同的适配器实例(通过内存变量)来停止扫描。