Passive Read
Native Android ==> Bluetooth module
The ValidicMobile library supports reading from bluetooth peripherals without any user interaction once a device has been successfully paired.
The PassiveBluetoothManager
manages background reads and interactions with any BluetoothPeripheralController
objects in use.
Reading or pairing a peripheral in the foreground will cancel any in progress readings from the background and will restart monitoring in the background once all bluetooth interaction in the foreground has finished.
To set peripherals to be read in the background use the PassiveBluetoothManager.getInstance(context)
singleton as such:
Set<BluetoothPeripheral> peripherals = new HashSet\<>();
peripherals.add(BluetoothPeripheral.getPeripheralForID(1);
peripherals.add(BluetoothPeripheral.getPeripheralForID(2);
peripherals.add(BluetoothPeripheral.getPeripheralForID(3);
PassiveBluetoothManager.getInstance(context).setPassivePeripherals(peripherals);
To stop monitoring peripherals in the background set the background peripherals to an empty set
PassiveBluetoothManager.getInstance(context).setPassivePeripherals(Collections.emptySet());
Listener
Records will be automatically uploaded as they are read. In order to receive events from the the PassiveBluetoothManager
, use one of the following two methods. In both cases a PassiveBluetoothManager.BluetoothListener
must be created in order to receive the events:
PassiveBluetoothManager.BluetoothListener listener = new PassiveBluetoothManager.BluetoothListener() {
@Override
public void onSuccess(BluetoothPeripheral peripheral, BluetoothDevice bluetoothDevice, List<Measurement> records) {
// Records received in the background are automatically uploaded
}
@Override
public void onFail(BluetoothPeripheral peripheral, BluetoothDevice bluetoothDevice, ValidicBluetoothException exception) {
// Reading failed in the background
}
@Override
public void onPeripheralDiscovered(BluetoothPeripheral peripheral) {
// A peripheral was discovered and we have setup a connection
}
@Override
public void onBackgroundStart() {
// Background scanning has begun
}
@Override
public void onBackgroundStop() {
// Background scanning has stopped
}
Note: The PassiveBluetoothManager uses a WeakReference to manage the passive listener. Activities and Fragments may be destroyed and then recreated when coming from the Background into the foreground. It is recommended to have a singleton maintain a strong reference to your listener or to set the listener in Activity.onCreate
or Fragment.onAttach
This listener can either be set on the PassiveBluetoothManager
instance:
PassiveBluetoothManager.getInstance(context).setBluetoothListener(listener);
Passive Read Considerations
- Passive reading is not automatically restarted after device reboot. Your application must be launched or a BootReceiver registered to start passive reading.
- To prevent duplicate records passive reading will not take readings from a peripheral if that peripheral has been read from within the last 15 seconds.
There are several situations where passive bluetooth reading is cancelled and resumed at a later time:
- When the system becomes low on memory, passive reading will be cancelled. When the system frees enough memory it will relaunch passive reading and begin scanning again.
- When the end user turns off bluetooth passive reading will be cancelled and will be automatically restarted once bluetooth is turned back on.
- When passive bluetooth reading is cancelled, any current passive readings will be cancelled, and the PassiveBluetoothManager.BluetoothListener will receive onCancelled for each of the peripherals.
Updated 4 days ago