Package org.altbeacon.bluetooth
Class BluetoothMedic
- java.lang.Object
-
- org.altbeacon.bluetooth.BluetoothMedic
-
public class BluetoothMedic extends Object
Utility class for checking the health of the bluetooth stack on the device by running two kinds of tests: scanning and transmitting. The class looks for specific failure codes from these tests to determine if the bluetooth stack is in a bad state and if so, optionally cycle power to bluetooth to try and fix the problem. This is known to work well on some Android devices. The tests may be called directly, or set up to run automatically approximately every 15 minutes. To set up in an automated way:BluetoothMedic medic = BluetoothMedic.getInstance(); medic.enablePowerCycleOnFailures(context); medic.enablePeriodicTests(context, BluetoothMedic.SCAN_TEST | BluetoothMedic.TRANSMIT_TEST);To set up in a manual way:BluetoothMedic medic = BluetoothMedic.getInstance(); medic.enablePowerCycleOnFailures(context); if (!medic.runScanTest(context)) { // Bluetooth stack is in a bad state } if (!medic.runTransmitterTest(context)) { // Bluetooth stack is in a bad state }
-
-
Field Summary
Fields Modifier and Type Field Description static intNO_TESTIndicates that no test should be run by the BluetoothTestJobstatic intSCAN_TESTIndicates that the bluetooth scan test should be run by the BluetoothTestJobstatic intTRANSMIT_TESTIndicates that the transmitter test should be run by the BluetoothTestJob
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidenablePeriodicTests(Context context, int testType)Calling this method starts a scheduled job that will run tests of the specified type to make sure bluetooth is OK and cycle power to bluetooth if needed and configured by enablePowerCycleOnFailuresvoidenablePowerCycleOnFailures(Context context)If set to true, bluetooth will be power cycled on any tests run that determine bluetooth is in a bad state.static BluetoothMedicgetInstance()Get a singleton instance of the BluetoothMedicbooleanrunScanTest(Context context)Starts up a brief blueooth scan with the intent of seeing if it results in an error condition indicating the bluetooth stack may be in a bad state.booleanrunTransmitterTest(Context context)Starts up a beacon transmitter with the intent of seeing if it results in an error condition indicating the bluetooth stack may be in a bad state.voidsetNotificationsEnabled(boolean enabled, int icon)Configure whether to send user-visible notification warnings when bluetooth power is cycled.
-
-
-
Field Detail
-
NO_TEST
public static final int NO_TEST
Indicates that no test should be run by the BluetoothTestJob- See Also:
- Constant Field Values
-
TRANSMIT_TEST
public static final int TRANSMIT_TEST
Indicates that the transmitter test should be run by the BluetoothTestJob- See Also:
- Constant Field Values
-
SCAN_TEST
public static final int SCAN_TEST
Indicates that the bluetooth scan test should be run by the BluetoothTestJob- See Also:
- Constant Field Values
-
-
Method Detail
-
getInstance
public static BluetoothMedic getInstance()
Get a singleton instance of the BluetoothMedic- Returns:
-
enablePowerCycleOnFailures
@RequiresApi(21) public void enablePowerCycleOnFailures(Context context)
If set to true, bluetooth will be power cycled on any tests run that determine bluetooth is in a bad state.- Parameters:
context-
-
enablePeriodicTests
@RequiresApi(21) public void enablePeriodicTests(Context context, int testType)
Calling this method starts a scheduled job that will run tests of the specified type to make sure bluetooth is OK and cycle power to bluetooth if needed and configured by enablePowerCycleOnFailures- Parameters:
context-testType- e.g. BluetoothMedic.TRANSMIT_TEST | BluetoothMedic.SCAN_TEST
-
runScanTest
@RequiresApi(21) public boolean runScanTest(Context context)
Starts up a brief blueooth scan with the intent of seeing if it results in an error condition indicating the bluetooth stack may be in a bad state. If the failure error code matches a pattern known to be associated with a bad bluetooth stack state, then the bluetooth stack is turned off and then back on after a short delay in order to try to recover.- Returns:
- false if the test indicates a failure indicating a bad state of the bluetooth stack
-
runTransmitterTest
@RequiresApi(21) public boolean runTransmitterTest(Context context)
Starts up a beacon transmitter with the intent of seeing if it results in an error condition indicating the bluetooth stack may be in a bad state. If the failure error code matches a pattern known to be associated with a bad bluetooth stack state, then the bluetooth stack is turned off and then back on after a short delay in order to try to recover.- Returns:
- false if the test indicates a failure indicating a bad state of the bluetooth stack
-
setNotificationsEnabled
@RequiresApi(21) public void setNotificationsEnabled(boolean enabled, int icon)Configure whether to send user-visible notification warnings when bluetooth power is cycled.- Parameters:
enabled- if true, a user-visible notification is sent to tell the user whenicon- the icon drawable to use in notifications (e.g. R.drawable.notification_icon)
-
-