×
Namespaces

Variants
Actions
(Difference between revisions)

Changing Bluetooth discoverability in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
m
tapla (Talk | contribs)
Line 126: Line 126:
 
==Supplementary material==
 
==Supplementary material==
  
* You can test the changing of Bluetooth discoverability in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: [[Media:BtDiscoverabilityMode.zip]].
+
This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.
  
* You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: [[Media:BtDiscoverabilityMode.diff.zip]]
+
* The patched, executable application that can be used to test the features described in this snippet is available for download at [[Media:BtDiscoverabilityMode.zip]].
 +
* You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in [[Media:BtDiscoverabilityMode.diff.zip]].
 +
* For general information on applying the patch, see [[Using Diffs]].
 +
* For unpatched stub applications, see [[Example stub]].
  
 
[[Category:Java ME]][[Category:Code Examples]]
 
[[Category:Java ME]][[Category:Code Examples]]

Revision as of 15:26, 20 March 2009


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia N78, Nokia 5800 XM, Nokia 6131
CompatibilityArticle
Keywords: javax.bluetooth.BluetoothStateException, javax.bluetooth.DiscoveryAgent, javax.bluetooth.LocalDevice
Created: (31 Jan 2009)
Last edited: tapla (20 Mar 2009)


Overview

The following code snippet demonstrates how to change discoverability using Bluetooth API.

Discoverability mode can be set using LocalDevice.setDiscoverable method. This method takes discoverability Id as a parameter. This parameter must hold one of the folowing values:

DiscoveryAgent.GIAC;
DiscoveryAgent.LIAC;
DiscoveryAgent.NOT_DISCOVERABLE;

More information on discoverability modes can be found in 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 text field will be displayed. To change current bluetooth device mode you should press 'Modes' softkey, then a list of available modes will be displayed.

When the list appeared select a specific mode from the list and press 'Change' softkey. Main form will be brought to the foreground. If discoverability mode changing procedure succeeded, a Discoverability mode changed' mesage will appear.

Supplementary material

This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at Media:BtDiscoverabilityMode.zip.
  • You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in Media:BtDiscoverabilityMode.diff.zip.
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example stub.
171 page views in the last 30 days.