aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Hallstrom <hallstrom@google.com>2022-04-05 02:42:01 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-04-05 02:42:01 +0000
commit032f80d701d6ed5e0060b37a7c2cc3f4a19efd6e (patch)
tree7244cdd13a194a1689551e00cf28fee3fd55119b
parent46ae5774b3198abe39e2a8cbfa80d74a0a660bf1 (diff)
parentb10ceb06e3a57b9d832d508dcf6348fee0515400 (diff)
downloadsl4a-032f80d701d6ed5e0060b37a7c2cc3f4a19efd6e.tar.gz
Merge "Create Bond OOB" am: eba9e2aeae am: b10ceb06e3
Original change: https://android-review.googlesource.com/c/platform/external/sl4a/+/2032866 Change-Id: I097224e6c88e9df5f1fc7f76fb99c3098f0f6493 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java80
1 files changed, 78 insertions, 2 deletions
diff --git a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
index 18efed8a..aeb9980e 100644
--- a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothConnectionFacade.java
@@ -83,8 +83,12 @@ public class BluetoothConnectionFacade extends RpcReceiver {
Log.d("Transport: " + transport);
Log.d("OobData: " + data);
Bundle results = new Bundle();
- results.putInt("Transport", transport);
- results.putParcelable("OobData", data);
+ results.putInt("transport", transport);
+ // Just what we need create a bond
+ results.putString("address_with_type",
+ toHexString(data.getDeviceAddressWithType()));
+ results.putString("confirmation", toHexString(data.getConfirmationHash()));
+ results.putString("randomizer", toHexString(data.getRandomizerHash()));
mEventFacade.postEvent("GeneratedOobData", results.clone());
}
};
@@ -601,6 +605,78 @@ public class BluetoothConnectionFacade extends RpcReceiver {
}
+ private static byte[] hexStringToByteArray(String s) {
+ if (s == null) {
+ throw new IllegalArgumentException("Hex String must not be null!");
+ }
+ int len = s.length();
+ if ((len % 2) != 0 || len < 1) { // Multiple of 2 or empty
+ throw new IllegalArgumentException("Hex String must be an even number > 0");
+ }
+ byte[] data = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ data[i / 2] = (byte) ((byte) (Character.digit(s.charAt(i), 16) << 4)
+ + (byte) Character.digit(s.charAt(i + 1), 16));
+ }
+ return data;
+ }
+
+ private static String toHexString(byte[] a) {
+ if (a == null) return null;
+ StringBuilder builder = new StringBuilder(a.length * 2);
+ for (byte b : a) {
+ builder.append(String.format("%02x", b));
+ }
+ return builder.toString();
+ }
+
+ /**
+ * Bond to a device using Out of Band Data.
+ *
+ * @param address String representation of address like "00:11:22:33:44:55"
+ * @param transport String "1", "2", "3" to match TRANSPORT_*
+ * @param c Hex String of the 16 octet confirmation
+ * @param r Hex String of the 16 octet randomizer
+ */
+ @Rpc(description = "Creates and Out of Band bond.")
+ public void bluetoothCreateBondOutOfBand(@RpcParameter(name = "address") String address,
+ @RpcParameter(name = "transport") String transport,
+ @RpcParameter(name = "c") String c, @RpcParameter(name = "r") String r) {
+ Log.d("bluetoothCreateBondOutOfBand(" + address + ", " + transport + "," + c + ", "
+ + r + ")");
+ BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(address);
+ byte[] addressBytes = new byte[7];
+ int i = 0;
+ for (String s : address.split(":")) {
+ addressBytes[i] = hexStringToByteArray(s)[0];
+ i++;
+ }
+ addressBytes[i] = 0x01;
+ OobData p192 = null;
+ OobData p256 = new OobData.LeBuilder(hexStringToByteArray(c),
+ addressBytes, OobData.LE_DEVICE_ROLE_BOTH_PREFER_CENTRAL)
+ .setRandomizerHash(hexStringToByteArray(r))
+ .build();
+ mContext.registerReceiver(new BondBroadcastReceiver(),
+ new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
+ remoteDevice.createBondOutOfBand(Integer.parseInt(transport), p192, p256);
+ }
+
+ private class BondBroadcastReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.d("BondBroadcastReceiver onReceive(" + context + ", " + intent + ")");
+ int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
+ BluetoothDevice.BOND_NONE);
+ if (state == BluetoothDevice.BOND_BONDED) {
+ Bundle event = new Bundle();
+ event.putBoolean("bonded_state", state == BluetoothDevice.BOND_BONDED);
+ mEventFacade.postEvent("Bonded", event);
+ mContext.unregisterReceiver(this);
+ }
+ }
+ }
+
@Rpc(description = "Return list of connected bluetooth devices over a profile",
returns = "List of devices connected over the profile")
public List<BluetoothDevice> bluetoothGetConnectedDevicesOnProfile(