-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from d4rken-org/airpods-gen4
Add support for AirPods (Gen 4 - no ANC)
- Loading branch information
Showing
4 changed files
with
142 additions
and
3 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/airpods/AirPodsGen4.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.airpods | ||
|
||
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.DualApplePods | ||
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 AirPodsGen4( | ||
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 reliability: Float = PodDevice.BASE_CONFIDENCE, | ||
private val rssiAverage: Int? = null, | ||
private val cachedBatteryPercentage: Float? = null, | ||
private val cachedCaseState: DualApplePods.LidState? = null | ||
) : DualApplePods, HasStateDetectionAirPods { | ||
|
||
override val model: PodDevice.Model = PodDevice.Model.AIRPODS_GEN4 | ||
|
||
override val batteryCasePercent: Float? | ||
get() = super.batteryCasePercent ?: cachedBatteryPercentage | ||
|
||
override val caseLidState: DualApplePods.LidState | ||
get() = cachedCaseState ?: super.caseLidState | ||
|
||
override val rssi: Int | ||
get() = rssiAverage ?: super<DualApplePods>.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 = AirPodsGen4(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, | ||
reliability = result.reliability, | ||
cachedBatteryPercentage = result.getLatestCaseBattery(), | ||
rssiAverage = result.rssiSmoothed(basic.rssi), | ||
cachedCaseState = result.getLatestCaseLidState(basic) | ||
) | ||
} | ||
|
||
} | ||
|
||
companion object { | ||
private val DEVICE_CODE = 0x1920.toUShort() | ||
private val TAG = logTag("PodDevice", "Apple", "AirPods", "Gen4") | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsGen4Test.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,46 @@ | ||
package eu.darken.capod.pods.core.apple.airpods | ||
|
||
import eu.darken.capod.pods.core.PodDevice | ||
import eu.darken.capod.pods.core.apple.BaseAirPodsTest | ||
import eu.darken.capod.pods.core.apple.DualApplePods | ||
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 AirPodsGen4Test : BaseAirPodsTest() { | ||
|
||
@Test | ||
fun `AirPods Gen4 via log from #225`() = runTest { | ||
create<AirPodsGen4>("07 19 01 19 20 2B 33 8F 11 00 04 59 D4 57 20 0F 1C 13 38 B2 00 74 E9 DD 70 D7 A5") { | ||
rawPrefix shouldBe 0x01.toUByte() | ||
rawDeviceModel shouldBe 0x1920.toUShort() | ||
rawStatus shouldBe 0x2B.toUByte() | ||
rawPodsBattery shouldBe 0x33.toUByte() | ||
rawFlags shouldBe 0x8.toUShort() | ||
rawCaseBattery shouldBe 0xF.toUShort() | ||
rawCaseLidState shouldBe 0x11.toUByte() | ||
rawDeviceColor shouldBe 0x00.toUByte() | ||
rawSuffix shouldBe 0x04.toUByte() | ||
|
||
batteryLeftPodPercent shouldBe 0.3f | ||
batteryRightPodPercent shouldBe 0.3f | ||
|
||
isCaseCharging shouldBe false | ||
isLeftPodCharging shouldBe false | ||
isRightPodCharging shouldBe false | ||
|
||
isLeftPodInEar shouldBe true | ||
isRightPodInEar shouldBe true | ||
batteryCasePercent shouldBe null | ||
|
||
caseLidState shouldBe DualApplePods.LidState.UNKNOWN | ||
|
||
state shouldBe HasStateDetectionAirPods.ConnectionState.IDLE | ||
|
||
podStyle.identifier shouldBe HasAppleColor.DeviceColor.WHITE.name | ||
|
||
model shouldBe PodDevice.Model.AIRPODS_GEN4 | ||
} | ||
} | ||
} |