adolfintel / OpenPods

The Free and Open Source app for monitoring your AirPods on Android
https://fdossena.com/?p=openPods/index.frag
GNU General Public License v3.0
931 stars 162 forks source link

Xposed Module #132

Open victor141516 opened 2 years ago

victor141516 commented 2 years ago

Hey I'm trying to do this by myself but I'm a newbie on Android development. The idea is to add a Xposed module to the app that enables the native battery indicator.

I made a simple module that sets the battery level to 50% (not very helpful) and I think you only need to feed it with the real value and that should be all.

The code of the module is as simple as:

public class MyModule implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        XposedHelpers.findAndHookMethod("android.bluetooth.BluetoothDevice", lpparam.classLoader, "getBatteryLevel", new XC_MethodHook() {
            @Override
            protected void beforeHookedMethod(XC_MethodHook.MethodHookParam param) throws Throwable {
                param.setResult(50);
            }
        });
    }
}

Some tweaks should be done, like checking the device is indeed a non-natively-compatible one.

I don't know if this idea is not new, I didn't see anything related in the issues.

An image of my "always 50% airpods pro" module: https://i.imgur.com/M4xQGht.png

adolfintel commented 2 years ago

If you could reuse code from this app to make a working xposed module, that would be absolutely awesome. With xposed you could probably get rid of the workaround for the randomized mac addresses, battery optimization, etc.

Keep me posted, I'm really interested.

victor141516 commented 2 years ago

I'm trying but I don't really understand what I'm doing. I suppose I should subscribe to PodsService and somehow retrieve the charging level. I also tried to copy the content of startAirPodsScanner to the Xposed method, but getSystemService and getApplicationContext are not found and I don't know what's happening here. Your help is greatly appreciated

adolfintel commented 2 years ago

I never programmed a Magisk module but I imagine there is some way to access BLE without going through the limited Android APIs like OpenPods does. If there is, all you have to do is listen for all beacons that have the right UUID, read the manufacturer specific data field 76, make sure the length is 27 bytes and do the decoding as explained here

victor141516 commented 2 years ago

I'll try to work on this when I have time, but I feel like an Android developer can do the thing in 5 minutes. It's just adding a few config lines and creating the class that's on the OP, replacing the 50 for the actual generating function.

authorisation commented 1 year ago

Any updates on this?