diff options
Diffstat (limited to 'framework/java')
4 files changed, 83 insertions, 10 deletions
diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index bde52eedbc..9f50c54276 100755 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -31,6 +31,7 @@ import android.util.Log; import android.util.Pair; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -407,10 +408,6 @@ public final class BluetoothAdapter { * @throws IllegalArgumentException if address is invalid */ public BluetoothDevice getRemoteDevice(String address) { - if (mService == null) { - Log.e(TAG, "BT not enabled. Cannot create Remote Device"); - return null; - } return new BluetoothDevice(address); } @@ -504,7 +501,6 @@ public final class BluetoothAdapter { * immediate error */ public boolean enable() { - boolean enabled = false; try { return mManagerService.enable(); @@ -1212,6 +1208,11 @@ public final class BluetoothAdapter { if (DBG) Log.d(TAG, "onBluetoothServiceUp: " + bluetoothService); synchronized (mManagerCallback) { mService = bluetoothService; + for (IBluetoothManagerCallback cb : mBluetoothManagerCallbackList ){ + try { + cb.onBluetoothServiceUp(bluetoothService); + } catch (Exception e) { Log.e(TAG,"",e);} + } } } @@ -1219,6 +1220,11 @@ public final class BluetoothAdapter { if (DBG) Log.d(TAG, "onBluetoothServiceDown: " + mService); synchronized (mManagerCallback) { mService = null; + for (IBluetoothManagerCallback cb : mBluetoothManagerCallbackList ){ + try { + cb.onBluetoothServiceDown(); + } catch (Exception e) { Log.e(TAG,"",e);} + } } } }; @@ -1354,9 +1360,20 @@ public final class BluetoothAdapter { return mManagerService; } - /*package*/ IBluetooth getBluetoothService() { + private ArrayList<IBluetoothManagerCallback> mBluetoothManagerCallbackList = new ArrayList<IBluetoothManagerCallback>(); + //private IBluetoothStateChangeCallback mBluetoothStateChangeCallback; + /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) { + synchronized (mManagerCallback) { + if (!mBluetoothManagerCallbackList.contains(cb)) { + mBluetoothManagerCallbackList.add(cb); + } + } + return mService; + } + + /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) { synchronized (mManagerCallback) { - return mService; + mBluetoothManagerCallbackList.remove(cb); } } } diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index 2f7204deea..25047632ce 100755 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -485,12 +485,28 @@ public final class BluetoothDevice implements Parcelable { synchronized (BluetoothDevice.class) { if (sService == null) { BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); - sService = adapter.getBluetoothService(); + sService = adapter.getBluetoothService(mStateChangeCallback); } } return sService; } + static IBluetoothManagerCallback mStateChangeCallback = new IBluetoothManagerCallback.Stub() { + + public void onBluetoothServiceUp(IBluetooth bluetoothService) + throws RemoteException { + synchronized (BluetoothDevice.class) { + sService = bluetoothService; + } + } + + public void onBluetoothServiceDown() + throws RemoteException { + synchronized (BluetoothDevice.class) { + sService = null; + } + } + }; /** * Create a new BluetoothDevice * Bluetooth MAC address must be upper case, such as "00:11:22:33:AA:BB", diff --git a/framework/java/android/bluetooth/BluetoothPan.java b/framework/java/android/bluetooth/BluetoothPan.java index 13d9078752..cae7a73bcf 100644 --- a/framework/java/android/bluetooth/BluetoothPan.java +++ b/framework/java/android/bluetooth/BluetoothPan.java @@ -131,6 +131,11 @@ public final class BluetoothPan implements BluetoothProfile { mContext = context; mServiceListener = l; mAdapter = BluetoothAdapter.getDefaultAdapter(); + try { + mAdapter.getBluetoothManager().registerStateChangeCallback(mStateChangeCallback); + } catch (RemoteException re) { + Log.w(TAG,"Unable to register BluetoothStateChangeCallback",re); + } Log.d(TAG, "BluetoothPan() call bindService"); if (!context.bindService(new Intent(IBluetoothPan.class.getName()), mConnection, 0)) { @@ -146,8 +151,43 @@ public final class BluetoothPan implements BluetoothProfile { mConnection = null; } mServiceListener = null; + try { + mAdapter.getBluetoothManager().unregisterStateChangeCallback(mStateChangeCallback); + } catch (RemoteException re) { + Log.w(TAG,"Unable to register BluetoothStateChangeCallback",re); + } + } + + protected void finalize() { + close(); } + private IBluetoothStateChangeCallback mStateChangeCallback = new IBluetoothStateChangeCallback.Stub() { + + @Override + public void onBluetoothStateChange(boolean on) throws RemoteException { + //Handle enable request to bind again. + if (on) { + Log.d(TAG, "onBluetoothStateChange(on) call bindService"); + if (!mContext.bindService(new Intent(IBluetoothPan.class.getName()), + mConnection, 0)) { + Log.e(TAG, "Could not bind to Bluetooth HID Service"); + } + Log.d(TAG, "BluetoothPan(), bindService called"); + } else { + if (DBG) Log.d(TAG,"Unbinding service..."); + synchronized (mConnection) { + try { + mPanService = null; + mContext.unbindService(mConnection); + } catch (Exception re) { + Log.e(TAG,"",re); + } + } + } + } + }; + /** * Initiate connection to a profile of the remote bluetooth device. * diff --git a/framework/java/android/bluetooth/BluetoothSocket.java b/framework/java/android/bluetooth/BluetoothSocket.java index d37f2d5384..221b1a3c85 100644 --- a/framework/java/android/bluetooth/BluetoothSocket.java +++ b/framework/java/android/bluetooth/BluetoothSocket.java @@ -285,7 +285,7 @@ public final class BluetoothSocket implements Closeable { try { // TODO(BT) derive flag from auth and encrypt if (mSocketState == SocketState.CLOSED) throw new IOException("socket closed"); - IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(); + IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); if (bluetoothProxy == null) throw new IOException("Bluetooth is off"); mPfd = bluetoothProxy.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags()); @@ -322,7 +322,7 @@ public final class BluetoothSocket implements Closeable { /*package*/ int bindListen() { int ret; if (mSocketState == SocketState.CLOSED) return EBADFD; - IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(); + IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null); if (bluetoothProxy == null) { Log.e(TAG, "bindListen fail, reason: bluetooth is off"); return -1; |