How to determine whether Bluetooth is ON in a MIDlet
I want to know the following things:
1. Whether it is possible for a MIDlet or Java ME code to determine whether the device it is running on has bluetooth turned ON or not.
2. Whether it is possible for a MIDlet or Java ME code to determine whether the device it is running on has bluetooth capability or not.
3. Whether it is possible for a MIDlet or Java ME code to turn ON bluetooth automatically if the device has bluetooth capability.
Any help would be highly appreciated.
[508 byte] By [
Nortona] at [2007-11-27 9:37:56]

# 1
Hi,
I recognize your problems and as far as I know there is no good way of dealing with these issues which is quite problematic and irritating for a user of a Midlet using bluetooth. Though we have found one way to get around issue 1 by analysing the exception thrown when attempting to make a bluetooth connection. Though this has only been tested on SonyEricsson phones and more specifically K800. Below you will find the code I wrote to do this. I call this early in the app and throw up an Alert asking the user to quit and turn on the bluetooth, also describing how to do it. Not a very nice solution but the best I have found so far.
public boolean isBluetoothActive() {
// this might be a bit tricky...
// there is no way to check if the user has turned on bluetooth in the
// phone or not but experience has shown one get a
// BluetoothConnectionException.FAILED_NOINFO when its turned off.
try {
StreamConnection connection = (StreamConnection)Connector.open("btspp://000000000000:1");
// successfully connected
connection.close();
return true;
} catch(BluetoothConnectionException bce) {
switch(bce.getStatus()) {
case BluetoothConnectionException.FAILED_NOINFO:
return false;
case BluetoothConnectionException.NO_RESOURCES:
Info.message(this,"BT Active test NO_RESOURCES",DL_5);
break;
case BluetoothConnectionException.SECURITY_BLOCK:
Info.message(this,"BT Active test SECURITY_BLOCK",DL_5);
break;
case BluetoothConnectionException.TIMEOUT:
Info.message(this,"BT Active test TIMEOUT",DL_5);
break;
case BluetoothConnectionException.UNACCEPTABLE_PARAMS:
Info.message(this,"BT Active test UNACCEPTABLE_PARAMS",DL_5);
break;
case BluetoothConnectionException.UNKNOWN_PSM:
Info.message(this,"BT Active test UNKNOWN_PSM",DL_5);
break;
default:
// if not caught above
}
} catch(IOException ioe) {
Info.message(this,"BT Active test IOExcpetion: "+ioe,DL_5);
}
return true;
}
/Kalle