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

Commit dad1f247 authored by Andre Eisenbach's avatar Andre Eisenbach
Browse files

LE: Add is_connected() function (3/3)

Change-Id: I71f64ff59e4602d2834f1c177310a36320e70ead
parent 2835b668
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -780,6 +780,22 @@ static jboolean cancelBondNative(JNIEnv* env, jobject obj, jbyteArray address) {
    return result;
}

static jboolean isConnectedNative(JNIEnv* env, jobject obj, jbyteArray address) {
    ALOGV("%s:",__FUNCTION__);
    if (!sBluetoothInterface) return JNI_FALSE;

    jbyte *addr = env->GetByteArrayElements(address, NULL);
    if (addr == NULL) {
        jniThrowIOException(env, EINVAL);
        return JNI_FALSE;
    }

    int ret = sBluetoothInterface->get_connection_state((bt_bdaddr_t *)addr);
    env->ReleaseByteArrayElements(address, addr, 0);

    return (ret != 0 ? JNI_TRUE : JNI_FALSE);
}

static jboolean pinReplyNative(JNIEnv *env, jobject obj, jbyteArray address, jboolean accept,
                               jint len, jbyteArray pinArray) {
    ALOGV("%s:",__FUNCTION__);
@@ -1065,6 +1081,7 @@ static JNINativeMethod sMethods[] = {
    {"createBondNative", "([B)Z", (void*) createBondNative},
    {"removeBondNative", "([B)Z", (void*) removeBondNative},
    {"cancelBondNative", "([B)Z", (void*) cancelBondNative},
    {"isConnectedNative", "([B)Z", (void*) isConnectedNative},
    {"pinReplyNative", "([BZI[B)Z", (void*) pinReplyNative},
    {"sspReplyNative", "([BIZI)Z", (void*) sspReplyNative},
    {"getRemoteServicesNative", "([B)Z", (void*) getRemoteServicesNative},
+4 −4
Original line number Diff line number Diff line
@@ -1314,10 +1314,8 @@ public class AdapterService extends Service {

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

        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
        if (deviceProp == null) return false;
        return deviceProp.getOpenAclConnectionCount() > 0;
        byte[] addr = Utils.getBytesFromAddress(device.getAddress());
        return isConnectedNative(addr);
    }

     String getRemoteName(BluetoothDevice device) {
@@ -1600,6 +1598,8 @@ public class AdapterService extends Service {
    /*package*/ native boolean removeBondNative(byte[] address);
    /*package*/ native boolean cancelBondNative(byte[] address);

    /*package*/ native boolean isConnectedNative(byte[] address);

    private native boolean startDiscoveryNative();
    private native boolean cancelDiscoveryNative();

+0 −13
Original line number Diff line number Diff line
@@ -107,11 +107,9 @@ final class RemoteDevices {
        private int mDeviceType;
        private String mAlias;
        private int mBondState;
        private AtomicInteger mOpenAclConnectionCount;

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

        /**
@@ -212,10 +210,6 @@ final class RemoteDevices {
                return mBondState;
            }
        }

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

    private void sendUuidIntent(BluetoothDevice device) {
@@ -415,7 +409,6 @@ final class RemoteDevices {
            return;
        }

        int openAclConnectionCount;
        DeviceProperties prop = getDeviceProperties(device);
        if (prop == null) {
            errorLog("aclStateChangeCallback reported unknown device " + Arrays.toString(address));
@@ -424,15 +417,9 @@ final class RemoteDevices {
        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);