Ok... it's ugly. Indeed, this was one of the things I found difficult
to do in the same manner in Android 1.x and Android 2.x
I actually found it difficult to have one app exercise both these
interfaces, one of the several reasons I wish I had a separate app for
1.x vs 2.x -- if you've not yet made a final choice there, split them
up and you will be happier.
In Android 1.x, this will do it, cribbed and reduced from Android
source.
I had to bring over a considerable portion of android.bluetooth to get
this working.
You would call the static enable() function to do it:
import android.bluetooth.BluetoothDevice;
import android.content.Context;
public class LocalBluetoothManager {
private static LocalBluetoothManager sSingleton;
/** Used when obtaining a reference to the singleton instance. */
private static Object INSTANCE_LOCK = new Object();
private boolean mInitialized;
private BluetoothDevice mManager;
public static boolean enable(Context context, boolean b) {
synchronized (INSTANCE_LOCK) {
if (sSingleton == null) {
sSingleton = new LocalBluetoothManager();
}
if (!sSingleton.init(context)) {
return false;
}
return b ? sSingleton.mManager.enable() :
sSingleton.mManager.disable();
}
}
private boolean init(Context context) {
if (mInitialized) return true;
mInitialized = true;
mManager = (BluetoothDevice)
context.getSystemService("bluetooth"); //
Context.BLUETOOTH_SERVICE);
if (mManager == null) {
return false;
}
return true;
}
}
In Android 2.x, it is simpler:
import android.bluetooth.BluetoothAdapter;
BluetoothAdapter adapt = BluetoothAdapter.getDefaultAdapter();
adapt.enable(); // or adapt.disable();
You MAY need to have these permissions in your manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
Make sure you test on both Android 1.x and 2.x before going to Market.
tone
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en