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

Commit 2835b668 authored by Andre Eisenbach's avatar Andre Eisenbach
Browse files

Adding a method BluetoothDevice.isConnected()



Adding a new method to BluetoothDevice that lets callers know whether a remote device is
connected.

Original-patch-by: default avatarJay Civelli <jcivelli@google.com>
Cherry-picked-from: master
Change-Id: Ia8ad2e3189d08b63832634bdf804656b8d013820
parent e046e10b
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -814,6 +814,14 @@ public class AdapterService extends Service {
            return service.getBondState(device);
        }

        public boolean isConnected(BluetoothDevice device) {
            AdapterService service = getService();
            if (service == null) {
                return false;
            }
            return service.isConnected(device);
        }

        public String getRemoteName(BluetoothDevice device) {
            if (!Utils.checkCaller()) {
                Log.w(TAG,"getRemoteName(): not allowed for non-active user");
@@ -1304,6 +1312,14 @@ public class AdapterService extends Service {
        return deviceProp.getBondState();
    }

    boolean isConnected(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");

        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
        if (deviceProp == null) return false;
        return deviceProp.getOpenAclConnectionCount() > 0;
    }

     String getRemoteName(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
+19 −1
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ import android.util.Log;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;

@@ -105,9 +107,11 @@ final class RemoteDevices {
        private int mDeviceType;
        private String mAlias;
        private int mBondState;
        private AtomicInteger mOpenAclConnectionCount;

        DeviceProperties() {
            mBondState = BluetoothDevice.BOND_NONE;
            mOpenAclConnectionCount = new AtomicInteger(0);
        }

        /**
@@ -208,8 +212,11 @@ final class RemoteDevices {
                return mBondState;
            }
        }
    }

        int getOpenAclConnectionCount() {
            return mOpenAclConnectionCount.get();
        }
    }

    private void sendUuidIntent(BluetoothDevice device) {
        DeviceProperties prop = getDeviceProperties(device);
@@ -408,13 +415,24 @@ final class RemoteDevices {
            return;
        }

        int openAclConnectionCount;
        DeviceProperties prop = getDeviceProperties(device);
        if (prop == null) {
            errorLog("aclStateChangeCallback reported unknown device " + Arrays.toString(address));
        }
        Intent intent = null;
        if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) {
            intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED);
            debugLog("aclStateChangeCallback: State:Connected to Device:" + device);
            if (prop != null) {
                prop.mOpenAclConnectionCount.incrementAndGet();
            }
        } else {
            intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
            debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device);
            if (prop != null) {
                prop.mOpenAclConnectionCount.decrementAndGet();
            }
        }
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);