diff --git a/src/android/CHIPTest/app/src/main/java/com/tcl/chip/chiptest/MainActivity.kt b/src/android/CHIPTest/app/src/main/java/com/tcl/chip/chiptest/MainActivity.kt index d1f4fa14ec8ff7..67e78748dc1cfd 100644 --- a/src/android/CHIPTest/app/src/main/java/com/tcl/chip/chiptest/MainActivity.kt +++ b/src/android/CHIPTest/app/src/main/java/com/tcl/chip/chiptest/MainActivity.kt @@ -53,6 +53,6 @@ class MainActivity : AppCompatActivity() { }.start() } - AndroidChipPlatform(AndroidBleManager(), PreferencesKeyValueStoreManager(this), PreferencesConfigurationManager(this), NsdManagerServiceResolver(this), ChipMdnsCallbackImpl()) + AndroidChipPlatform.defaultInstance(this) } } diff --git a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt index 7fcdfeb46f706f..322495a3b29041 100644 --- a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt +++ b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/ChipClient.kt @@ -50,7 +50,7 @@ object ChipClient { if (!this::androidPlatform.isInitialized && context != null) { //force ChipDeviceController load jni ChipDeviceController.loadJni() - androidPlatform = AndroidChipPlatform(AndroidBleManager(), PreferencesKeyValueStoreManager(context), PreferencesConfigurationManager(context), NsdManagerServiceResolver(context), ChipMdnsCallbackImpl()) + androidPlatform = AndroidChipPlatform.defaultInstance(context) } return androidPlatform } diff --git a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/AddressCommissioningFragment.kt b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/AddressCommissioningFragment.kt index dd7015fac13dd6..27aa3be27a912c 100644 --- a/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/AddressCommissioningFragment.kt +++ b/src/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/AddressCommissioningFragment.kt @@ -5,6 +5,8 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment import com.google.chip.chiptool.ChipClient import com.google.chip.chiptool.R @@ -13,10 +15,21 @@ import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceInfo import com.google.chip.chiptool.util.FragmentUtil import kotlinx.android.synthetic.main.address_commissioning_fragment.addressEditText import kotlinx.android.synthetic.main.address_commissioning_fragment.commissionBtn +import kotlinx.android.synthetic.main.address_commissioning_fragment.discoverBtn +import kotlinx.android.synthetic.main.address_commissioning_fragment.discoverListSpinner import kotlinx.android.synthetic.main.address_commissioning_fragment.discriminatorEditText import kotlinx.android.synthetic.main.address_commissioning_fragment.pincodeEditText +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch + class AddressCommissioningFragment : Fragment() { + private val ipAddressList = ArrayList() + private val scope = CoroutineScope(Dispatchers.Main + Job()) + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -46,6 +59,39 @@ class AddressCommissioningFragment : Fragment() { ) ) } + + discoverBtn.setOnClickListener { _ -> + discoverBtn.isEnabled = false + val deviceController = ChipClient.getDeviceController(requireContext()) + deviceController.discoverCommissionableNodes() + scope.launch { + delay(7000) + updateSpinner() + discoverBtn.isEnabled = true + } + } + } + + private fun updateSpinner() { + val deviceController = ChipClient.getDeviceController(requireContext()) + for(i in 0..10) { + val device = deviceController.getDiscoveredDevice(i) ?: break + ipAddressList.add("${device.ipAddress}, ${device.discriminator}") + } + requireActivity().runOnUiThread { + discoverListSpinner.adapter = + ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, ipAddressList) + discoverListSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { + val address = ipAddressList[position].split(",")[0].trim() + val discriminator = ipAddressList[position].split(",")[1].trim() + addressEditText.setText(address) + discriminatorEditText.setText(discriminator) + } + + override fun onNothingSelected(parent: AdapterView<*>) {} + } + } } companion object { diff --git a/src/android/CHIPTool/app/src/main/res/layout/address_commissioning_fragment.xml b/src/android/CHIPTool/app/src/main/res/layout/address_commissioning_fragment.xml index 21fafbd3ff9edf..4ff57d1767d7ed 100644 --- a/src/android/CHIPTool/app/src/main/res/layout/address_commissioning_fragment.xml +++ b/src/android/CHIPTool/app/src/main/res/layout/address_commissioning_fragment.xml @@ -16,6 +16,7 @@ app:layout_constraintTop_toTopOf="parent" /> + + +