Skip to content

Commit

Permalink
Bluetooth device connections (#147)
Browse files Browse the repository at this point in the history
# *Bluetooth device connections*

## ♻️ Current situation & Problem
- Fixes last connected (seen) timestamp to be formatted to current time
zone
- Fixes double paring request. I was able to reproduce the issue and
from different stack overflow posts it seems to be an android bug. This
[suggestion](PhilipsHue/flutter_reactive_ble#507 (comment))
to create the bond request manually before connecting seems to work.

## ⚙️ Release Notes 
*Add a bullet point list summary of the feature and possible migration
guides if this is a breaking change so this section can be added to the
release notes.*
*Include code snippets that provide examples of the feature implemented
or links to the documentation if it appends or changes the public
interface.*


## 📚 Documentation
*Please ensure that you properly document any additions in conformance
to [Spezi Documentation
Guide](https://github.com/StanfordSpezi/.github/blob/main/DOCUMENTATIONGUIDE.md).*
*You can use this section to describe your solution, but we encourage
contributors to document your reasoning and changes using in-line
documentation.*


## ✅ Testing
*Please ensure that the PR meets the testing requirements set by CodeCov
and that new functionality is appropriately tested.*
*This section describes important information about the tests and why
some elements might not be testable.*


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [ ] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).

---------

Co-authored-by: Paul Schmiedmayer <[email protected]>
  • Loading branch information
eldcn and PSchmiedmayer authored Dec 8, 2024
1 parent d101a43 commit a5e72cc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class BluetoothUiStateMapper @Inject constructor(
)
}

private val systemDefaultDateFormatter by lazy {
dateFormatter.withZone(ZoneId.systemDefault())
}

fun mapBleServiceState(state: EngageBLEServiceState): BluetoothUiState {
return when (state) {
EngageBLEServiceState.Idle -> {
Expand Down Expand Up @@ -81,7 +85,7 @@ class BluetoothUiStateMapper @Inject constructor(
name = it.device.name,
summary = summary,
connected = it.device.connected,
lastSeen = StringResource(R.string.last_seen_on, dateFormatter.format(time))
lastSeen = StringResource(R.string.last_seen_on, systemDefaultDateFormatter.format(time))
)
}
val header = if (devices.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal class BLEDeviceConnector @AssistedInject constructor(
fun connect() {
val currentGatt = bluetoothGatt
if (currentGatt != null || isDestroyed.get()) return
device.createBond()
bluetoothGatt = device.connectGatt(context, false, gattCallback)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BLEDeviceConnectorTest {

@Before
fun setup() {
every { device.createBond() } returns true
every { device.connectGatt(context, false, any()) } returns bluetoothGatt
every { bluetoothGatt.disconnect() } just Runs
}
Expand All @@ -59,6 +60,7 @@ class BLEDeviceConnectorTest {
repeat(10) { sut.connect() }

// then
verify(exactly = 1) { device.createBond() }
verify(exactly = 1) { device.connectGatt(context, false, any()) }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package edu.stanford.spezi.core.utils

import java.time.Instant
import javax.inject.Inject

class TimeProvider @Inject constructor() {
fun currentTimeMillis(): Long = System.currentTimeMillis()
fun currentTimeMillis(): Long = Instant.now().toEpochMilli()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package edu.stanford.spezi.core.utils

import com.google.common.truth.Truth.assertThat
import org.junit.Test
import java.time.Instant

class TimeProviderTest {
private val provider = TimeProvider()

@Test
fun `it should indicate system currentTimeMillis`() {
fun `it should indicate now instant epoch millis`() {
// given
val current = System.currentTimeMillis()
val current = Instant.now().toEpochMilli()
val threshold = 1000L

// when
Expand Down

0 comments on commit a5e72cc

Please sign in to comment.