Skip to content

Commit

Permalink
[Android] Support Manual Pairing Code (#25333)
Browse files Browse the repository at this point in the history
* Support Manual Pairing Code in Android

* Modify some errors
  • Loading branch information
joonhaengHeo authored and pull[bot] committed Nov 1, 2023
1 parent 9bcff24 commit e84f9d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ data class CHIPDeviceInfo(
var commissioningFlow: Int = 0,
val optionalQrCodeInfoMap: Map<Int, QrCodeInfo> = mapOf(),
val discoveryCapabilities: Set<DiscoveryCapability> = 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,
Expand All @@ -56,6 +57,7 @@ data class CHIPDeviceInfo(
)
},
setupPayload.discoveryCapabilities,
isShortDiscriminator
)
}
}
Expand Down

0 comments on commit e84f9d7

Please sign in to comment.