Bluetooth Operations
Native Android ==> Bluetooth module
Pair
Check if a BluetoothPeripheral requires pairing by calling:
BluetoothPeripheral peripheral = // ...
boolean pairBeforeRead = peripheral.requiresPairing();
If it does, the user must pair the peripheral before they can take a reading.
BluetoothPeripheral peripheral = // Choose a perpheral from the supported perpherals list
BluetoothPeripheralControllerListener listener = new BluetoothPeripheralControllerListener() {
@Override
public void onPeripheralDiscovered(@NonNull BluetoothPeripheralController controller, @NonNull BluetoothPeripheral bluetoothPeripheral, @NonNull BluetoothDevice bluetoothDevice) {
// A BluetoothDevice matching the BluetoothPeripheral has been discovered.
}
@Override
public boolean onSuccess(@NonNull BluetoothPeripheralController controller, @NonNull BluetoothPeripheral bluetoothPeripheral, @NonNull BluetoothDevice bluetoothDevice, @NonNull List<Measurement> list) {
// Pairing was successful using the BluetoothPeripheral profile.
// Some devices support previous storing historical data automatically transmit previously stored Records.
return true; // automatically submit records collected to the Validic
}
@Override
public void onFail(@NonNull BluetoothPeripheralController validicBluetoothPeripheralController, @NonNull BluetoothPeripheral bluetoothPeripheral, @Nullable BluetoothDevice bluetoothDevice, @NonNull final ValidicBluetoothException error) {
if (!isCanceling) {
instructions.post(() -> fail(error.getError()));
}
}
};
BluetoothPeripheralController controller = new BluetoothPeripheralController();
controller.pairPeripheral(peripheral, listener);
String pairingInstructions = peripheral.getPairingInstruction();
// Display the pairing Instructions to the user
Read
Once you are ready to read from a peripheral, the process is fairly similar to the pairing process. You’ll want to first show the peripheral’s instructions and eventually show the reading instructions once onPeripheralDiscovered is called.
BluetoothPeripheral peripheral = // The peripheral you want to read from
BluetoothPeripheralControllerListener listener = new BluetoothPeripheralControllerListener(){
@Override
public void onPeripheralDiscovered(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, BluetoothDevice bluetoothDevice) {
// Display the reading Instructions to the user
}
@Override
public void onFail(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, BluetoothDevice bluetoothDevice, ValidicBluetoothException exception) {
BluetoothError error = exception.getError();
switch (error) {
case BluetoothError.BluetoothTurnedOff:
Toast.makeText(getApplicationContext(), "Your bluetooth is off!", Toast.LENGTH_LONG).show();
break;
case BluetoothError.NoUser:
Toast.makeText(getApplicationContext(), "You have not started the session yet!", Toast.LENGTH_LONG).show();
break;
case BluetoothError.Cancelled:
Toast.makeText(getApplicationContext(), "Reading has been cancelled", Toast.LENGTH_LONG).show();
break;
}
Log.e("Error", error.getMessage());
}
@Override
public boolean onSuccess(final BluetoothPeripheralController peripheralController, BluetoothPeripheral peripheral, BluetoothDevice bluetoothDevice, List<Measurement> measurements) {
// Measurements were collected from a BluetoothDevice using a BluetoothPeripheral's profile
return true; // Automatically submit measurements collected to the Validic Mobile API
}
};
String instruction = peripheral.getInstruction();
// Display the instruction to the user
BluetoothPeripheralController controller = new BluetoothPeripheralController();
controller.readFromPeripheral(peripheral, listener);
Custom global error handler
If using the bluetooth module, a global error handler will be registered via: RxJavaPlugins.setErrorHandler()
.
If you want to customize this handler, it can set by calling RxJavaPlugins.setErrorHandler(handler)
at any point.
Please review the RxJava 2.x error handling documentation for more information about this handler.
Special Considerations
Most supported Bluetooth devices function similarly and you can follow the instructions in Bluetooth permissions, Bluetooth Operations and Passive Read to work with them.
However, a few of our supported devices have additional requirements and/or considerations that you need to be aware of. Those are covered in Bluetooth Behavior.
Updated 4 days ago