@@ -49,23 +49,21 @@ class WifiActivity : AppCompatActivity() {
4949 override fun onReceive (context : Context , intent : Intent ) {
5050 doScan()
5151
52- if (rssiRecord[ssidList[0 ]]!! .size >= size
53- && rssiRecord[ssidList[1 ]]!! .size >= size
54- && rssiRecord[ssidList[2 ]]!! .size >= size
52+ if (rssiRecord[ssidList[0 ]]!! .size >= 10
53+ && rssiRecord[ssidList[1 ]]!! .size >= 10
54+ && rssiRecord[ssidList[2 ]]!! .size >= 10
5555 ) {
5656 unregisterReceiver(this )
5757 Toast .makeText(applicationContext, " Scanning is finished." , Toast .LENGTH_SHORT ).show()
5858 binding.scanButton.isEnabled = false
5959 binding.sendButton.isEnabled = true
60- binding.scanButton2.isEnabled = false
61- binding.localizationButton.isEnabled = true
60+ binding.localizationButton.isEnabled = false
6261 }
6362 }
6463 }
6564
6665 private val ssidList = mutableListOf<String >()
6766 private val rssiRecord = mutableMapOf<String , MutableList <Int >>()
68- private var size = 25
6967
7068 // permissions
7169 private var permissionAccepted = false
@@ -142,13 +140,10 @@ class WifiActivity : AppCompatActivity() {
142140 rssiRecord[ssid1] = mutableListOf ()
143141 rssiRecord[ssid2] = mutableListOf ()
144142 rssiRecord[ssid3] = mutableListOf ()
145- size = 25
146143
147144 binding.scanButton.isEnabled = false
148145 binding.sendButton.isEnabled = false
149- binding.scanButton2.isEnabled = false
150146 binding.localizationButton.isEnabled = false
151-
152147 doScan()
153148 registerReceiver(wifiReceiver, IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ))
154149 }
@@ -158,75 +153,22 @@ class WifiActivity : AppCompatActivity() {
158153 }
159154
160155 binding.sendButton.setOnClickListener {
161- val builder = AlertDialog .Builder (this )
162- val dialogView = layoutInflater.inflate(R .layout.dialog_section, null )
163-
164- builder.setView(dialogView)
165- .setPositiveButton(" OK" ) { _, _ ->
166- try {
167- val section = dialogView.findViewById<EditText >(R .id.sectionEditText).text.toString().toInt()
168- if (saveExcel(section)) {
169- runBlocking {
170- saveSensorData(section)
171- }
172- }
173- } catch (e: Exception ) {
174- Toast .makeText(application, " Wrong section number" , Toast .LENGTH_SHORT ).show()
175- }
176-
177- binding.scanButton.isEnabled = true
178- binding.sendButton.isEnabled = false
179- binding.scanButton2.isEnabled = true
180- binding.localizationButton.isEnabled = true
181- }
182- .setNegativeButton(" Cancel" ) { _, _ -> }
183-
184- builder.create().show()
185- }
186-
187- binding.scanButton2.setOnClickListener {
188- val builder = AlertDialog .Builder (this )
189- val dialogView = layoutInflater.inflate(R .layout.dialog_ssid, null )
190- dialogView.findViewById<EditText >(R .id.ssidEditText1).setText(pref.getString(" ssid1" , " " ))
191- dialogView.findViewById<EditText >(R .id.ssidEditText2).setText(pref.getString(" ssid2" , " " ))
192- dialogView.findViewById<EditText >(R .id.ssidEditText3).setText(pref.getString(" ssid3" , " " ))
193-
194- builder.setView(dialogView)
195- .setPositiveButton(" OK" ) { _, _ ->
196- val ssid1 = dialogView.findViewById<EditText >(R .id.ssidEditText1).text.toString()
197- val ssid2 = dialogView.findViewById<EditText >(R .id.ssidEditText2).text.toString()
198- val ssid3 = dialogView.findViewById<EditText >(R .id.ssidEditText3).text.toString()
199- editor.putString(" ssid1" , ssid1).apply ()
200- editor.putString(" ssid2" , ssid2).apply ()
201- editor.putString(" ssid3" , ssid3).apply ()
202- ssidList.clear()
203- ssidList.add(ssid1)
204- ssidList.add(ssid2)
205- ssidList.add(ssid3)
206- rssiRecord.clear()
207- rssiRecord[ssid1] = mutableListOf ()
208- rssiRecord[ssid2] = mutableListOf ()
209- rssiRecord[ssid3] = mutableListOf ()
210- size = 10
211-
212- binding.scanButton.isEnabled = false
213- binding.sendButton.isEnabled = false
214- binding.scanButton2.isEnabled = false
215- binding.localizationButton.isEnabled = false
216- doScan()
217- registerReceiver(wifiReceiver, IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ))
156+ if (saveExcel()) {
157+ runBlocking {
158+ saveSensorData()
218159 }
219- .setNegativeButton( " Cancel " ) { _, _ -> }
160+ }
220161
221- builder.create().show()
162+ binding.scanButton.isEnabled = true
163+ binding.sendButton.isEnabled = false
164+ binding.localizationButton.isEnabled = true
222165 }
223166
224167 binding.localizationButton.setOnClickListener {
225168 runBlocking {
226169 doLocalization()
227170 binding.scanButton.isEnabled = true
228171 binding.sendButton.isEnabled = false
229- binding.scanButton2.isEnabled = true
230172 binding.localizationButton.isEnabled = false
231173 }
232174 }
@@ -261,7 +203,7 @@ class WifiActivity : AppCompatActivity() {
261203 val rssi = it.find { it.SSID == ssid }?.level
262204 if (rssi != null ) {
263205 append(" RSSI: $rssi \n\n " )
264- if ((rssiRecord[ssid]?.size ? : 0 ) < size ) {
206+ if ((rssiRecord[ssid]?.size ? : 0 ) < 10 ) {
265207 rssiRecord[ssid]?.add(rssi)
266208 }
267209 } else {
@@ -275,8 +217,9 @@ class WifiActivity : AppCompatActivity() {
275217 }
276218 }
277219
278- private fun saveExcel (section : Int ): Boolean {
220+ private fun saveExcel (): Boolean {
279221 if (! isExternalStorageWritable()) {
222+ Toast .makeText(applicationContext, " Please allow app permissions." , Toast .LENGTH_SHORT ).show()
280223 return false
281224 }
282225
@@ -289,7 +232,7 @@ class WifiActivity : AppCompatActivity() {
289232 row.createCell(2 ).setCellValue(" Y" )
290233 row.createCell(3 ).setCellValue(" Z" )
291234
292- for (i in 0 until size ) {
235+ for (i in 0 until 10 ) {
293236 row = this .createRow(i + 1 )
294237 row.createCell(0 ).setCellValue(0.0 )
295238 for (j in ssidList.indices) {
@@ -303,7 +246,7 @@ class WifiActivity : AppCompatActivity() {
303246 if (! File (dir).exists()) {
304247 File (dir).mkdirs()
305248 }
306- val excelFile = File (dir, " ${section} .xls" )
249+ val excelFile = File (dir, " rssi_data .xls" )
307250
308251 return try {
309252 workbook.write(FileOutputStream (excelFile))
@@ -316,10 +259,10 @@ class WifiActivity : AppCompatActivity() {
316259 }
317260 }
318261
319- private suspend fun saveSensorData (section : Int ) {
262+ private suspend fun saveSensorData () {
320263 val file = File (
321264 Environment .getExternalStorageDirectory().absolutePath +
322- " /Indoor Positioning System/${section} .xls"
265+ " /Indoor Positioning System/rssi_data .xls"
323266 )
324267
325268 // 1st parameter: MediaType 으로 보내는 파일의 타입을 정하는 것
@@ -362,7 +305,10 @@ class WifiActivity : AppCompatActivity() {
362305 if (response.isSuccessful) {
363306 val result = response.body()
364307 Handler (Looper .getMainLooper()).post {
365- binding.resultTextView.text = " Section: $result "
308+ binding.resultTextView.text = buildString {
309+ append(" Section: " )
310+ append(result)
311+ }
366312 Toast .makeText(applicationContext, " Section: $result " , Toast .LENGTH_LONG ).show()
367313 }
368314 } else {
0 commit comments