This repository was archived by the owner on Mar 15, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +18
-26
lines changed
app/src/main/java/com/q42/milightremotecontrol Expand file tree Collapse file tree 2 files changed +18
-26
lines changed Original file line number Diff line number Diff line change @@ -28,32 +28,13 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
28
28
setContentView(R .layout.activity_main)
29
29
30
30
controller = MiLightController (this @MainActivity)
31
- // controller.discover()
31
+
32
32
launch(Dispatchers .IO ) {
33
- val ip = try {
34
- InetAddress .getByName(bridge)
35
- } catch (ex: UnknownHostException ) {
36
- withContext(Dispatchers .Main ) {
37
- toast(" Error while looking for bridge:\n ${ex.message} " )
38
- }
39
- null
40
- }
41
- ip?.let {
42
- Log .i(TAG , " $ip " )
43
- controller.bridgeAddress = ip
44
- }
33
+ discoverBridge()
45
34
}
46
35
47
36
discover.setOnClickListener {
48
- launch(Dispatchers .IO ) {
49
-
50
- val bridgeAddress = async { controller.discover() }
51
-
52
- withContext(Dispatchers .Main ) {
53
-
54
- bridge_id.text = bridgeAddress.await().toString()
55
- }
56
- }
37
+ discoverBridge()
57
38
}
58
39
59
40
on_button.setOnClickListener {
@@ -102,6 +83,18 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
102
83
}
103
84
}
104
85
86
+ fun discoverBridge () {
87
+ launch(Dispatchers .IO ) {
88
+
89
+ val bridgeAddress = async { controller.discover() }
90
+
91
+ withContext(Dispatchers .Main ) {
92
+
93
+ bridge_id.text = bridgeAddress.await().toString()
94
+ }
95
+ }
96
+ }
97
+
105
98
106
99
override fun onDestroy () {
107
100
super .onDestroy()
Original file line number Diff line number Diff line change @@ -48,17 +48,18 @@ class MiLightController(
48
48
socket.receive(receivePacket)
49
49
val session1 = UByte .fromByte(receivePacket.data[19 ]).toInt()
50
50
val session2 = UByte .fromByte(receivePacket.data[20 ]).toInt()
51
- Log .i (TAG , " session id: $session1 $session2 " )
51
+ Log .d (TAG , " session id: $session1 $session2 " )
52
52
return Session (socket, session1, session2)
53
53
}
54
54
55
55
private fun sendCommand (cmd : ByteArray ) {
56
56
57
57
if (bridgeAddress == null ) {
58
- Log .i (TAG , " NO BRIDGE" )
58
+ Log .w (TAG , " NO BRIDGE" )
59
59
return
60
60
}
61
61
62
+ Log .d(TAG , " Request session" )
62
63
launch(Dispatchers .IO ) {
63
64
try {
64
65
// request session
@@ -122,8 +123,6 @@ class MiLightController(
122
123
withContext(Dispatchers .Main ) {
123
124
Log .i(TAG , (" received ${response.length} bytes from ${response.address} : $data " ))
124
125
}
125
-
126
- socket.close()
127
126
}
128
127
129
128
bridgeAddress = response.address
You can’t perform that action at this time.
0 commit comments