Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Support Manual Pairing Code #25333

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ class BluetoothManager : BleCallback {
.toByteArray()
}

private fun getServiceDataMask(isManualPairingMode: Boolean): ByteArray {
val manualPairingMask = when(isManualPairingMode) {
true -> 0x00 false -> 0xff
}
return intArrayOf(0xff, manualPairingMask, 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, isManualPairingMode: 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(isManualPairingMode)

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,8 +183,17 @@ class BarcodeFragment : Fragment() {

private fun handleInputQrCode(qrCode: String) {
lateinit var payload: SetupPayload
var isManualPairingCode = false
joonhaengHeo marked this conversation as resolved.
Show resolved Hide resolved
try {
payload = SetupPayloadParser().parseQrCode(qrCode)
} catch (ex: SetupPayloadException) {
try {
payload = SetupPayloadParser().parseManualEntryCode(qrCode)
isManualPairingCode = 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()
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