From da9f738c0cfeeebd77e7e7313a63cc1156b8ea13 Mon Sep 17 00:00:00 2001 From: Deqiang Chen Date: Tue, 12 Mar 2019 14:08:12 -0700 Subject: [PATCH] Avoid IllegalArgumentException when bluetooth is disabled When the corresponding profile service is not available on device, bluetoothA2dp, bluetoothHidHost and bluetoothPbap will report IllegalArgumentException when bluetooth is disabled. This change avoid that exception. Bug: 127359897 Test: local test and observe there is no IllegalArgumentException when bluetooth is disabled Change-Id: I457e5225b8710dd5d02c405ef558892e99d1736f (cherry picked from commit ee8371007b82ff8c6d26e8ac90c6e3d3cf6bf2a8) --- core/java/android/bluetooth/BluetoothA2dp.java | 6 ++++-- core/java/android/bluetooth/BluetoothHidHost.java | 6 ++++-- core/java/android/bluetooth/BluetoothPbap.java | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/core/java/android/bluetooth/BluetoothA2dp.java b/core/java/android/bluetooth/BluetoothA2dp.java index 171c2f5b1a086..9faa1d675d8d9 100644 --- a/core/java/android/bluetooth/BluetoothA2dp.java +++ b/core/java/android/bluetooth/BluetoothA2dp.java @@ -224,8 +224,10 @@ public final class BluetoothA2dp implements BluetoothProfile { if (VDBG) Log.d(TAG, "Unbinding service..."); try { mServiceLock.writeLock().lock(); - mService = null; - mContext.unbindService(mConnection); + if (mService != null) { + mService = null; + mContext.unbindService(mConnection); + } } catch (Exception re) { Log.e(TAG, "", re); } finally { diff --git a/core/java/android/bluetooth/BluetoothHidHost.java b/core/java/android/bluetooth/BluetoothHidHost.java index 0ca39f169a721..289e769a99ef8 100644 --- a/core/java/android/bluetooth/BluetoothHidHost.java +++ b/core/java/android/bluetooth/BluetoothHidHost.java @@ -232,8 +232,10 @@ public final class BluetoothHidHost implements BluetoothProfile { if (VDBG) Log.d(TAG, "Unbinding service..."); synchronized (mConnection) { try { - mService = null; - mContext.unbindService(mConnection); + if (mService != null) { + mService = null; + mContext.unbindService(mConnection); + } } catch (Exception re) { Log.e(TAG, "", re); } diff --git a/core/java/android/bluetooth/BluetoothPbap.java b/core/java/android/bluetooth/BluetoothPbap.java index ae264e19bb7c2..b303c34ee0ae3 100644 --- a/core/java/android/bluetooth/BluetoothPbap.java +++ b/core/java/android/bluetooth/BluetoothPbap.java @@ -120,8 +120,10 @@ public class BluetoothPbap implements BluetoothProfile { log("Unbinding service..."); synchronized (mConnection) { try { - mService = null; - mContext.unbindService(mConnection); + if (mService != null) { + mService = null; + mContext.unbindService(mConnection); + } } catch (Exception re) { Log.e(TAG, "", re); }