From 16459402316fd979faeba500c8beb046a9803ea0 Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Wed, 3 Nov 2021 01:28:17 +0900 Subject: [PATCH] Android add discover (#10682) * Add Android Discover * Fix Build error * Modify coding rule, string xml * Modify coding style * Restyled by google-java-format * Restyled by gn * Change browse using android platform layer * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format * Restyled by clang-format * Modify after review * Remove unused code * Restyled by google-java-format * Restyled by gn * Modify some issue * Fix some issue after reviewing * restore file * Restyled by clang-format Co-authored-by: Restyled.io --- .../com/tcl/chip/chiptest/MainActivity.kt | 2 +- .../com/google/chip/chiptool/ChipClient.kt | 2 +- .../AddressCommissioningFragment.kt | 46 +++++ .../layout/address_commissioning_fragment.xml | 23 +++ .../app/src/main/res/values/strings.xml | 1 + src/controller/java/BUILD.gn | 1 + .../java/CHIPDeviceController-JNI.cpp | 49 +++++ .../ChipDeviceController.java | 16 ++ .../devicecontroller/DiscoveredDevice.java | 23 +++ .../android/AndroidChipPlatform-JNI.cpp | 19 +- src/platform/android/BUILD.gn | 2 + src/platform/android/DnssdImpl.cpp | 175 +++++++++++++++++- src/platform/android/DnssdImpl.h | 9 +- .../chip/platform/AndroidChipPlatform.java | 37 +++- .../java/chip/platform/ChipMdnsCallback.java | 7 + .../chip/platform/ChipMdnsCallbackImpl.java | 15 ++ .../platform/NsdManagerServiceBrowser.java | 154 +++++++++++++++ .../platform/NsdManagerServiceResolver.java | 11 +- .../java/chip/platform/ServiceBrowser.java | 27 +++ 19 files changed, 589 insertions(+), 30 deletions(-) create mode 100644 src/controller/java/src/chip/devicecontroller/DiscoveredDevice.java create mode 100644 src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java create mode 100644 src/platform/android/java/chip/platform/ServiceBrowser.java 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" /> + + +