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

Commit 6903a7de authored by Matthew Xie's avatar Matthew Xie Committed by Android (Google) Code Review
Browse files

Merge "Provide an API to set the friendly name of a remote device."

parents 318fecea 269e81a5
Loading
Loading
Loading
Loading
+48 −0
Original line number Original line Diff line number Diff line
@@ -565,6 +565,54 @@ public final class BluetoothDevice implements Parcelable {
        return null;
        return null;
    }
    }


    /**
     * Get the Bluetooth alias of the remote device.
     * <p>Alias is the locally modified name of a remote device.
     *
     * @return the Bluetooth alias, or null if no alias or there was a problem
     * @hide
     */
    public String getAlias() {
        try {
            return sService.getRemoteAlias(mAddress);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return null;
    }

    /**
     * Set the Bluetooth alias of the remote device.
     * <p>Alias is the locally modified name of a remote device.
     * <p>This methoid overwrites the alias. The changed
     * alias is saved in the local storage so that the change
     * is preserved over power cycle.
     *
     * @return true on success, false on error
     * @hide
     */
    public boolean setAlias(String alias) {
        try {
            return sService.setRemoteAlias(mAddress, alias);
        } catch (RemoteException e) {Log.e(TAG, "", e);}
        return false;
    }

    /**
     * Get the Bluetooth alias of the remote device.
     * If Alias is null, get the Bluetooth name instead.
     * @see #getAlias()
     * @see #getName()
     *
     * @return the Bluetooth alias, or null if no alias or there was a problem
     * @hide
     */
    public String getAliasName() {
        String name = getAlias();
        if (name == null) {
            name = getName();
        }
        return name;
    }

    /**
    /**
     * Start the bonding (pairing) process with the remote device.
     * Start the bonding (pairing) process with the remote device.
     * <p>This is an asynchronous call, it will return immediately. Register
     * <p>This is an asynchronous call, it will return immediately. Register
+2 −0
Original line number Original line Diff line number Diff line
@@ -66,6 +66,8 @@ interface IBluetooth
    boolean setDeviceOutOfBandData(in String address, in byte[] hash, in byte[] randomizer);
    boolean setDeviceOutOfBandData(in String address, in byte[] hash, in byte[] randomizer);


    String getRemoteName(in String address);
    String getRemoteName(in String address);
    String getRemoteAlias(in String address);
    boolean setRemoteAlias(in String address, in String name);
    int getRemoteClass(in String address);
    int getRemoteClass(in String address);
    ParcelUuid[] getRemoteUuids(in String address);
    ParcelUuid[] getRemoteUuids(in String address);
    boolean fetchRemoteUuids(in String address, in ParcelUuid uuid, in IBluetoothCallback callback);
    boolean fetchRemoteUuids(in String address, in ParcelUuid uuid, in IBluetoothCallback callback);
+2 −0
Original line number Original line Diff line number Diff line
@@ -418,6 +418,8 @@ class BluetoothEventLoop {
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
            intent.putExtra(BluetoothDevice.EXTRA_NAME, propValues[1]);
            intent.putExtra(BluetoothDevice.EXTRA_NAME, propValues[1]);
            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
        } else if (name.equals("Alias")) {
            mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]);
        } else if (name.equals("Class")) {
        } else if (name.equals("Class")) {
            mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]);
            mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]);
            Intent intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
            Intent intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED);
+36 −1
Original line number Original line Diff line number Diff line
@@ -851,7 +851,6 @@ public class BluetoothService extends IBluetooth.Stub {
        return uuids;
        return uuids;
    }
    }



    /**
    /**
     * Returns the user-friendly name of a remote device.  This value is
     * Returns the user-friendly name of a remote device.  This value is
     * returned from our local cache, which is updated when onPropertyChange
     * returned from our local cache, which is updated when onPropertyChange
@@ -871,6 +870,40 @@ public class BluetoothService extends IBluetooth.Stub {
        return mDeviceProperties.getProperty(address, "Name");
        return mDeviceProperties.getProperty(address, "Name");
    }
    }


    /**
     * Returns alias of a remote device.  This value is returned from our
     * local cache, which is updated when onPropertyChange event is received.
     *
     * @param address Bluetooth address of remote device.
     *
     * @return The alias of the specified remote device.
     */
    public synchronized String getRemoteAlias(String address) {

        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            return null;
        }
        return mDeviceProperties.getProperty(address, "Alias");
    }

    /**
     * Set the alias of a remote device.
     *
     * @param address Bluetooth address of remote device.
     * @param alias new alias to change to
     * @return true on success, false on error
     */
    public synchronized boolean setRemoteAlias(String address, String alias) {
        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            return false;
        }

        return setDevicePropertyStringNative(getObjectPathFromAddress(address),
                                             "Alias", alias);
    }

    /**
    /**
     * Get the discoverability window for the device.  A timeout of zero
     * Get the discoverability window for the device.  A timeout of zero
     * means that the device is permanently discoverable (if the device is
     * means that the device is permanently discoverable (if the device is
@@ -2626,6 +2659,8 @@ public class BluetoothService extends IBluetooth.Stub {


    private native boolean setDevicePropertyBooleanNative(String objectPath, String key,
    private native boolean setDevicePropertyBooleanNative(String objectPath, String key,
            int value);
            int value);
    private native boolean setDevicePropertyStringNative(String objectPath, String key,
            String value);
    private native boolean createDeviceNative(String address);
    private native boolean createDeviceNative(String address);
    /*package*/ native boolean discoverServicesNative(String objectPath, String pattern);
    /*package*/ native boolean discoverServicesNative(String objectPath, String pattern);


