From f0c8a4965a2ccd003f4139af2be7dc135e6610fe Mon Sep 17 00:00:00 2001 From: Kavish Devar Date: Tue, 15 Oct 2024 11:43:25 +0530 Subject: [PATCH] fix typo in rename packet in definitions.md --- AAP Definitions.md | 4 +- .../me/kavishdevar/aln/StartupReceiver.kt | 74 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 android/app/src/main/java/me/kavishdevar/aln/StartupReceiver.kt diff --git a/AAP Definitions.md b/AAP Definitions.md index 3f8fe9f..d2a7031 100644 --- a/AAP Definitions.md +++ b/AAP Definitions.md @@ -162,7 +162,7 @@ The airpods will respond with the same packet after the mode has been changed. We can send a packet to rename the AirPods. The packet format is as follows: ```plaintext -04 00 04 00 1A 01 [size] 00 [name] +04 00 04 00 1A 00 01 [size] 00 [name] ``` ## Toggle case charging sounds @@ -326,4 +326,4 @@ Example packet ```plaintext 04 00 04 00 17 00 00 00 10 00 11 00 08 7E 10 02 42 0B 08 4E 10 02 1A 05 01 00 00 00 00 -``` \ No newline at end of file +``` diff --git a/android/app/src/main/java/me/kavishdevar/aln/StartupReceiver.kt b/android/app/src/main/java/me/kavishdevar/aln/StartupReceiver.kt new file mode 100644 index 0000000..9cfe896 --- /dev/null +++ b/android/app/src/main/java/me/kavishdevar/aln/StartupReceiver.kt @@ -0,0 +1,74 @@ +package me.kavishdevar.aln + +import android.annotation.SuppressLint +import android.bluetooth.BluetoothAdapter +import android.bluetooth.BluetoothDevice +import android.bluetooth.BluetoothProfile +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.os.ParcelUuid + +class StartupReceiver : BroadcastReceiver() { + + companion object { + val PodsUUIDS: Set = setOf( + ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a"), + ParcelUuid.fromString("2a72e02b-7b99-778f-014d-ad0b7221ec74") + ) + + val btActions: Set = setOf( + BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED, + BluetoothDevice.ACTION_ACL_CONNECTED, + BluetoothDevice.ACTION_ACL_DISCONNECTED, + BluetoothDevice.ACTION_BOND_STATE_CHANGED, + BluetoothDevice.ACTION_NAME_CHANGED + ) + } + + override fun onReceive(context: Context?, intent: Intent?) { + if (intent == null || context == null) return + + intent.action?.let { action -> + if (btActions.contains(action)) { + try { + val state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, BluetoothAdapter.ERROR) + val device: BluetoothDevice? = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) + device?.let { + btProfileChanges(context, state, it) + } + } catch (e: NullPointerException) { + } + } + } + } + + @SuppressLint("MissingPermission") + private fun isPods(device: BluetoothDevice): Boolean { + device.uuids?.forEach { uuid -> + if (PodsUUIDS.contains(uuid)) { + return true + } + } + return false + } + + private fun startPodsService(context: Context, device: BluetoothDevice) { + if (!isPods(device)) return + val intent = Intent(context, AirPodsService::class.java).apply { + putExtra(BluetoothDevice.EXTRA_DEVICE, device) + } + context.startService(intent) + } + + private fun stopPodsService(context: Context) { + context.stopService(Intent(context, AirPodsService::class.java)) + } + + private fun btProfileChanges(context: Context, state: Int, device: BluetoothDevice) { + when (state) { + BluetoothProfile.STATE_CONNECTED -> startPodsService(context, device) + BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_DISCONNECTING -> stopPodsService(context) + } + } +} \ No newline at end of file