-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collect more data from Bluetooth beacons (beacon type, identifiers)
- Loading branch information
Showing
10 changed files
with
621 additions
and
64 deletions.
There are no files selected for viewing
491 changes: 491 additions & 0 deletions
491
app/schemas/xyz.malkki.neostumbler.db.ReportDatabase/6.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/src/main/java/xyz/malkki/neostumbler/domain/BluetoothBeacon.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package xyz.malkki.neostumbler.domain | ||
|
||
import android.os.SystemClock | ||
import org.altbeacon.beacon.Beacon | ||
|
||
data class BluetoothBeacon( | ||
val macAddress: String, | ||
val beaconType: Int?, | ||
val id1: String?, | ||
val id2: String?, | ||
val id3: String?, | ||
val signalStrength: Int, | ||
val timestamp: Long | ||
) { | ||
companion object { | ||
fun fromBeacon(beacon: Beacon): BluetoothBeacon { | ||
val timestamp = SystemClock.elapsedRealtime() - (System.currentTimeMillis() - beacon.lastCycleDetectionTimestamp) | ||
|
||
return BluetoothBeacon( | ||
macAddress = beacon.bluetoothAddress, | ||
beaconType = beacon.beaconTypeCode, | ||
id1 = beacon.id1?.toString(), | ||
id2 = beacon.id2?.toString(), | ||
id3 = beacon.id3?.toString(), | ||
signalStrength = beacon.rssi, | ||
timestamp = timestamp | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...src/main/java/xyz/malkki/neostumbler/scanner/source/BeaconLibraryBluetoothBeaconSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package xyz.malkki.neostumbler.scanner.source | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.map | ||
import org.altbeacon.beacon.Beacon | ||
import org.altbeacon.beacon.BeaconManager | ||
import org.altbeacon.beacon.Region | ||
import xyz.malkki.neostumbler.domain.BluetoothBeacon | ||
import kotlin.random.Random | ||
|
||
class BeaconLibraryBluetoothBeaconSource(context: Context) : BluetoothBeaconSource { | ||
private val appContext = context.applicationContext | ||
|
||
private fun getBeaconFlow(context: Context): Flow<List<Beacon>> = callbackFlow { | ||
val beaconManager = BeaconManager.getInstanceForApplication(context) | ||
|
||
val rangeNotifier: (Collection<Beacon>, Region) -> Unit = { beacons: Collection<Beacon>, _: Region -> | ||
trySendBlocking(beacons.toList()) | ||
} | ||
|
||
beaconManager.addRangeNotifier(rangeNotifier) | ||
|
||
val region = Region("all_beacons_${Random.Default.nextInt(0, Int.MAX_VALUE)}", null, null, null) | ||
|
||
beaconManager.startRangingBeacons(region) | ||
|
||
awaitClose { | ||
beaconManager.stopRangingBeacons(region) | ||
|
||
beaconManager.removeRangeNotifier(rangeNotifier) | ||
} | ||
} | ||
|
||
override fun getBluetoothBeaconFlow(): Flow<List<BluetoothBeacon>> = getBeaconFlow(appContext).map { beacons -> | ||
beacons.map { beacon -> | ||
BluetoothBeacon.fromBeacon(beacon) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/xyz/malkki/neostumbler/scanner/source/BluetoothBeaconSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package xyz.malkki.neostumbler.scanner.source | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import xyz.malkki.neostumbler.domain.BluetoothBeacon | ||
|
||
interface BluetoothBeaconSource { | ||
fun getBluetoothBeaconFlow(): Flow<List<BluetoothBeacon>> | ||
} |
31 changes: 0 additions & 31 deletions
31
app/src/main/java/xyz/malkki/neostumbler/utils/beaconFlow.kt
This file was deleted.
Oops, something went wrong.