Changing Bluetooth discoverability in Java ME
m (Mtilli - - →Supplementary material) |
m (Mtilli - - →Supplementary material) |
||
| Line 135: | Line 135: | ||
softkey. The main form will be brought to the foreground. If the discoverability mode | softkey. The main form will be brought to the foreground. If the discoverability mode | ||
change procedure is successful, a 'Discoverability mode changed' message will appear. | change procedure is successful, a 'Discoverability mode changed' message will appear. | ||
| − | |||
| − | |||
Revision as of 14:14, 10 August 2012
Article Metadata
Code Example
Tested with
Compatibility
Article
Overview
The following code snippet demonstrates how to change device discoverability using the Bluetooth API.
Discoverability mode can be set using the LocalDevice.setDiscoverable method. This method takes discoverability id as a parameter. This parameter must be one of the following values:
DiscoveryAgent.GIAC;
DiscoveryAgent.LIAC;
DiscoveryAgent.NOT_DISCOVERABLE;
More information on discoverability modes can be found in the JSR-82 specification.
Source file: BTDiscoverabilityMode.java
/**
* Allows to select discover mode from Bluetooth device.
*/
private List listModes;
/**
* Discoverability mode id array.
*/
private int[] modeArray;
/**
* Instantiates a listModes.
*/
private void setupModeList() {
listModes = new List( "Mode list", Choice.IMPLICIT );
listModes.setSelectCommand( CHANGE_COMMAND );
listModes.addCommand( BACK_COMMAND );
listModes.setCommandListener( this );
// adding discoverability modes to the list
listModes.append( "GIAC", null );
listModes.append( "LIAC", null );
listModes.append( "NOT DISCOVERABLE", null );
//setting mode id array values
modeArray[0] = DiscoveryAgent.GIAC;
modeArray[1] = DiscoveryAgent.LIAC;
modeArray[2] = DiscoveryAgent.NOT_DISCOVERABLE;
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
* @param command the command that was invoked
* @param displayable the displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {
if (command == EXIT_COMMAND) {
// Exit the MIDlet
exit();
} else if (command == EXECUTE_COMMAND) {
// Open discoverability mode list
display.setCurrent( listModes );
} else if ( command == CHANGE_COMMAND ) {
//get currently selected contact
int i = listModes.getSelectedIndex();
String item = listModes.getString( i );
printString( "Selected mode: " + item );
// Bringing back main form to the foreground
display.setCurrent( mainForm );
// Changing discoverability mode
changeMode( i );
} else if ( command == BACK_COMMAND ) {
display.setCurrent( mainForm );
}
}
/**
*
* @param modeIndex
*/
private void changeMode( int modeIndex ) {
LocalDevice devLocal = null;
try {
printString( "Getting local device ..." );
devLocal = LocalDevice.getLocalDevice();
} catch ( BluetoothStateException e ) {
printString( e.toString() );
return;
}
try {
printString( "Changing discoverability mode" );
devLocal.setDiscoverable( modeArray[ modeIndex ] );
} catch ( BluetoothStateException e ) {
printString( e.toString() );
return;
}
printString( "Discoverability mode changed." );
}
Postconditions
When the MIDlet is started, the main form with a text field will be displayed. To change the current bluetooth device mode, you should press the 'Modes' softkey, which displays a list of available modes.
When the list appears, select a specific mode and press the 'Change' softkey. The main form will be brought to the foreground. If the discoverability mode change procedure is successful, a 'Discoverability mode changed' message will appear.