+14 −0
Original line number Original line Diff line number Diff line
@@ -895,6 +895,18 @@ static jboolean setDevicePropertyBooleanNative(JNIEnv *env, jobject object,
#endif
#endif
}
}


static jboolean setDevicePropertyStringNative(JNIEnv *env, jobject object,
                                              jstring path, jstring key, jstring value) {
#ifdef HAVE_BLUETOOTH
    const char *c_value = env->GetStringUTFChars(value, NULL);
    jboolean ret = setDevicePropertyNative(env, object, path, key,
                                           (void *)&c_value, DBUS_TYPE_STRING);
    env->ReleaseStringUTFChars(value, (char *)c_value);
    return ret;
#else
    return JNI_FALSE;
#endif
}


static jboolean createDeviceNative(JNIEnv *env, jobject object,
static jboolean createDeviceNative(JNIEnv *env, jobject object,
                                                jstring address) {
                                                jstring address) {
@@ -1718,6 +1730,8 @@ static JNINativeMethod sMethods[] = {
            (void *)cancelPairingUserInputNative},
            (void *)cancelPairingUserInputNative},
    {"setDevicePropertyBooleanNative", "(Ljava/lang/String;Ljava/lang/String;I)Z",
    {"setDevicePropertyBooleanNative", "(Ljava/lang/String;Ljava/lang/String;I)Z",
            (void *)setDevicePropertyBooleanNative},
            (void *)setDevicePropertyBooleanNative},
    {"setDevicePropertyStringNative", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z",
            (void *)setDevicePropertyStringNative},
    {"createDeviceNative", "(Ljava/lang/String;)Z", (void *)createDeviceNative},
    {"createDeviceNative", "(Ljava/lang/String;)Z", (void *)createDeviceNative},
    {"discoverServicesNative", "(Ljava/lang/String;Ljava/lang/String;)Z", (void *)discoverServicesNative},
    {"discoverServicesNative", "(Ljava/lang/String;Ljava/lang/String;)Z", (void *)discoverServicesNative},
    {"addRfcommServiceRecordNative", "(Ljava/lang/String;JJS)I", (void *)addRfcommServiceRecordNative},
    {"addRfcommServiceRecordNative", "(Ljava/lang/String;JJS)I", (void *)addRfcommServiceRecordNative},