I have been searching for weeks for a way to turn on and off the
bluetooth for any Android version from 1.5 onward.  I know it must be
possible, since there are widgets in the marketplace that do just
this, however I have never found any answers to my questions.  I do
know that I can make it work for Android 1.x using reflection.  I also
know it should be possible to use reflection on Android 2.x, but by
retrieving the methods for a different class since the official
bluetooth API was added in 2.0.  The code I've got so far is:

private static void BT_Enable(Context context, boolean enable){
                String V = android.os.Build.VERSION.RELEASE;
                int ver = Integer.valueOf(V.substring(0, 1));
                String eMethod = "enable";
                if (!enable) {
                        eMethod = "disable";
                }
                try{
                        Object manager = context.getSystemService("bluetooth");
                        Class<?> c = manager.getClass();
                        if (ver > 1){c =
Class.forName("android.bluetooth.BluetoothAdapter");}
                        Method method = c.getMethod(eMethod);
                        method.setAccessible(true);
                        method.invoke(manager);
                }
                catch (Exception e){
                        Toast.makeText(context, "Error: " + e, 1).show();
                }
        }

One of the issues that exists in 2.x is that the call
getSystemService("bluetooth") returns null, so to obtain the
appropriate class I've added the following:

                        if (ver > 1){c =
Class.forName("android.bluetooth.BluetoothAdapter");}


however, since there is no system Bluetooth service, there is no
receiver object to invoke to, causing a failure.  Any help is greatly
appreciated.

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to