-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add preliminary support for Beats Fit Pro (#75)
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
app-common/src/main/java/eu/darken/capod/pods/core/apple/beats/BeatsFitPro.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,70 @@ | ||
package eu.darken.capod.pods.core.apple.beats | ||
|
||
import eu.darken.capod.common.bluetooth.BleScanResult | ||
import eu.darken.capod.common.debug.logging.logTag | ||
import eu.darken.capod.pods.core.PodDevice | ||
import eu.darken.capod.pods.core.apple.ApplePods | ||
import eu.darken.capod.pods.core.apple.DualAirPods | ||
import eu.darken.capod.pods.core.apple.DualApplePodsFactory | ||
import eu.darken.capod.pods.core.apple.protocol.ProximityPairing | ||
import java.time.Instant | ||
import javax.inject.Inject | ||
|
||
data class BeatsFitPro( | ||
override val identifier: PodDevice.Id = PodDevice.Id(), | ||
override val seenLastAt: Instant = Instant.now(), | ||
override val seenFirstAt: Instant = Instant.now(), | ||
override val seenCounter: Int = 1, | ||
override val scanResult: BleScanResult, | ||
override val proximityMessage: ProximityPairing.Message, | ||
override val confidence: Float = PodDevice.BASE_CONFIDENCE, | ||
private val rssiAverage: Int? = null, | ||
private val cachedBatteryPercentage: Float? = null, | ||
private val cachedCaseState: DualAirPods.LidState? = null | ||
) : DualAirPods { | ||
|
||
override val model: PodDevice.Model = PodDevice.Model.BEATS_FIT_PRO | ||
|
||
override val batteryCasePercent: Float? | ||
get() = super.batteryCasePercent ?: cachedBatteryPercentage | ||
|
||
override val caseLidState: DualAirPods.LidState | ||
get() = cachedCaseState ?: super.caseLidState | ||
|
||
override val rssi: Int | ||
get() = rssiAverage ?: super.rssi | ||
|
||
class Factory @Inject constructor() : DualApplePodsFactory(TAG) { | ||
|
||
override fun isResponsible(message: ProximityPairing.Message): Boolean = message.run { | ||
getModelInfo().full == DEVICE_CODE && length == ProximityPairing.PAIRING_MESSAGE_LENGTH | ||
} | ||
|
||
override fun create(scanResult: BleScanResult, message: ProximityPairing.Message): ApplePods { | ||
var basic = BeatsFitPro(scanResult = scanResult, proximityMessage = message) | ||
val result = searchHistory(basic) | ||
|
||
if (result != null) basic = basic.copy(identifier = result.id) | ||
updateHistory(basic) | ||
|
||
if (result == null) return basic | ||
|
||
return basic.copy( | ||
identifier = result.id, | ||
seenFirstAt = result.seenFirstAt, | ||
seenLastAt = scanResult.receivedAt, | ||
seenCounter = result.seenCounter, | ||
confidence = result.confidence, | ||
cachedBatteryPercentage = result.getLatestCaseBattery(), | ||
rssiAverage = result.averageRssi(basic.rssi), | ||
cachedCaseState = result.getLatestCaseLidState(basic) | ||
) | ||
} | ||
|
||
} | ||
|
||
companion object { | ||
private val DEVICE_CODE = 0x1220.toUShort() | ||
private val TAG = logTag("PodDevice", "Beats", "Fit", "Pro") | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app-common/src/test/java/eu/darken/capod/pods/core/apple/beats/BeatsFitProTest.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 eu.darken.capod.pods.core.apple.beats | ||
|
||
import eu.darken.capod.pods.core.PodDevice | ||
import eu.darken.capod.pods.core.apple.BaseAirPodsTest | ||
import eu.darken.capod.pods.core.apple.HasAppleColor | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
|
||
class BeatsFitProTest : BaseAirPodsTest() { | ||
|
||
/** | ||
* From https://github.com/d4rken-org/capod/issues/33#issuecomment-1256235651 | ||
*/ | ||
@Test | ||
fun `test basics`() = runTest { | ||
create<BeatsFitPro>("07 19 01 12 20 20 FA 8F 01 11 24 9B 9B 23 52 60 5A 8C 32 1C A5 C2 81 51 82 AF C8") { | ||
rawPrefix shouldBe 0x01.toUByte() | ||
rawDeviceModel shouldBe 0x1220.toUShort() | ||
rawStatus shouldBe 0x20.toUByte() | ||
rawPodsBattery shouldBe 0xFA.toUByte() | ||
rawFlags shouldBe 0x8.toUShort() | ||
rawCaseBattery shouldBe 0xF.toUShort() | ||
rawCaseLidState shouldBe 0x01.toUByte() | ||
rawDeviceColor shouldBe 0x11.toUByte() | ||
rawSuffix shouldBe 0x24.toUByte() | ||
|
||
isLeftPodMicrophone shouldBe true | ||
isRightPodMicrophone shouldBe false | ||
|
||
batteryLeftPodPercent shouldBe 1.0f | ||
batteryRightPodPercent shouldBe null | ||
|
||
isCaseCharging shouldBe false | ||
isRightPodCharging shouldBe false | ||
isLeftPodCharging shouldBe false | ||
batteryCasePercent shouldBe null | ||
podStyle.identifier shouldBe HasAppleColor.DeviceColor.UNKNOWN.name | ||
|
||
model shouldBe PodDevice.Model.BEATS_FIT_PRO | ||
} | ||
} | ||
} |