AltBeacon is stopping the beacon scan. But keep scanning

左心房为你撑大大i 提交于 2021-01-29 10:21:41

问题


It is designed to scan every 4 seconds. However, beacons may not be detected from the start to the stop of scanning.
After that, the beacon is detected after stopping the scan.

The problem occurs at the following dates and times in the log.
08:54:20.807 D/CycledLeScanner(21211): Scan started
08:54:21.906 D/CycledLeScanner(21211): Done with scan cycle
08:54:22.336 D/Cycled LeScanner(21211): stopping bluetooth le scan
08:54:23.638 D/CycledLeScannerForLollipop(21211): got record

And after this problem occurs, scanning is always done.
This problem turns off Bluetooth on the device. Or it will be solved if you quit the application. This problem occurred when I kept the program running for 12 hours.
It seems to occur rarely, but I don't know what triggered it.
Can anyone help me with how to solve this problem?
The terminal information, source code and logs are listed below.

arrows M03
Android OS 6.0
'org.altbeacon:android-beacon-library:2.16.2'

MainActivity

public class MainActivity extends AppCompatActivity implements BeaconConsumer {

private BeaconManager beaconManager;
Region mRegion;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.getBeaconParsers().remove(0);
    beaconManager.setForegroundBetweenScanPeriod(4000);

    mRegion = new Region("ibeacon", null, null, null);

    Button btnFore = (Button) findViewById(R.id.btnFore);
    btnFore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            beaconManager.bind(MainActivity.this);
            beaconManager.setDebug(true);
        }
    });

    thdWriteLog l_thdWriteLog = new thdWriteLog();
    l_thdWriteLog.start();
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {}

        @Override
        public void didExitRegion(Region region) {}

        @Override
        public void didDetermineStateForRegion(int i, Region region) {}
    });

    try {
        beaconManager.startMonitoringBeaconsInRegion(mRegion);
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {}
    });
}

Output Log Program

