Skip to content

Commit

Permalink
Fix AirPods Max wear detection. (#108)
Browse files Browse the repository at this point in the history
Closes #106
  • Loading branch information
d4rken authored Mar 10, 2023
1 parent db80fa1 commit 94e2d40
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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.common.isBitSet
import eu.darken.capod.pods.core.HasChargeDetection
import eu.darken.capod.pods.core.HasEarDetection
import eu.darken.capod.pods.core.PodDevice
import eu.darken.capod.pods.core.apple.ApplePods
Expand All @@ -22,21 +23,18 @@ data class AirPodsMax(
override val proximityMessage: ProximityPairing.Message,
override val reliability: Float = PodDevice.BASE_CONFIDENCE,
private val rssiAverage: Int? = null,
) : SingleApplePods, HasEarDetection, HasAppleColor {
) : SingleApplePods, HasEarDetection, HasChargeDetection, HasAppleColor {

override val model: PodDevice.Model = PodDevice.Model.AIRPODS_MAX

override val rssi: Int
get() = rssiAverage ?: super<SingleApplePods>.rssi

val isHeadphonesBeingWorn: Boolean
get() = rawStatus.isBitSet(1)

val isHeadsetBeingCharged: Boolean
override val isHeadsetBeingCharged: Boolean
get() = rawFlags.isBitSet(0)

override val isBeingWorn: Boolean
get() = isHeadphonesBeingWorn
get() = !rawStatus.isBitSet(5)

class Factory @Inject constructor() : SingleApplePodsFactory(TAG) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SingleApplePodsTest : BaseAirPodsTest() {

isHeadsetBeingCharged shouldBe false

isHeadphonesBeingWorn shouldBe true
isBeingWorn shouldBe true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.darken.capod.pods.core.apple.airpods

import eu.darken.capod.common.isBitSet
import eu.darken.capod.pods.core.PodDevice
import eu.darken.capod.pods.core.apple.BaseAirPodsTest
import eu.darken.capod.pods.core.apple.HasAppleColor
Expand Down Expand Up @@ -27,8 +28,6 @@ class AirPodsMaxTest : BaseAirPodsTest() {

isHeadsetBeingCharged shouldBe false

isHeadphonesBeingWorn shouldBe true

model shouldBe PodDevice.Model.AIRPODS_MAX
}
}
Expand All @@ -50,8 +49,6 @@ class AirPodsMaxTest : BaseAirPodsTest() {
batteryHeadsetPercent shouldBe 0.5f

isHeadsetBeingCharged shouldBe false

isHeadphonesBeingWorn shouldBe true
}
}

Expand All @@ -72,9 +69,25 @@ class AirPodsMaxTest : BaseAirPodsTest() {

isHeadsetBeingCharged shouldBe false

isHeadphonesBeingWorn shouldBe true
rawStatus.isBitSet(5) shouldBe true

podStyle shouldBe HasAppleColor.DeviceColor.BLUE
}
}

@Test
fun `wear status`() = runTest {
create<AirPodsMax>("07 19 01 0A 20 03 07 80 03 03 65 1F 28 32 D0 D9 71 43 00 9A 40 E7 6B EA 6C 2C FB") {
rawStatus shouldBe 0x03.toUByte()

rawStatus.isBitSet(5) shouldBe false
isBeingWorn shouldBe true
}
create<AirPodsMax>("07 19 01 0A 20 23 07 80 03 03 65 1F 28 32 D0 D9 71 43 00 9A 40 E7 6B EA 6C 2C FB") {
rawStatus shouldBe 0x23.toUByte()

rawStatus.isBitSet(5) shouldBe true
isBeingWorn shouldBe false
}
}
}

0 comments on commit 94e2d40

Please sign in to comment.