简体   繁体   中英

Continuously scan for BLE-MAC

Is there a library to do background scanning for Bluetooth-Low-Energy devices with a specific MAC? I know altbeacon does this but seems only to work for beacon type BLE devices - not other types. Or is there a way to leverage altbeacon for this? I just want to detect if a certain device is on and in range - as far as I understand this should be possible similar to the beacons - I am just not filtering for some data in the advertisment - only for the mac. I could implement this but do not want to reinvent the wheel

To give you a robust solution that consumes low amounts of energy and works across all OS versions with their different scanning APIs would be literally hundreds of lines of complex code. I know because I've done it, see https://github.com/iDevicesInc/SweetBlue/blob/master/src/com/idevicesinc/sweetblue/P_Task_Scan.java for a small taste of what you have to handle.

So if you're in proof-of-concept phase and just want something quick, try the following in your Activity class using SweetBlue :

// Wake lock might not be needed for your application, up to you.
BleManager.get(this).pushWakeLock();

BleManager.get(this).setConfig(new BleManagerConfig()
{{
        scanMode = BleScanMode.LOW_POWER;
}});

BleManager.get(this).startPeriodicScan(Interval.FIVE_SECS, Interval.FIVE_SECS, new ScanFilter()
{
    @Override public Please onEvent(ScanEvent e)
    {
        return Please.acknowledgeIf(e.macAddress().equals("DE:CA:FF:C0:FF:EE"));
    }
});

This will give you scanning for five seconds on, five seconds off, repeating until you call stopPeriodicScan() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM