BA: Handling null check condition when BT on and OFF happening

Handling null check conditions. When it is continuous BT ON/OFF
there is a race condition triggering NULL point exception in
GattBroadcast service.

Change-Id: I4a8def7d7e2e27b82ea788c02c0e5924db480f2f
diff --git a/packages_apps_bluetooth_ext/src/ba/BATService.java b/packages_apps_bluetooth_ext/src/ba/BATService.java
index 5f6a01b..d65eda5 100644
--- a/packages_apps_bluetooth_ext/src/ba/BATService.java
+++ b/packages_apps_bluetooth_ext/src/ba/BATService.java
@@ -301,8 +301,10 @@
                         BluetoothAdapter.ERROR);
                 if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_ON) {
                     Log.d(TAG,"ACTION_BLE_STATE_CHANGED state: " + state);
-                    mGattBroadcastService = new GattBroadcastService();
-                    mGattBroadcastService.start(getApplicationContext());
+                    if (mGattBroadcastService == null) {
+                        mGattBroadcastService = new GattBroadcastService();
+                        mGattBroadcastService.start(getApplicationContext());
+                    }
                 }
             }
         }
@@ -420,6 +422,7 @@
 
         if(mGattBroadcastService != null) {
             mGattBroadcastService.stop();
+            mGattBroadcastService = null;
         }
         Log.d(TAG, "BATService :: stop - ");
         return true;
diff --git a/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java b/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
index cef1810..b77e501 100644
--- a/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
+++ b/packages_apps_bluetooth_ext/src/ba/GattBroadcastService.java
@@ -1290,25 +1290,29 @@
             addState(mBRADisabledStreamingActive);
             addState(mBRAEnabledStreamingPaused);
             addState(mBRAEnabledStreamingActive);
-
-            if (mBATService.getBATState() == BATService.STATE_PLAYING) {
-                setInitialState(mBRADisabledStreamingActive);
-                int div = mBATService.getDIV();
-                Log.d("GattBroadcastServiceStateMachine:", "div = " + div);
-                if (div != -1) {
-                    mBleAdvertiser.setDiv(div);
+            if (mBATService != null) {
+                if (mBATService.getBATState() == BATService.STATE_PLAYING) {
+                    setInitialState(mBRADisabledStreamingActive);
+                    int div = mBATService.getDIV();
+                    Log.d("GattBroadcastServiceStateMachine:", "div = " + div);
+                    if (div != -1 && mBleAdvertiser != null) {
+                        mBleAdvertiser.setDiv(div);
+                    }
+                    if (mBleAdvertiser != null) {
+                        mBleAdvertiser.stopAdvertising();
+                        Log.d("GattBroadcastServiceStateMachine:",
+                            " starting advertising");
+                        mBleAdvertiser.startAdvertising(false,
+                            BleAdvertiser.BRA_DISABLED_STREAMING_ACTIVE_ADV_INTERVAL);
+                    }
                 }
-                if (mBleAdvertiser != null) {
-                    mBleAdvertiser.stopAdvertising();
-                    Log.d("GattBroadcastServiceStateMachine:"," starting advertising");
-                    mBleAdvertiser.startAdvertising(false,
-                        BleAdvertiser.BRA_DISABLED_STREAMING_ACTIVE_ADV_INTERVAL);
-                }
+                else if (mBATService.getBATState() == BATService.STATE_PAUSED)
+                    setInitialState(mBRADisabledStreamingPaused);
+                else
+                    setInitialState(mBRADisabledStreamingDisabled);
             }
-            else if (mBATService.getBATState() == BATService.STATE_PAUSED)
-                setInitialState(mBRADisabledStreamingPaused);
             else
-                setInitialState(mBRADisabledStreamingDisabled);
+                Log.e(TAG, "BA Service is null");
 
         }
 
@@ -2075,4 +2079,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}