From d06f49189c2e7f4dee280b513afc147a639eaf7b Mon Sep 17 00:00:00 2001 From: Marie Janssen Date: Mon, 20 Mar 2017 11:01:19 -0700 Subject: [PATCH] Bluetooth: stop pairing service correctly If BluetoothPairingService was stopped and restarted, it would receive a null intent, and it also would not signal correctly when it needed to be stopped. Since restarts can happen, check that we are still bonding when starting. Also prevent crashes when we might be called with a null intent. Test: initiate pairing remotely, adb am kill service, notice no crash Bug: 36130262 Change-Id: I408811935fdb36591826f4481d103cf4799db27e --- .../bluetooth/BluetoothPairingService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/bluetooth/BluetoothPairingService.java b/src/com/android/settings/bluetooth/BluetoothPairingService.java index a24a3f04bd6..5dfd2839b0e 100644 --- a/src/com/android/settings/bluetooth/BluetoothPairingService.java +++ b/src/com/android/settings/bluetooth/BluetoothPairingService.java @@ -80,6 +80,7 @@ public final class BluetoothPairingService extends Service { Log.d(TAG, "Dismiss pairing for " + mDevice.getAddress() + " (" + mDevice.getName() + "), Cancelled."); } stopForeground(true); + stopSelf(); } }; @@ -89,6 +90,12 @@ public final class BluetoothPairingService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { + if (intent == null) { + Log.e(TAG, "Can't start: null intent!"); + stopSelf(); + return START_NOT_STICKY; + } + Resources res = getResources(); Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(android.R.drawable.stat_sys_data_bluetooth) @@ -99,6 +106,12 @@ public final class BluetoothPairingService extends Service { mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + if (mDevice.getBondState() != BluetoothDevice.BOND_BONDING) { + Log.w(TAG, "Device " + mDevice + " not bonding: " + mDevice.getBondState()); + stopSelf(); + return START_NOT_STICKY; + } + String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); if (TextUtils.isEmpty(name)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); @@ -119,7 +132,7 @@ public final class BluetoothPairingService extends Service { registerReceiver(mCancelReceiver, filter); startForeground(NOTIFICATION_ID, builder.getNotification()); - return START_STICKY; + return START_REDELIVER_INTENT; } @Override