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

Commit 1c62e7d9 authored by Sanket Agarwal's avatar Sanket Agarwal
Browse files

Implement autoconnect for PAN.

Bug: b/30679978
Change-Id: I52c9ac49ec72a419e5f02c656abc46f2c7e4c7cc
(cherry picked from commit 7b3328c36a47ddaf37fe75dc89fa4b812ada983a)
parent 78e4c649
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -66,4 +66,7 @@

    <!-- For enabling the hfp client connection service -->
    <bool name="hfp_client_connection_service_enabled">false</bool>

    <!-- Enabling autoconnect over pan -->
    <bool name="config_bluetooth_pan_enable_autoconnect">true</bool>
</resources>
+28 −5
Original line number Diff line number Diff line
@@ -65,12 +65,13 @@ import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.hid.HidService;
import com.android.bluetooth.hfp.HeadsetService;
import com.android.bluetooth.hfpclient.HeadsetClientService;
import com.android.bluetooth.pan.PanService;
import com.android.bluetooth.pbapclient.PbapClientService;
import com.android.bluetooth.sdp.SdpManager;
import com.android.internal.app.IBatteryStats;
import com.android.internal.R;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;
import com.android.bluetooth.Utils;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
@@ -253,6 +254,8 @@ public class AdapterService extends Service {
        HeadsetService headsetService = HeadsetService.getHeadsetService();
        HeadsetClientService headsetClientService = HeadsetClientService.getHeadsetClientService();
        PbapClientService pbapClientService = PbapClientService.getPbapClientService();
        PanService panService = PanService.getPanService();


        // Set profile priorities only for the profiles discovered on the remote device.
        // This avoids needless auto-connect attempts to profiles non-existent on the remote device
@@ -296,6 +299,14 @@ public class AdapterService extends Service {
             (pbapClientService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED))) {
            pbapClientService.setPriority(device, BluetoothProfile.PRIORITY_ON);
        }

        if ((panService != null) &&
            (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.PANU) &&
             (panService.getPriority(device) == BluetoothProfile.PRIORITY_UNDEFINED) &&
             getResources().getBoolean(
                 R.bool.config_bluetooth_pan_enable_autoconnect))) {
            panService.setPriority(device, BluetoothProfile.PRIORITY_ON);
        }
    }

    private void processProfileStateChanged(BluetoothDevice device, int profileId, int newState, int prevState) {
@@ -309,7 +320,8 @@ public class AdapterService extends Service {

        // Profiles relevant to Car Kitts.
        if (((profileId == BluetoothProfile.A2DP_SINK) ||
             (profileId == BluetoothProfile.HEADSET_CLIENT)) &&
             (profileId == BluetoothProfile.HEADSET_CLIENT) ||
             (profileId == BluetoothProfile.PBAP_CLIENT)) &&
            (newState == BluetoothProfile.STATE_CONNECTED)) {
            debugLog( "Profile connected. Schedule missing profile connection if any");
            connectOtherProfile(device, PROFILE_CONN_CONNECTED);
@@ -545,7 +557,7 @@ public class AdapterService extends Service {
    void BleOnProcessStart() {
        debugLog("BleOnProcessStart()");

        if (getApplicationContext().getResources().getBoolean(
        if (getResources().getBoolean(
                R.bool.config_bluetooth_reload_supported_profiles_when_enabled)) {
            Config.init(getApplicationContext());
        }
@@ -1736,7 +1748,6 @@ public class AdapterService extends Service {
         }
    }


    public void connectOtherProfile(BluetoothDevice device, int firstProfileStatus){
        if ((mHandler.hasMessages(MESSAGE_CONNECT_OTHER_PROFILES) == false) &&
            (isQuietModeEnabled()== false)){
@@ -1760,6 +1771,7 @@ public class AdapterService extends Service {
        HeadsetClientService headsetClientService = HeadsetClientService.getHeadsetClientService();
        A2dpSinkService a2dpSinkService = A2dpSinkService.getA2dpSinkService();
        PbapClientService pbapClientService = PbapClientService.getPbapClientService();
        PanService panService = PanService.getPanService();

        boolean allProfilesEmpty = true;
        List<BluetoothDevice> a2dpConnDevList = null;
@@ -1767,6 +1779,7 @@ public class AdapterService extends Service {
        List<BluetoothDevice> headsetClientConnDevList = null;
        List<BluetoothDevice> a2dpSinkConnDevList = null;
        List<BluetoothDevice> pbapClientConnDevList = null;
        List<BluetoothDevice> panConnDevList = null;

        if (hsService != null) {
            hsConnDevList = hsService.getConnectedDevices();
@@ -1788,6 +1801,10 @@ public class AdapterService extends Service {
            pbapClientConnDevList = pbapClientService.getConnectedDevices();
            allProfilesEmpty = allProfilesEmpty && pbapClientConnDevList.isEmpty();
        }
        if (panService != null) {
            panConnDevList = panService.getConnectedDevices();
            allProfilesEmpty = allProfilesEmpty && panConnDevList.isEmpty();
        }

        if (allProfilesEmpty && (PROFILE_CONN_CONNECTED  == firstProfileStatus)) {
            // must have connected then disconnected, don't bother connecting others.
@@ -1824,6 +1841,12 @@ public class AdapterService extends Service {
                pbapClientService.connect(device);
            }
        }
        if (panService != null) {
            if (panConnDevList.isEmpty() &&
                (panService.getPriority(device) >= BluetoothProfile.PRIORITY_ON)) {
                panService.connect(device);
            }
        }
    }

     private void adjustOtherHeadsetPriorities(HeadsetService  hsService,
+65 −6
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.os.INetworkManagementService;
import android.os.Message;
import android.os.ServiceManager;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;

import com.android.bluetooth.btservice.ProfileService;
@@ -51,6 +52,7 @@ import java.util.List;
public class PanService extends ProfileService {
    private static final String TAG = "PanService";
    private static final boolean DBG = false;
    private static PanService sPanService;

    private static final String BLUETOOTH_IFACE_ADDR_START= "192.168.44.1";
    private static final int BLUETOOTH_MAX_PAN_CONNECTIONS = 5;
@@ -83,6 +85,40 @@ public class PanService extends ProfileService {
        return new BluetoothPanBinder(this);
    }

    public static synchronized PanService getPanService() {
        if (sPanService != null && sPanService.isAvailable()) {
            if (DBG) {
                Log.d(TAG, "getPanService(): returning " + sPanService);
            }
            return sPanService;
        }
        if (DBG) {
            if (sPanService == null) {
                Log.d(TAG, "getPanService(): service is NULL");
            } else if (!sPanService.isAvailable()) {
                Log.d(TAG, "getPanService(): service is not available");
            }
        }
        return null;
    }

    private static synchronized void setPanService(PanService instance) {
        if (instance != null && instance.isAvailable()) {
            if (DBG) {
                Log.d(TAG, "setPanService(): set to: " + instance);
            }
            sPanService = instance;
        } else {
            if (DBG) {
                if (instance == null) {
                    Log.d(TAG, "setPanService(): service not available");
                } else if (!instance.isAvailable()) {
                    Log.d(TAG, "setPanService(): service is cleaning up");
                }
            }
        }
    }

    protected boolean start() {
        mPanDevices = new HashMap<BluetoothDevice, BluetoothPanDevice>();
        mBluetoothIfaceAddresses = new ArrayList<String>();
@@ -97,6 +133,7 @@ public class PanService extends ProfileService {

        mNetworkFactory = new BluetoothTetheringNetworkFactory(getBaseContext(), getMainLooper(),
                this);
        setPanService(this);

        return true;
    }
@@ -246,7 +283,7 @@ public class PanService extends ProfileService {
        }
    };

    boolean connect(BluetoothDevice device) {
    public boolean connect(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (getConnectionState(device) != BluetoothProfile.STATE_DISCONNECTED) {
            Log.e(TAG, "Pan Device not disconnected: " + device);
@@ -257,14 +294,14 @@ public class PanService extends ProfileService {
        return true;
    }

    boolean disconnect(BluetoothDevice device) {
    public boolean disconnect(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        Message msg = mHandler.obtainMessage(MESSAGE_DISCONNECT,device);
        mHandler.sendMessage(msg);
        return true;
    }

    int getConnectionState(BluetoothDevice device) {
    public int getConnectionState(BluetoothDevice device) {
        BluetoothPanDevice panDevice = mPanDevices.get(device);
        if (panDevice == null) {
            return BluetoothPan.STATE_DISCONNECTED;
@@ -280,7 +317,7 @@ public class PanService extends ProfileService {
        if(DBG) Log.d(TAG, "isTetheringOn call getPanLocalRoleNative");
        return (getPanLocalRoleNative() & BluetoothPan.LOCAL_PANU_ROLE) != 0;
    }
     boolean isTetheringOn() {
    public boolean isTetheringOn() {
        // TODO(BT) have a variable marking the on/off state
        return mTetherOn;
    }
@@ -302,7 +339,29 @@ public class PanService extends ProfileService {
        }
    }

    List<BluetoothDevice> getConnectedDevices() {
    public boolean setPriority(BluetoothDevice device, int priority) {
        if (device == null) {
            throw new IllegalArgumentException("Null device");
        }
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
        Settings.Global.putInt(getContentResolver(),
                Settings.Global.getBluetoothPanPriorityKey(device.getAddress()),
                priority);
        if (DBG) {
            Log.d(TAG,"Saved priority " + device + " = " + priority);
        }
        return true;
    }

    public int getPriority(BluetoothDevice device) {
        if (device == null) throw new IllegalArgumentException("Null device");
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH_ADMIN permission");
        return Settings.Global.getInt(getContentResolver(),
                Settings.Global.getBluetoothPanPriorityKey(device.getAddress()),
                BluetoothProfile.PRIORITY_UNDEFINED);
    }

    public List<BluetoothDevice> getConnectedDevices() {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        List<BluetoothDevice> devices = getDevicesMatchingConnectionStates(
                new int[] {BluetoothProfile.STATE_CONNECTED});
+2 −0
Original line number Diff line number Diff line
@@ -183,6 +183,7 @@ public class PbapClientService extends ProfileService {
            return service.getConnectionState(device);
        }

        @Override
        public boolean setPriority(BluetoothDevice device, int priority) {
            PbapClientService service = getService();
            if (service == null) {
@@ -191,6 +192,7 @@ public class PbapClientService extends ProfileService {
            return service.setPriority(device, priority);
        }

        @Override
        public int getPriority(BluetoothDevice device) {
            PbapClientService service = getService();
            if (service == null) {