The tracing of contacts through mobile apps became the Number One hot topic in the last few days, governements and institutes of the EU countries are still working on technical solutions to trace transmissions of SARS-CoV-2 (though a bit late for the first wave that has hit most countries worldwide). At the same time there is an intense debate about these apps in terms of data usage, privacy, etc. The apps wont stop the spread or protect the person using the app but they should help to keep the situation under control in the times to come, maybe as a permanent tool to stay for a long period. Even more important not to build a tracking tool following examples of more authoritian states, but to have a solution that protect privacy.
In this blog series, looking at the technical aspects, we still touch both tracing and tracking for the matter of the discussion. In the last post we only touched the Bluetooth basics, now get into discovering nearby devices.
Android to discover Bluetooth devices
About device discovery
- Discovery of Bluetooth devices is the step before pairing and coummunicating with another device. We can scan for nearby devices without the other devices (its owner) noticing it.
- But for classic Bluetooth, the device need to be set to discoverable by its user, usually only for a limited period. It is consuming additional energy and would drain the battery faster if left on permanently (putting aside security concerns, see references).
- BLE works like a beacon permanently being discoverable, certain location type application work like this, eg. to help navigate in buildings equipped with beacons.
- The 3 key device attributes when discovering devices:
– Name: Not unique, just a label, can be set/changed by the user.
– MAC: The unique identifier (see previous post)
– Signal strength in dBm (more about this later)
Discover classic Bluetooth devices
We need to register a broadcast receiver and listen to the intents for discovery start and end. The discovery need to be triggered, it will run for about 12 seconds.
Register BC Receiver
private void initBCReceiver(){
final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); // dBm
System.out.println("Found: " + device.getName() + "," + device.getAddress() + "," + rssi);
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
System.out.println("ACTION_DISCOVERY_STARTED");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
System.out.println("ACTION_DISCOVERY_FINISHED");
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
}
Now trigger the discovery
bAdapter.startDiscovery();
Discover BLE devices
The BLe devices (beacons) constantly send their signal, we can pick it up in an async thread. The Android BT library supports this with less than 15 lines of code to capture the devices. Implement the callback and start/stop the scanning.
private ScanCallback leScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
System.out.println(result.getDevice().getAddress() + "-" + result.getDevice().getName() + " rssi: " + result.getRssi() + "\n");
}
};
public void startScanning(View view) {
System.out.println("start scanning");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.startScan(leScanCallback);
}
});
}
public void stopScanning(View view) {
System.out.println("stopping scanning");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
btScanner.stopScan(leScanCallback);
}
});
}
Interesting observations:
– The MS Designer Mouse is operating in both classic and BLE mode.
– The signal strength of devices can change without being physically being moved.
Conclusion
- The Bluetooth classic mode is not feasible for the tracing requirement. It would drain batteries quickly and we cant disnguish between phones and other devices using solely the MAC (though we could identify manufacturers).
- We need to consider the BLE peripheral model for our tracing app. Remember, we need to capture the unique key from another nearby user of the app, we cant achieve this without basic 2 way communication between the two apps.
Fun Facts
- Did you know that Bluetooth is named after the king Harald “Bluetooth” Gormsson of Denmark and Norway in the 10th century ? ..and why ?
- Jim Kardach (Intel), developing the new communication standard, reading Frans G. Bengtsson’s historical novel The Long Ships about Vikings and King Harald Bluetooth.
- The famous Bluetooth logo is a merge of Harald’s initials ᚼ, Hagall and ᛒ, Bjarkan. (Source: Wikipedia)
Stay safe and tuned..
References
- Forbes Article August 2019, Wired Website and Bluetooth.com
- Overview Security concerns at Wikipedia
- Bluetooth documentation developer.android.com
- Bluetooth LE documentation developer.android.com
Image by Free-Photos from Pixabay.