Skip to content
Snippets Groups Projects
Commit 14cd91a1 authored by Quoc Gia Cat Tran's avatar Quoc Gia Cat Tran
Browse files

Merge remote-tracking branch 'origin/master'

parents ea5b2d6c a4ee6278
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,12 @@ import kotlinx.android.synthetic.main.activity_main.*
import mumayank.com.airlocationlibrary.AirLocation
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.core.app.ComponentActivity
import androidx.core.app.ComponentActivity.ExtraData
import androidx.core.content.ContextCompat.getSystemService
import android.icu.lang.UCharacter.GraphemeClusterBreak.T
class MainActivity : AppCompatActivity() {
......@@ -55,13 +61,15 @@ class MainActivity : AppCompatActivity() {
currentLocation = Location(location.latitude, location.longitude)
var wifiString = ""
var wifiLocations: MutableList<WifiLocation> = ArrayList()
var bestLevel = -100
var bestDistance = Double.MAX_VALUE
wifiManager?.scanResults?.forEach {
val level = WifiManager.calculateSignalLevel(it.level, 5)
wifiString += "\n" + it.BSSID + ": " + it.SSID + ": " + level
wifiLocations.add(WifiLocation(it.timestamp, it.BSSID, it.SSID, it.level, location.latitude, location.longitude))
if (bestLevel < it.level) {
bestLevel = it.level
//val level = WifiManager.calculateSignalLevel(it.level, 5)
//https://stackoverflow.com/a/18359639 - constant used returns meters
val distance = Math.pow(10.0, (27.55 - 20 * Math.log10(it.frequency.toDouble()) + Math.abs(it.level)) / 20.0)
wifiString += "\n" + it.BSSID + ": " + it.SSID + ": " + distance
wifiLocations.add(WifiLocation(it.timestamp, it.BSSID, it.SSID, it.level, it.frequency, distance, location.latitude, location.longitude))
if (bestDistance > distance) {
bestDistance = distance
closestBSSID = it.BSSID
}
}
......
......@@ -11,6 +11,8 @@ data class WifiLocation(
@ColumnInfo(name = "bssid") @NonNull val bssid: String,
@ColumnInfo(name = "ssid") @NonNull val ssid: String,
@ColumnInfo(name = "level") @NonNull val level: Int,
@ColumnInfo(name = "frequency") @NonNull val frequency: Int,
@ColumnInfo(name = "distance") @NonNull val distance: Double,
@ColumnInfo(name = "latitude") @NonNull val latitude: Double,
@ColumnInfo(name = "longitude") @NonNull val longitude: Double
)
......
......@@ -21,8 +21,7 @@ interface WifiLocationDao {
@Query("SELECT * FROM wifilocation WHERE bssid IN (:bssids)")
suspend fun findLocationsBySSID(bssids: List<String>): List<WifiLocation>
//TODO: improve algorithm
@Query("SELECT latitude, longitude FROM wifilocation WHERE bssid = :bssid ORDER BY level DESC, date DESC LIMIT 1")
@Query("SELECT latitude, longitude FROM wifilocation WHERE bssid = :bssid ORDER BY distance DESC, date DESC LIMIT 1")
suspend fun findBestLocationBySSID(bssid: String): Location
@Insert
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment