OSDN Git Service

Add support for setting passkey as pairing variant
[android-x86/frameworks-base.git] / core / java / android / bluetooth / BluetoothDevice.java
index 5e50b69..ad723f1 100644 (file)
@@ -18,6 +18,7 @@ package android.bluetooth;
 
 import android.annotation.SdkConstant;
 import android.annotation.SdkConstant.SdkConstantType;
+import android.annotation.SystemApi;
 import android.content.Context;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -28,6 +29,8 @@ import android.util.Log;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.util.UUID;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
 
 /**
  * Represents a remote Bluetooth device. A {@link BluetoothDevice} lets you
@@ -67,6 +70,14 @@ public final class BluetoothDevice implements Parcelable {
     private static final boolean DBG = false;
 
     /**
+     * Connection state bitmask as returned by getConnectionState.
+     */
+    private static final int CONNECTION_STATE_DISCONNECTED = 0;
+    private static final int CONNECTION_STATE_CONNECTED = 1;
+    private static final int CONNECTION_STATE_ENCRYPTED_BREDR = 2;
+    private static final int CONNECTION_STATE_ENCRYPTED_LE = 4;
+
+    /**
      * Sentinel error value for this class. Guaranteed to not equal any other
      * integer constant in this class. Provided as a convenience for functions
      * that require a sentinel error value, for example:
@@ -263,6 +274,7 @@ public final class BluetoothDevice implements Parcelable {
      * intents to indicate pairing method used. Possible values are:
      * {@link #PAIRING_VARIANT_PIN},
      * {@link #PAIRING_VARIANT_PASSKEY_CONFIRMATION},
+     * {@link #PAIRING_VARIANT_PASSKEY},
      */
     public static final String EXTRA_PAIRING_VARIANT =
             "android.bluetooth.device.extra.PAIRING_VARIANT";
@@ -474,7 +486,6 @@ public final class BluetoothDevice implements Parcelable {
 
     /**
      * The user will be prompted to enter a passkey
-     * @hide
      */
     public static final int PAIRING_VARIANT_PASSKEY = 1;
 
@@ -940,13 +951,36 @@ public final class BluetoothDevice implements Parcelable {
      * @return True if there is at least one open connection to this device.
      * @hide
      */
+    @SystemApi
     public boolean isConnected() {
         if (sService == null) {
             // BT is not enabled, we cannot be connected.
             return false;
         }
         try {
-            return sService.isConnected(this);
+            return sService.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED;
+        } catch (RemoteException e) {
+            Log.e(TAG, "", e);
+            return false;
+        }
+    }
+
+    /**
+     * Returns whether there is an open connection to this device
+     * that has been encrypted.
+     * <p>Requires {@link android.Manifest.permission#BLUETOOTH}.
+     *
+     * @return True if there is at least one encrypted connection to this device.
+     * @hide
+     */
+    @SystemApi
+    public boolean isEncrypted() {
+        if (sService == null) {
+            // BT is not enabled, we cannot be connected.
+            return false;
+        }
+        try {
+            return sService.getConnectionState(this) > CONNECTION_STATE_CONNECTED;
         } catch (RemoteException e) {
             Log.e(TAG, "", e);
             return false;
@@ -1062,13 +1096,27 @@ public final class BluetoothDevice implements Parcelable {
         return false;
     }
 
-    /** @hide */
+    /**
+     * Set the passkey during pairing when the pairing method is
+     * {@link #PAIRING_VARIANT_PASSKEY}
+     * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}.
+     *
+     * @return true if passkey has been set
+     *         flase for error
+     */
     public boolean setPasskey(int passkey) {
-        //TODO(BT)
-        /*
+        if (sService == null) {
+            Log.e(TAG, "BT not enabled. Cannot set Remote Device passkey");
+            return false;
+        }
+        ByteBuffer buff = ByteBuffer.allocate(5);
+        buff.order(ByteOrder.nativeOrder());
+        buff.putInt(passkey);
+        byte[] passkeyByte = buff.array();
+
         try {
-            return sService.setPasskey(this, true, 4, passkey);
-        } catch (RemoteException e) {Log.e(TAG, "", e);}*/
+            return sService.setPasskey(this, true, passkeyByte.length, passkeyByte);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }