diff options
| -rw-r--r-- | core/java/android/bluetooth/BluetoothHidDevice.java | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/core/java/android/bluetooth/BluetoothHidDevice.java b/core/java/android/bluetooth/BluetoothHidDevice.java index 3d6209a..468df4d 100644 --- a/core/java/android/bluetooth/BluetoothHidDevice.java +++ b/core/java/android/bluetooth/BluetoothHidDevice.java @@ -148,18 +148,27 @@ public final class BluetoothHidDevice implements BluetoothProfile { public void onBluetoothStateChange(boolean up) { Log.d(TAG, "onBluetoothStateChange: up=" + up); - synchronized (mConnection) { if (!up) { - mService = null; - mContext.unbindService(mConnection); + Log.d(TAG,"Unbinding service..."); + if (mService != null) { + mService = null; + try { + mContext.unbindService(mConnection); + } catch (IllegalArgumentException e) { + Log.e(TAG,"onBluetoothStateChange: could not unbind service:", e); + } + } } else { - if (mService == null) { - Log.v(TAG, "Binding service"); - if (!mContext.bindService(new Intent(IBluetoothHidDevice.class.getName()), - mConnection, 0)) { - Log.e(TAG, "Could not bind service"); + try { + if (mService == null) { + Log.d(TAG,"Binding HID Device service..."); + doBind(); } + } catch (IllegalStateException e) { + Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e); + } catch (SecurityException e) { + Log.e(TAG,"onBluetoothStateChange: could not bind to HID Dev service: ", e); } } } @@ -191,7 +200,7 @@ public final class BluetoothHidDevice implements BluetoothProfile { }; BluetoothHidDevice(Context context, ServiceListener listener) { - Log.v(TAG, "BluetoothInputDevice()"); + Log.v(TAG, "BluetoothHidDevice"); mContext = context; mServiceListener = listener; @@ -206,10 +215,20 @@ public final class BluetoothHidDevice implements BluetoothProfile { } } - if (!context.bindService(new Intent(IBluetoothHidDevice.class.getName()), - mConnection, 0)) { - Log.e(TAG, "Could not bind service"); + doBind(); + } + + boolean doBind() { + Intent intent = new Intent(IBluetoothHidDevice.class.getName()); + ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); + intent.setComponent(comp); + if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, + android.os.Process.myUserHandle())) { + Log.e(TAG, "Could not bind to Bluetooth HID Device Service with " + intent); + return false; } + Log.d(TAG, "Bound to HID Device Service"); + return true; } void close() { @@ -227,7 +246,11 @@ public final class BluetoothHidDevice implements BluetoothProfile { synchronized (mConnection) { if (mService != null) { mService = null; - mContext.unbindService(mConnection); + try { + mContext.unbindService(mConnection); + } catch (IllegalArgumentException e) { + Log.e(TAG,"close: could not unbind HID Dev service: ", e); + } } } |
