سؤال

I'm a newbie in both Java and Android and I'm trying to figure out how to do the following action. Btw, I find that the official Google Android documentation is NOT for newbies and would like any referral to something a bit more... non-native programmer friendly.

Ok, here goes:

I would like to turn on/off Bluetooth automatically when I plug in/unplug the phone - pc or ac.

So, I have found the following components:

BatteryManager: Seems like using the ACTION_BATTERY_CHANGED intent is the way to go, and setting up a broadcast receiver for when this changes. Is it correct?

I've also found BluetoothAdapter and within that, there's the enable() method. Which says: "do not use without explicit user action to turn on Bluetooth." - is plugging in the phone a explicit user action? Is this the right thing for me to use? The same goes for disable(), of course.

Also, the BroadcastReceivers I've seen examples for in the Internet(s) only show me how to launch an intent - usually to open a new activity. I assume I can have the Broadcast Receiver launch a method in which I will turn bluetooth on and off? Can an intent be a method, and not just activity?

Any help would be appreciated!

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top