-
-
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.
Merge pull request #228 from d4rken-org/airpods-gen4-ANC
Add support for AirPods (Gen 4 with ANC)
- Loading branch information
Showing
4 changed files
with
122 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/airpods/AirPodsGen4Anc.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 AirPodsGen4Anc( | ||
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_ANC | ||
|
||
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 = AirPodsGen4Anc(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 = 0x1B20.toUShort() | ||
private val TAG = logTag("PodDevice", "Apple", "AirPods", "Gen4-ANC") | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
app-common/src/test/java/eu/darken/capod/pods/core/apple/airpods/AirPodsGen4AncTest.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 AirPodsGen4AncTest : BaseAirPodsTest() { | ||
|
||
@Test | ||
fun `AirPods Gen4 with ANC via log from #226`() = runTest { | ||
create<AirPodsGen4Anc>("07 19 01 1B 20 0B 9A 8F 10 00 04 43 DF EC 1D D3 F1 C3 F4 A1 9B 29 26 B9 E7 3A A0") { | ||
rawPrefix shouldBe 0x01.toUByte() | ||
rawDeviceModel shouldBe 0x1B20.toUShort() | ||
rawStatus shouldBe 0x0b.toUByte() | ||
rawPodsBattery shouldBe 0x9A.toUByte() | ||
rawFlags shouldBe 0x8.toUShort() | ||
rawCaseBattery shouldBe 0xF.toUShort() | ||
rawCaseLidState shouldBe 0x10.toUByte() | ||
rawDeviceColor shouldBe 0x00.toUByte() | ||
rawSuffix shouldBe 0x04.toUByte() | ||
|
||
batteryLeftPodPercent shouldBe 0.9f | ||
batteryRightPodPercent shouldBe 1.0f | ||
|
||
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_ANC | ||
} | ||
} | ||
} |