Skip to content

Commit

Permalink
불필요한 기능 삭제 및 데모 준비
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunJinNo committed Dec 12, 2023
1 parent 13b4e86 commit 41a03a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,5 @@ class MainActivity : AppCompatActivity() {

builder.create().show()
}

binding.testButton.setOnClickListener {
startActivity(Intent(this, WifiActivity::class.java))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,21 @@ class WifiActivity : AppCompatActivity() {
override fun onReceive(context: Context, intent: Intent) {
doScan()

if (rssiRecord[ssidList[0]]!!.size >= size
&& rssiRecord[ssidList[1]]!!.size >= size
&& rssiRecord[ssidList[2]]!!.size >= size
if (rssiRecord[ssidList[0]]!!.size >= 10
&& rssiRecord[ssidList[1]]!!.size >= 10
&& rssiRecord[ssidList[2]]!!.size >= 10
) {
unregisterReceiver(this)
Toast.makeText(applicationContext, "Scanning is finished.", Toast.LENGTH_SHORT).show()
binding.scanButton.isEnabled = false
binding.sendButton.isEnabled = true
binding.scanButton2.isEnabled = false
binding.localizationButton.isEnabled = true
binding.localizationButton.isEnabled = false
}
}
}

private val ssidList = mutableListOf<String>()
private val rssiRecord = mutableMapOf<String, MutableList<Int>>()
private var size = 25

// permissions
private var permissionAccepted = false
Expand Down Expand Up @@ -142,13 +140,10 @@ class WifiActivity : AppCompatActivity() {
rssiRecord[ssid1] = mutableListOf()
rssiRecord[ssid2] = mutableListOf()
rssiRecord[ssid3] = mutableListOf()
size = 25

binding.scanButton.isEnabled = false
binding.sendButton.isEnabled = false
binding.scanButton2.isEnabled = false
binding.localizationButton.isEnabled = false

doScan()
registerReceiver(wifiReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
}
Expand All @@ -158,75 +153,22 @@ class WifiActivity : AppCompatActivity() {
}

binding.sendButton.setOnClickListener {
val builder = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_section, null)

builder.setView(dialogView)
.setPositiveButton("OK") { _, _ ->
try {
val section = dialogView.findViewById<EditText>(R.id.sectionEditText).text.toString().toInt()
if (saveExcel(section)) {
runBlocking {
saveSensorData(section)
}
}
} catch (e: Exception) {
Toast.makeText(application, "Wrong section number", Toast.LENGTH_SHORT).show()
}

binding.scanButton.isEnabled = true
binding.sendButton.isEnabled = false
binding.scanButton2.isEnabled = true
binding.localizationButton.isEnabled = true
}
.setNegativeButton("Cancel") { _, _ -> }

builder.create().show()
}

binding.scanButton2.setOnClickListener {
val builder = AlertDialog.Builder(this)
val dialogView = layoutInflater.inflate(R.layout.dialog_ssid, null)
dialogView.findViewById<EditText>(R.id.ssidEditText1).setText(pref.getString("ssid1", ""))
dialogView.findViewById<EditText>(R.id.ssidEditText2).setText(pref.getString("ssid2", ""))
dialogView.findViewById<EditText>(R.id.ssidEditText3).setText(pref.getString("ssid3", ""))

builder.setView(dialogView)
.setPositiveButton("OK") { _, _ ->
val ssid1 = dialogView.findViewById<EditText>(R.id.ssidEditText1).text.toString()
val ssid2 = dialogView.findViewById<EditText>(R.id.ssidEditText2).text.toString()
val ssid3 = dialogView.findViewById<EditText>(R.id.ssidEditText3).text.toString()
editor.putString("ssid1", ssid1).apply()
editor.putString("ssid2", ssid2).apply()
editor.putString("ssid3", ssid3).apply()
ssidList.clear()
ssidList.add(ssid1)
ssidList.add(ssid2)
ssidList.add(ssid3)
rssiRecord.clear()
rssiRecord[ssid1] = mutableListOf()
rssiRecord[ssid2] = mutableListOf()
rssiRecord[ssid3] = mutableListOf()
size = 10

binding.scanButton.isEnabled = false
binding.sendButton.isEnabled = false
binding.scanButton2.isEnabled = false
binding.localizationButton.isEnabled = false
doScan()
registerReceiver(wifiReceiver, IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
if (saveExcel()) {
runBlocking {
saveSensorData()
}
.setNegativeButton("Cancel") { _, _ -> }
}

builder.create().show()
binding.scanButton.isEnabled = true
binding.sendButton.isEnabled = false
binding.localizationButton.isEnabled = true
}

binding.localizationButton.setOnClickListener {
runBlocking {
doLocalization()
binding.scanButton.isEnabled = true
binding.sendButton.isEnabled = false
binding.scanButton2.isEnabled = true
binding.localizationButton.isEnabled = false
}
}
Expand Down Expand Up @@ -261,7 +203,7 @@ class WifiActivity : AppCompatActivity() {
val rssi = it.find { it.SSID == ssid }?.level
if (rssi != null) {
append("RSSI: $rssi\n\n")
if ((rssiRecord[ssid]?.size ?: 0) < size) {
if ((rssiRecord[ssid]?.size ?: 0) < 10) {
rssiRecord[ssid]?.add(rssi)
}
} else {
Expand All @@ -275,8 +217,9 @@ class WifiActivity : AppCompatActivity() {
}
}

private fun saveExcel(section: Int): Boolean {
private fun saveExcel(): Boolean {
if (!isExternalStorageWritable()) {
Toast.makeText(applicationContext, "Please allow app permissions.", Toast.LENGTH_SHORT).show()
return false
}

Expand All @@ -289,7 +232,7 @@ class WifiActivity : AppCompatActivity() {
row.createCell(2).setCellValue("Y")
row.createCell(3).setCellValue("Z")

for (i in 0 until size) {
for (i in 0 until 10) {
row = this.createRow(i + 1)
row.createCell(0).setCellValue(0.0)
for (j in ssidList.indices) {
Expand All @@ -303,7 +246,7 @@ class WifiActivity : AppCompatActivity() {
if (!File(dir).exists()) {
File(dir).mkdirs()
}
val excelFile = File(dir, "${section}.xls")
val excelFile = File(dir, "rssi_data.xls")

return try {
workbook.write(FileOutputStream(excelFile))
Expand All @@ -316,10 +259,10 @@ class WifiActivity : AppCompatActivity() {
}
}

private suspend fun saveSensorData(section: Int) {
private suspend fun saveSensorData() {
val file = File(
Environment.getExternalStorageDirectory().absolutePath +
"/Indoor Positioning System/${section}.xls"
"/Indoor Positioning System/rssi_data.xls"
)

// 1st parameter: MediaType 으로 보내는 파일의 타입을 정하는 것
Expand Down Expand Up @@ -362,7 +305,10 @@ class WifiActivity : AppCompatActivity() {
if (response.isSuccessful) {
val result = response.body()
Handler(Looper.getMainLooper()).post {
binding.resultTextView.text = "Section: $result"
binding.resultTextView.text = buildString {
append("Section: ")
append(result)
}
Toast.makeText(applicationContext, "Section: $result", Toast.LENGTH_LONG).show()
}
} else {
Expand Down
12 changes: 1 addition & 11 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,10 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Start"
app:layout_constraintBottom_toTopOf="@+id/testButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleView"
app:layout_constraintVertical_bias="0.5" />

<Button
android:id="@+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/startButton"
app:layout_constraintVertical_bias="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
24 changes: 6 additions & 18 deletions app/src/main/res/layout/activity_wifi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"
app:layout_constraintBottom_toTopOf="@+id/scrollView2"
app:layout_constraintBottom_toTopOf="@+id/scrollView"
app:layout_constraintEnd_toStartOf="@+id/sendButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -23,22 +23,10 @@
android:layout_height="wrap_content"
android:enabled="false"
android:text="Send"
app:layout_constraintBottom_toTopOf="@+id/scrollView2"
app:layout_constraintEnd_toStartOf="@+id/scanButton2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/scanButton"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/scanButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan2"
app:layout_constraintBottom_toTopOf="@+id/scrollView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/scrollView"
app:layout_constraintEnd_toStartOf="@+id/localizationButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/sendButton"
app:layout_constraintStart_toEndOf="@+id/scanButton"
app:layout_constraintTop_toTopOf="parent" />

<Button
Expand All @@ -47,14 +35,14 @@
android:layout_height="wrap_content"
android:enabled="false"
android:text="Localization"
app:layout_constraintBottom_toTopOf="@id/scrollView2"
app:layout_constraintBottom_toTopOf="@id/scrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/scanButton2"
app:layout_constraintStart_toEndOf="@+id/sendButton"
app:layout_constraintTop_toTopOf="parent" />

<ScrollView
android:id="@+id/scrollView2"
android:id="@+id/scrollView"
android:layout_width="409dp"
android:layout_height="673dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 41a03a8

Please sign in to comment.