Add BLE scan filter method for IRK in hex string format

Add a variation of bleSetScanFilterDeviceAddressTypeAndIrk method that
takes in an IRK in hex string format.

This approach was decided for simplicity for callers, since the
conversion from bytes->String in python and back to bytes in Java is
non-trivial (requires including charset to use, logic on either end to
translate the object - providing a method to handle conversion from hex
format in Java is much more straightforward than possible alternative).

Bug: 222349537
Test: gd/cert/run --device --sl4a_sl4a ConnectWithIrkTest
Merged-In: I5e5ab000d3c78f84bfd5411cee84a2f5661899c9

Change-Id: I5e5ab000d3c78f84bfd5411cee84a2f5661899c9
diff --git a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
index 5913027..290cacf 100644
--- a/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/bluetooth/BluetoothLeScanFacade.java
@@ -44,6 +44,7 @@
 import com.googlecode.android_scripting.rpc.RpcOptional;
 import com.googlecode.android_scripting.rpc.RpcParameter;
 
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -896,6 +897,40 @@
     }
 
     /**
+     * Add filter "macAddress", "addressType", and "irk" to existing ScanFilter
+     *
+     * @param macAddress the macAddress to filter against
+     * @param addressType the type of macAddress to filter against
+     * @param irk IRK for address resolution in hex format
+     * @throws Exception
+     */
+    @Rpc(description =
+            "Add filter \"macAddress\", \"addressType\", and \"irk\" to existing ScanFilter")
+    public void bleSetScanFilterDeviceAddressTypeAndIrkHexString(
+            @RpcParameter(name = "macAddress") String macAddress,
+            @RpcParameter(name = "addressType") Integer addressType,
+            @RpcParameter(name = "irk") String irk
+    ) throws UnsupportedEncodingException {
+        mScanFilterBuilder.setDeviceAddress(macAddress, addressType, hexStringToByteArray(irk));
+    }
+
+    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;
+    }
+
+    /**
      * Add filter "manufacturereDataId and/or manufacturerData" to existing
      * ScanFilter
      * @param manufacturerDataId the manufacturer data id to filter against