Improve BT enable/disable stability under stress tests

Change-Id: Ie6323a225b5e1ccd1ad9f65e90b144c93b957a8b
diff --git a/android/app/AndroidManifest.xml b/android/app/AndroidManifest.xml
index 0620831..df07838 100644
--- a/android/app/AndroidManifest.xml
+++ b/android/app/AndroidManifest.xml
@@ -58,6 +58,7 @@
             android:permission="android.permission.ACCESS_BLUETOOTH_SHARE" />
         <receiver
             android:process="@string/process"
+            android:exported="true"
             android:name=".opp.BluetoothOppReceiver">
             <intent-filter>
                 <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
@@ -173,6 +174,7 @@
         </service>
         <receiver
             android:process="@string/process"
+            android:exported="true"
             android:name=".pbap.BluetoothPbapReceiver">
             <intent-filter>
                 <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
diff --git a/android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp b/android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp
index 5b4ef30..89d4823 100755
--- a/android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp
+++ b/android/app/jni/com_android_bluetooth_btservice_AdapterService.cpp
@@ -3,7 +3,6 @@
  */
 
 #define LOG_TAG "BluetoothServiceJni"
-
 #include "com_android_bluetooth.h"
 #include "hardware/bt_sock.h"
 #include "utils/Log.h"
@@ -89,7 +88,8 @@
 
     *types = callbackEnv->NewIntArray(num_properties);
     if (*types == NULL) goto Fail;
-
+    // Delete the reference to val
+    callbackEnv->DeleteLocalRef(val);
     for (int i = 0; i < num_properties; i++) {
 
        /* The higher layers expect rssi as a short int value, while the value is sent as a byte
@@ -108,7 +108,8 @@
        callbackEnv->SetByteArrayRegion(propVal, 0, properties[i].len,
                                             (jbyte*)properties[i].val);
        callbackEnv->SetObjectArrayElement(*props, i, propVal);
-
+       // Delete reference to propVal
+       callbackEnv->DeleteLocalRef(propVal);
        callbackEnv->SetIntArrayRegion(*types, i, 1, (jint *)&properties[i].type);
     }
     return 0;
diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java b/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java
index 36de84f..9fa065c 100755
--- a/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java
+++ b/android/app/src/com/android/bluetooth/btservice/AdapterProperties.java
@@ -443,6 +443,13 @@
         }
     }
 
+    void onBluetoothDisable() {
+        // When BT disable is invoked, set the scan_mode to NONE
+        // so no incoming connections are possible
+        if (getState() == BluetoothAdapter.STATE_TURNING_OFF) {
+            setScanMode(AbstractionLayer.BT_SCAN_MODE_NONE);
+        }
+    }
     void discoveryStateChangeCallback(int state) {
         infoLog("Callback:discoveryStateChangeCallback with state:" + state);
         synchronized (mObject) {
diff --git a/android/app/src/com/android/bluetooth/btservice/AdapterState.java b/android/app/src/com/android/bluetooth/btservice/AdapterState.java
index 11bdbfe..1086c22 100644
--- a/android/app/src/com/android/bluetooth/btservice/AdapterState.java
+++ b/android/app/src/com/android/bluetooth/btservice/AdapterState.java
@@ -128,6 +128,9 @@
                    //Fall Through
                case AIRPLANE_MODE_ON:
                    sendIntent(BluetoothAdapter.STATE_TURNING_OFF);
+                   // Invoke onBluetoothDisable which shall trigger a
+                   // setScanMode to SCAN_MODE_NONE
+                   mAdapterProperties.onBluetoothDisable();
                    if (mAdapterProperties.getConnectionState() !=
                            BluetoothAdapter.STATE_DISCONNECTED) {
                        sendMessageDelayed(ALL_DEVICES_DISCONNECTED,