Package 

Class BluetoothMedic


  • 
    public class BluetoothMedic
    
                        

    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
      public final static int NO_TEST
      public final static int TRANSMIT_TEST
      public final static int SCAN_TEST
    • Method Summary

      Modifier and Type Method Description
      void processMedicAction(String action, int errorCode)
      static BluetoothMedic getInstance() Get a singleton instance of the BluetoothMedic
      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.
      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
      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.
      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.
      void setNotificationsEnabled(boolean enabled, int icon) Configure whether to send user-visible notification warnings when bluetooth power is cycled.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • enablePowerCycleOnFailures

         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.

      • enablePeriodicTests

         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:
        testType - e.g.
      • runScanTest

         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.

      • runTransmitterTest

         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.

      • setNotificationsEnabled

         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 when
        icon - the icon drawable to use in notifications (e.g.