From 26671812bf655ac40d9e39fcfcba4228cf44f152 Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Thu, 2 Mar 2023 05:31:03 +0900 Subject: [PATCH] [Android] Support Manual Pairing Code (#25333) * Support Manual Pairing Code in Android * Modify some errors --- .../chip/chiptool/bluetooth/BluetoothManager.kt | 14 +++++++++++++- .../provisioning/DeviceProvisioningFragment.kt | 2 +- .../setuppayloadscanner/BarcodeFragment.kt | 13 ++++++++++++- .../chiptool/setuppayloadscanner/CHIPDeviceInfo.kt | 4 +++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt index 7d83ee731b7e2c..4618a0591f9044 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt @@ -43,8 +43,18 @@ class BluetoothManager : BleCallback { .toByteArray() } + private fun getServiceDataMask(isShortDiscriminator: Boolean): ByteArray { + val shortDiscriminatorMask = when(isShortDiscriminator) { + true -> 0x00 false -> 0xff + } + return intArrayOf(0xff, shortDiscriminatorMask, 0xff).map { it.toByte() }.toByteArray() + } + suspend fun getBluetoothDevice(context: Context, discriminator: Int): BluetoothDevice? { + return getBluetoothDevice(context, discriminator, false) + } + suspend fun getBluetoothDevice(context: Context, discriminator: Int, isShortDiscriminator: Boolean): BluetoothDevice? { if (! bluetoothAdapter.isEnabled) { bluetoothAdapter.enable(); } @@ -75,9 +85,11 @@ class BluetoothManager : BleCallback { } val serviceData = getServiceData(discriminator) + val serviceDataMask = getServiceDataMask(isShortDiscriminator) + val scanFilter = ScanFilter.Builder() - .setServiceData(ParcelUuid(UUID.fromString(CHIP_UUID)), serviceData) + .setServiceData(ParcelUuid(UUID.fromString(CHIP_UUID)), serviceData, serviceDataMask) .build() val scanSettings = ScanSettings.Builder() diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt index 3a7fe9a460bf78..fa6c2bf7883db0 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/DeviceProvisioningFragment.kt @@ -169,7 +169,7 @@ class DeviceProvisioningFragment : Fragment() { R.string.rendezvous_over_ble_scanning_text, deviceInfo.discriminator.toString() ) - val device = bluetoothManager.getBluetoothDevice(requireContext(), deviceInfo.discriminator) ?: run { + val device = bluetoothManager.getBluetoothDevice(requireContext(), deviceInfo.discriminator, deviceInfo.isShortDiscriminator) ?: run { showMessage(R.string.rendezvous_over_ble_scanning_failed_text) return@launch } diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt index ddc55ccd65179f..e591fd64ebd948 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt @@ -38,6 +38,8 @@ import androidx.core.content.ContextCompat.checkSelfPermission import androidx.fragment.app.Fragment import chip.setuppayload.SetupPayload import chip.setuppayload.SetupPayloadParser +import chip.setuppayload.SetupPayloadParser.SetupPayloadException +import chip.setuppayload.SetupPayloadParser.InvalidEntryCodeFormatException import chip.setuppayload.SetupPayloadParser.UnrecognizedQrCodeException import com.google.chip.chiptool.R import com.google.chip.chiptool.SelectActionFragment @@ -181,14 +183,23 @@ class BarcodeFragment : Fragment() { private fun handleInputQrCode(qrCode: String) { lateinit var payload: SetupPayload + var isShortDiscriminator = false try { payload = SetupPayloadParser().parseQrCode(qrCode) + } catch (ex: SetupPayloadException) { + try { + payload = SetupPayloadParser().parseManualEntryCode(qrCode) + isShortDiscriminator = true + } catch (ex: Exception) { + Log.e(TAG, "Unrecognized Manual Pairing Code", ex) + Toast.makeText(requireContext(), "Unrecognized Manual Pairing Code", Toast.LENGTH_SHORT).show() + } } catch (ex: UnrecognizedQrCodeException) { Log.e(TAG, "Unrecognized QR Code", ex) Toast.makeText(requireContext(), "Unrecognized QR Code", Toast.LENGTH_SHORT).show() } FragmentUtil.getHost(this@BarcodeFragment, Callback::class.java) - ?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload)) + ?.onCHIPDeviceInfoReceived(CHIPDeviceInfo.fromSetupPayload(payload, isShortDiscriminator)) } private fun handleScannedQrCode(barcode: Barcode) { diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPDeviceInfo.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPDeviceInfo.kt index 6e011110aa5903..d4a76221365080 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPDeviceInfo.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/CHIPDeviceInfo.kt @@ -34,12 +34,13 @@ data class CHIPDeviceInfo( var commissioningFlow: Int = 0, val optionalQrCodeInfoMap: Map = mapOf(), val discoveryCapabilities: Set = setOf(), + val isShortDiscriminator: Boolean = false, val ipAddress: String? = null, ) : Parcelable { companion object { - fun fromSetupPayload(setupPayload: SetupPayload): CHIPDeviceInfo { + fun fromSetupPayload(setupPayload: SetupPayload, isShortDiscriminator: Boolean = false): CHIPDeviceInfo { return CHIPDeviceInfo( setupPayload.version, setupPayload.vendorId, @@ -56,6 +57,7 @@ data class CHIPDeviceInfo( ) }, setupPayload.discoveryCapabilities, + isShortDiscriminator ) } }