ligi
ligi

Reputation: 39519

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

Upvotes: 0

Views: 867

Answers (1)

Doug Koellmer
Doug Koellmer

Reputation: 427

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().

Upvotes: 2

Related Questions