-
public class BluetoothMedicUtility 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 public final static intNO_TESTpublic final static intTRANSMIT_TESTpublic final static intSCAN_TEST
-
Method Summary
Modifier and Type Method Description voidprocessMedicAction(String action, int errorCode)static BluetoothMedicgetInstance()Get a singleton instance of the BluetoothMedic voidenablePowerCycleOnFailures(Context context)If set to true, bluetooth will be power cycled on any tests run that determine bluetooth isin a bad state. voidlegacyEnablePowerCycleOnFailures(Context context)If set to true, bluetooth will be power cycled on any tests run that determine bluetooth isin a bad state. voidenablePeriodicTests(Context context, int testType)Calling this method starts a scheduled job that will run tests of the specified type tomake sure bluetooth is OK and cycle power to bluetooth if needed and configured byenablePowerCycleOnFailures booleanrunScanTest(Context context)Starts up a brief blueooth scan with the intent of seeing if it results in an error conditionindicating 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 stackstate, then the bluetooth stack is turned off and then back on after a short delay in orderto try to recover. booleanrunTransmitterTest(Context context)Starts up a beacon transmitter with the intent of seeing if it results in an error conditionindicating 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 stackstate, then the bluetooth stack is turned off and then back on after a short delay in orderto try to recover. voidsetNotificationsEnabled(boolean enabled, int icon)Configure whether to send user-visible notification warnings when bluetooth power is cycled. -
-
Method Detail
-
processMedicAction
void processMedicAction(String action, int errorCode)
-
getInstance
static BluetoothMedic getInstance()
Get a singleton instance of the BluetoothMedic
-
enablePowerCycleOnFailures
@RequiresApi(value = 21)@Deprecated() void enablePowerCycleOnFailures(Context context)
If set to true, bluetooth will be power cycled on any tests run that determine bluetooth isin a bad state. This only works on Anroid 4.3-12.x devices, as the ability to power cycleBluetooth has been blocked from 3rd party apps on Android 13.
-
legacyEnablePowerCycleOnFailures
@RequiresApi(value = 21) void legacyEnablePowerCycleOnFailures(Context context)
If set to true, bluetooth will be power cycled on any tests run that determine bluetooth isin a bad state. This only works on Anroid 4.3-12.x devices, as the ability to power cycleBluetooth has been blocked from 3rd party apps on Android 13.
-
enablePeriodicTests
@RequiresApi(value = 21) void enablePeriodicTests(Context context, int testType)
Calling this method starts a scheduled job that will run tests of the specified type tomake sure bluetooth is OK and cycle power to bluetooth if needed and configured byenablePowerCycleOnFailures
- Parameters:
testType- e.g.
-
runScanTest
@RequiresApi(value = 21) boolean runScanTest(Context context)
Starts up a brief blueooth scan with the intent of seeing if it results in an error conditionindicating 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 stackstate, then the bluetooth stack is turned off and then back on after a short delay in orderto try to recover.
-
runTransmitterTest
@RequiresApi(value = 21) boolean runTransmitterTest(Context context)
Starts up a beacon transmitter with the intent of seeing if it results in an error conditionindicating 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 stackstate, then the bluetooth stack is turned off and then back on after a short delay in orderto try to recover.
-
setNotificationsEnabled
@RequiresApi(value = 21) 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.
-
-
-
-