简体   繁体   中英

How I can monitor when ADB over WiFi has been toggled?

In my application, I'd like to monitor when ADB over WiFi has been enabled or disabled. There doesn't seem to a BroadcastIntent for this so I don't know how to monitor the status of this.

Would you recommend a way of doing this? The only method I've come across is to poll every few seconds and though the shell, check where adbd is running or not. This seems horribly, horrible inefficient and kludgey.

There is a property that determines the port that adb may be listening to :

adb shell getprop service.adb.tcp.port

But apperently there is no way of being notified of a property change inside the android system : http://rxwen.blogspot.fr/2010/01/android-property-system.html

You would still have to poll it to get notified of a change, but that maybe easier. Nevertheless, having this port set doesn't really mean adb is running over wifi. There are chances though.

It can be accomplished by add a ContentOberserver to monitor the settings. ADB runs on port 5555 by default and is generally set to -1 when it isn't enabled.

getApplicationContext().getContentResolver().registerContentObserver(
    Settings.Secure.getUriFor("adb_port"), false, 
        new ContentObserver() {

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Integer intPort = Settings.Secure.getInt(getContentResolver(), "adb_port", -1);
                if (intPort > -1)  {
                    System.out.println("Enabled");
                } else {
                    System.out.println("Disabled");
                }
            }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM