Skip to content

Commit c3f482c

Browse files
committed
Improve performance of discovery service
The listener was registered way too many times and would duplicate the amount of work needed when discovering devices. It could grow exponentially worse.
1 parent 7663d35 commit c3f482c

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

app/src/main/java/ca/cgagnier/wlednativeandroid/MainActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ class MainActivity : AutoDiscoveryActivity, DeviceDiscovery.DeviceDiscoveredList
9898
}
9999

100100
override fun onResume() {
101+
(application as DevicesApplication).deviceDiscovery
102+
.registerDeviceDiscoveredListener(this)
101103
startAutoDiscovery()
102104
super.onResume()
103105
}
104106

105107
override fun onPause() {
108+
(application as DevicesApplication).deviceDiscovery
109+
.unregisterDeviceDiscoveredListener(this)
106110
stopAutoDiscovery()
107111
super.onPause()
108112
}
@@ -113,8 +117,6 @@ class MainActivity : AutoDiscoveryActivity, DeviceDiscovery.DeviceDiscoveredList
113117
return
114118
}
115119
Log.i(TAG, "Starting auto discovery")
116-
(application as DevicesApplication).deviceDiscovery
117-
.registerDeviceDiscoveredListener(this)
118120
(application as DevicesApplication).deviceDiscovery.start()
119121
autoDiscoveryLoopHandler.postDelayed({
120122
runOnUiThread {
@@ -126,8 +128,6 @@ class MainActivity : AutoDiscoveryActivity, DeviceDiscovery.DeviceDiscoveredList
126128
override fun stopAutoDiscovery() {
127129
Log.i(TAG, "Stopping auto discovery")
128130
autoDiscoveryLoopHandler.removeCallbacksAndMessages(null)
129-
(application as DevicesApplication).deviceDiscovery
130-
.unregisterDeviceDiscoveredListener(this)
131131
(application as DevicesApplication).deviceDiscovery.stop()
132132
}
133133

app/src/main/java/ca/cgagnier/wlednativeandroid/service/DeviceDiscovery.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class DeviceDiscovery(val context: Context) {
5555
}
5656

5757
override fun onDiscoveryStarted(serviceType: String?) {
58-
Log.d(TAG, "Service discovery started")
58+
Log.d(TAG, "Service discovery started: $serviceType")
5959
}
6060

6161
override fun onDiscoveryStopped(serviceType: String?) {
@@ -162,7 +162,7 @@ class DeviceDiscovery(val context: Context) {
162162
const val TAG = "DEVICE_DISCOVERY"
163163
const val SERVICE_TYPE = "_wled._tcp."
164164

165-
const val DEFAULT_WLED_AP_IP = "4.3.2.1"
165+
private const val DEFAULT_WLED_AP_IP = "4.3.2.1"
166166

167167

168168
@SuppressLint("WifiManagerPotentialLeak")

0 commit comments

Comments
 (0)