Merge "BluetoothMidiDevice: Cleanup and error handling improvements" into mnc-dev
diff --git a/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java b/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java
index 63a8da7..0ccbf6a 100644
--- a/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java
+++ b/media/packages/BluetoothMidiService/src/com/android/bluetoothmidiservice/BluetoothMidiDevice.java
@@ -92,7 +92,6 @@
                         mBluetoothGatt.discoverServices());
             } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                 Log.i(TAG, "Disconnected from GATT server.");
-                // FIXME synchronize?
                 close();
             }
         }
@@ -121,8 +120,8 @@
                     }
                 }
             } else {
-                Log.w(TAG, "onServicesDiscovered received: " + status);
-                // FIXME - report error back to client?
+                Log.e(TAG, "onServicesDiscovered received: " + status);
+                close();
             }
         }
 
@@ -137,9 +136,12 @@
 
             BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                     CLIENT_CHARACTERISTIC_CONFIG);
-            // FIXME null check
-            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
-            mBluetoothGatt.writeDescriptor(descriptor);
+            if (descriptor != null) {
+                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
+                mBluetoothGatt.writeDescriptor(descriptor);
+            } else {
+                Log.e(TAG, "No CLIENT_CHARACTERISTIC_CONFIG for device " + mBluetoothDevice);
+            }
         }
 
         @Override
@@ -244,16 +246,18 @@
         }.start();
     }
 
-    void close() {
+    private void close() {
+        synchronized (mBluetoothDevice) {
         mEventScheduler.close();
-        if (mDeviceServer != null) {
-            IoUtils.closeQuietly(mDeviceServer);
-            mDeviceServer = null;
-            mService.deviceClosed(mBluetoothDevice);
-        }
-        if (mBluetoothGatt != null) {
-            mBluetoothGatt.close();
-            mBluetoothGatt = null;
+            if (mDeviceServer != null) {
+                IoUtils.closeQuietly(mDeviceServer);
+                mDeviceServer = null;
+                mService.deviceClosed(mBluetoothDevice);
+            }
+            if (mBluetoothGatt != null) {
+                mBluetoothGatt.close();
+                mBluetoothGatt = null;
+            }
         }
     }