diff options
author | Matthew Xie <mattx@google.com> | 2011-08-25 16:45:58 -0700 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2011-08-29 16:24:50 -0700 |
commit | 6e1b765e5b8cc70bacbaf04862d00cf8cc8c4a99 (patch) | |
tree | 0517574b7a2bd4b1683a8edc5c4ca27a787b5c8a /framework/java/android/bluetooth/BluetoothAdapter.java | |
parent | 26dbe77f8c0ed4a256a185c73c98c38067ded708 (diff) |
Move Bluetooth remove service record handler to the app main thread
Move Bluetooth remove service record handler to the app main thread.
This removes the dependency of caller thread that constructs the
BluetoothAdapter singleton instance. The caller thread could stop while
BluetoothAdapter singleton exists but lose the handler context.
Make the BluetoothService.removeServiceRecord return quickly without
blocking on native call.
bug 4999443
Change-Id: I302534baa381f4aca8192e1e4a9ea21793b07ba6
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothAdapter.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothAdapter.java | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index 223692859d..254c98ff8c 100644 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -22,6 +22,7 @@ import android.content.Context; import android.os.Binder; import android.os.Handler; import android.os.IBinder; +import android.os.Looper; import android.os.Message; import android.os.ParcelUuid; import android.os.RemoteException; @@ -348,6 +349,8 @@ public final class BluetoothAdapter { private final IBluetooth mService; + private Handler mServiceRecordHandler; + /** * Get a handle to the default local Bluetooth adapter. * <p>Currently Android only supports one Bluetooth adapter, but the API @@ -376,6 +379,7 @@ public final class BluetoothAdapter { throw new IllegalArgumentException("service is null"); } mService = service; + mServiceRecordHandler = null; } /** @@ -1011,7 +1015,21 @@ public final class BluetoothAdapter { } catch (IOException e) {} throw new IOException("Not able to register SDP record for " + name); } - socket.setCloseHandler(mHandler, handle); + + if (mServiceRecordHandler == null) { + mServiceRecordHandler = new Handler(Looper.getMainLooper()) { + public void handleMessage(Message msg) { + /* handle socket closing */ + int handle = msg.what; + try { + if (DBG) Log.d(TAG, "Removing service record " + + Integer.toHexString(handle)); + mService.removeServiceRecord(handle); + } catch (RemoteException e) {Log.e(TAG, "", e);} + } + }; + } + socket.setCloseHandler(mServiceRecordHandler, handle); return socket; } @@ -1243,17 +1261,6 @@ public final class BluetoothAdapter { return Collections.unmodifiableSet(devices); } - private Handler mHandler = new Handler() { - public void handleMessage(Message msg) { - /* handle socket closing */ - int handle = msg.what; - try { - if (DBG) Log.d(TAG, "Removing service record " + Integer.toHexString(handle)); - mService.removeServiceRecord(handle); - } catch (RemoteException e) {Log.e(TAG, "", e);} - } - }; - /** * Validate a Bluetooth address, such as "00:43:A8:23:10:F0" * <p>Alphabetic characters must be uppercase to be valid. |