Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 903ac6f3 authored by fredc's avatar fredc Committed by Android (Google) Code Review
Browse files

Fixed issue with Settings app crashing after during on/off and unpair.

Fixed issue with BluetoothAdapter.getRemoteDevice() returning null.

Change-Id: Ie86813532530a6b57bde1c430c7b4875ecc7354c
parent 7077272d
Loading
Loading
Loading
Loading
+24 −7
Original line number Diff line number Diff line
@@ -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) {
            mBluetoothManagerCallbackList.remove(cb);
        }
    }
}
+17 −1
Original line number Diff line number Diff line
@@ -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",
+40 −0
Original line number Diff line number Diff line
@@ -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,7 +151,42 @@ 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.
+2 −2
Original line number Diff line number Diff line
@@ -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;