public class thdWriteLog extends Thread {

@Override
public void run() {
    java.lang.Process proc = null;
    BufferedReader reader = null;
    PrintWriter writer = null;

    final String pId =  Integer.toString(android.os.Process.myPid());

    try {
        proc = Runtime.getRuntime().exec(new String[] { "logcat", "-v", "time"});
        reader = new BufferedReader(new InputStreamReader(proc.getInputStream()), 1024);
        String line;

        while ( true ) {

            line = reader.readLine();
            if (line.length() == 0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                }
                continue;
            }

            if (line.indexOf(pId) != -1) {

                try {
                    OutputStream out;
                    File l_file = new File(Environment.getExternalStorageDirectory().getPath() + "/log.text");

                    out = new FileOutputStream(l_file, true);
                    writer = new PrintWriter(new OutputStreamWriter(out,"UTF-8"));

                    String l_time = line.substring(0, 18);
                    String l_logType = line.substring(19, 21);
                    String l_logStr1 = line.substring(21, line.indexOf("):") + 2);
                    String l_logStr2 = line.substring(line.indexOf("):") + 3);
                    String l_logStr = l_time + ',' + l_logType + ',' + l_logStr1 + ',' + l_logStr2;

                    writer.println(l_logStr);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (writer != null) {
                        writer.close();
                    }
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Log

08:54:16.812 D/CycledLeScanner(21211): Normalizing between scan period from 4000 to 3987
08:54:16.812 D/CycledLeScannerForLollipop(21211): This is Android L but we last saw a beacon only 10 ago so we will not keep scanning in background.
08:54:16.812 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 3987 milliseconds
08:54:16.812 D/CycledLeScannerForLollipop(21211): Stopping LE scan on scan handler
08:54:16.814 D/BluetoothAdapter(21211): STATE_ON
08:54:17.815 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 2984 milliseconds
08:54:18.818 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 1981 milliseconds
08:54:19.820 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 979 milliseconds
08:54:20.801 D/CycledLeScanner(21211): starting a new scan cycle
08:54:20.803 D/CycledLeScanner(21211): starting a new bluetooth le scan
08:54:20.805 D/CycledLeScannerForLollipop(21211): starting a scan in SCAN_MODE_LOW_LATENCY
08:54:20.805 D/CycledLeScannerForLollipop(21211): Using no scan filter since this is pre-8.1
08:54:20.806 D/BluetoothAdapter(21211): STATE_ON
08:54:20.807 D/CycledLeScanner(21211): Waiting to stop scan cycle for another 1100 milliseconds
08:54:20.807 D/CycledLeScanner(21211): Scan started
08:54:21.810 D/CycledLeScanner(21211): Waiting to stop scan cycle for another 95 milliseconds
08:54:21.906 D/CycledLeScanner(21211): Done with scan cycle
08:54:21.906 D/ScanHelper(21211): Beacon simulator not enabled
08:54:22.336 D/CycledLeScanner(21211): stopping bluetooth le scan
08:54:22.336 D/CycledLeScannerForLollipop(21211): Stopping scan
08:54:22.339 D/CycledLeScanner(21211): Normalizing between scan period from 4000 to 3560
08:54:22.339 D/CycledLeScannerForLollipop(21211): This is Android L but we last saw a beacon only 5537 ago so we will not keep scanning in background.
08:54:22.342 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 3560 milliseconds
08:54:22.813 D/CycledLeScannerForLollipop(21211): Stopping LE scan on scan handler
08:54:22.814 D/BluetoothAdapter(21211): STATE_ON
08:54:22.814 D/BluetoothLeScanner(21211): could not find callback wrapper
08:54:23.430 E/ CycledLeScannerForLollipop(21211): Scan failed: app cannot be registered
08:54:23.430 D/CycledLeScannerForLollipop(21211): Waiting to start full Bluetooth scan for another 2469 milliseconds
08:54:23.468 D/BluetoothLeScanner(21211): onClientRegistered() - status=0 clientIf=7
08:54:23.638 D/CycledLeScannerForLollipop(21211): got record
08:54:23.643 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.643 D/BeaconParser(21211): Processing pdu type FF: 02011a0bff4c0009060301c0a8000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 14
08:54:23.643 D/BeaconParser(21211): This is not a matching Beacon advertisement. (Was expecting 02 15. The bytes I see are: 02011a0bff4c0009060301c0a8000f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
08:54:23.669 D/CycledLeScannerForLollipop(21211): got record
08:54:23.670 D/BeaconParser(21211): Processing pdu type FF: 1eff060001092002d9da8d7c318ba216cf4f0f1840361ab991a8f9fe57a93c00000000000000000000000000000000000000000000000000000000000000 with startIndex: 2 endIndex: 30
08:54:23.670 D/BeaconParser(21211): This is not a matching Beacon advertisement. (Was expecting 02 15. The bytes I see are: 1eff060001092002d9da8d7c318ba216cf4f0f1840361ab991a8f9fe57a93c00000000000000000000000000000000000000000000000000000000000000
08:54:23.677 D/CycledLeScannerForLollipop(21211): got record
08:54:23.678 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.678 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29
08:54:23.679 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.679 D/BeaconParser(21211): Bytes are: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000
08:54:23.691 D/ScanHelper(21211): Beacon packet detected for: id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -62
08:54:23.695 D/CycledLeScannerForLollipop(21211): got record
08:54:23.695 D/ScanHelper(21211): beacon detected : id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.696 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.699 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215d9407f30f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29
08:54:23.703 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.706 D/BeaconParser(21211): Bytes are: 0201061aff4c000215d9407f30f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000
08:54:23.719 D/CycledLeScannerForLollipop(21211): got record
08:54:23.720 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.720 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215b9407f30f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29
08:54:23.720 D/ScanHelper(21211): Beacon packet detected for: id1: d9407f30-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -64
08:54:23.720 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.720 D/ScanHelper(21211): beacon detected : id1: d9407f30-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.720 D/BeaconParser(21211): Bytes are: 0201061aff4c000215b9407f30f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000
08:54:23.720 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.724 D/ScanHelper(21211): Beacon packet detected for: id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -60
08:54:23.724 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.724 D/ScanHelper(21211): beacon detected : id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.724 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.778 D/CycledLeScannerForLollipop(21211): got record
08:54:23.779 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.779 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29
08:54:23.779 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.780 D/BeaconParser(21211): Bytes are: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000
08:54:23.784 D/ScanHelper(21211): Beacon packet detected for: id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -72
08:54:23.784 D/ScanHelper(21211): beacon detected : id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.785 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.797 D/CycledLeScannerForLollipop(21211): got record
08:54:23.798 D/BeaconParser(21211): Processing pdu type FF: 1aff4c000215b9407f91f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000000000 with startIndex: 2 endIndex: 26
08:54:23.798 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.798 D/BeaconParser(21211): Bytes are: 1aff4c000215b9407f91f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000000000
08:54:23.801 D/ScanHelper(21211): Beacon packet detected for: id1: b9407f91-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -71
08:54:23.801 D/ScanHelper(21211): beacon detected : id1: b9407f91-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.802 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.888 D/CycledLeScannerForLollipop(21211): got record
08:54:23.892 D/BeaconParser(21211): Processing pdu type FF: 1eff0600010920029291ed7a8667fafff88483e841cb14ca9a67f8eb84aaa200000000000000000000000000000000000000000000000000000000000000 with startIndex: 2 endIndex: 30
08:54:23.892 D/BeaconParser(21211): This is not a matching Beacon advertisement. (Was expecting 02 15. The bytes I see are: 1eff0600010920029291ed7a8667fafff88483e841cb14ca9a67f8eb84aaa200000000000000000000000000000000000000000000000000000000000000
08:54:23.965 D/CycledLeScannerForLollipop(21211): got record
08:54:23.966 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.966 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29
08:54:23.966 D/BeaconParser(21211): This is a recognized beacon advertisement -- 02 15 seen
08:54:23.966 D/BeaconParser(21211): Bytes are: 0201061aff4c000215b9407f90f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000
08:54:23.977 D/ScanHelper(21211): Beacon packet detected for: id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0 with rssi -72
08:54:23.977 D/ScanHelper(21211): beacon detected : id1: b9407f90-f5f8-466e-aff9-25556b57fe6d id2: 0 id3: 0
08:54:23.977 D/ScanHelper(21211): looking for ranging region matches for this beacon
08:54:23.994 D/CycledLeScannerForLollipop(21211): got record
08:54:23.997 D/BeaconParser(21211): Ignoring pdu type 01
08:54:23.997 D/BeaconParser(21211): Processing pdu type FF: 0201061aff4c000215e9407f30f5f8466eaff925556b57fe6d00000000c40000000000000000000000000000000000000000000000000000000000000000 with startIndex: 5 endIndex: 29


回答1:


In the case you show in the log, the command to the underlying Android APIs to stop the scan failed:

08:54:22.813 D/CycledLeScannerForLollipop(21211): Stopping LE scan on scan handler
08:54:22.814 D/BluetoothAdapter(21211): STATE_ON
08:54:22.814 D/BluetoothLeScanner(21211): could not find callback wrapper
08:54:23.430 E/ CycledLeScannerForLollipop(21211): Scan failed: app cannot be registered

The last line above where it says "Scan failed", this actually refers to the attempt to stop the scan.

This is most certainly a problem inside the bluetooth stack on the test device. Since the interface between the Android bluetooth stack and the underlying bluetooth chip always involve some proprietary customizations by the OEM, it is likely that this is due to some bug in that implementation.

Since you can't fix the bluetooth stack, your best bet is to try to find some kind of workaround to regain control of scans. This will undoubtedly require trial and error. You might try disabling the Android Beacon Library's scanning service then restarting it, by stopping all monitoring (and ranging if being done), waiting 30 seconds and then restarting monitoring (and ranging if desired).



来源:https://stackoverflow.com/questions/65284689/altbeacon-is-stopping-the-beacon-scan-but-keep-scanning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!