×
Namespaces

Variants
Actions
(Difference between revisions)

Changing Bluetooth discoverability in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
olkazmin (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(28 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Java ME]][[Category:Code Examples]][[Category:Connectivity]][[Category:Series 40]][[Category:Symbian]][[Category:Bluetooth]][[Category:Bluetooth API (JSR-82)]][[Category:Java Runtime 2.3 for Symbian]][[Category:Nokia Belle]][[Category:S60 3rd Edition (initial release)]][[Category:S60 5th Edition]][[Category:Series 40 Developer Platform 1.1]][[Category:Series 40 Developer Platform 2.0]]
__NOEDITSECTION__
+
{{ArticleMetaData <!-- v1.2 -->
{{KBCS}}
+
|sourcecode= [[Media:BTDiscoverabilityModeSource.zip]]
{{CodeSnippet
+
|installfile= [[Media:BTDiscoverabilityModeBinaries.zip]]
|id=
+
|devices= Nokia N78, Nokia 5800 XpressMusic, Nokia 6131, Nokia 701, Nokia Asha 305
|platform=S60 3rd Edition FP2, S60 5th Edition, S40 3rd Edition, FP 1
+
|sdk= [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 1.1 for Java],  [http://www.developer.nokia.com/Develop/Java/Tools/ Nokia SDK 2.0 for Java]
|devices=Nokia N78, Nokia 5800 XM, Nokia 6131
+
|platform= Series 40, Symbian
|category=Java ME
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|subcategory=Bluetooth
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|creationdate=January 31, 2009
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|keywords=javax.bluetooth.BluetoothStateException, javax.bluetooth.DiscoveryAgent,
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
javax.bluetooth.LocalDevice
+
|keywords= javax.bluetooth.BluetoothStateException, javax.bluetooth.DiscoveryAgent, javax.bluetooth.LocalDevice
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090219
 +
|author= [[User:Olkazmin]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001310
 
}}
 
}}
 
  
 
==Overview==
 
==Overview==
  
The folowing code snippet demonstrates how to change discoverability using
+
{{Abstract|This code example demonstrates how to change device discoverability using the Java ME Bluetooth API.}}
Bluetooth API.
+
  
Discoverability mode can be set using <tt>LocalDevice.setDiscoverable</tt>
+
Discoverability mode can be set using the {{Icode|LocalDevice.setDiscoverable}}
method. This method takes discoverability Id as a parameter. This parameter must
+
method. This method takes discoverability id as a parameter. This parameter must
hold one of the folowing values:
+
be one of the following values:
  
 
<code>
 
<code>
Line 29: Line 39:
 
</code>
 
</code>
  
More information on discoverability modes can be found in JSR-82 specification.
+
More information on discoverability modes can be found in the JSR-82 specification.
  
==Preconditions==
+
==Source file: BTDiscoverabilityMode.java==
 
+
In order to test this snippet there should be at least one event in the database.
+
Application needs permission to read and write PIM database data.
+
 
+
==Source file: DeleteEvent.java==
+
  
 
<code java>
 
<code java>
 
     /**
 
     /**
     * Allows to select discoer mode from Bluetooth device.
+
     * Allows to select discover mode from Bluetooth device.
 
     */
 
     */
 
     private List listModes;
 
     private List listModes;
Line 110: Line 115:
 
         }
 
         }
 
         try {
 
         try {
             printString( "Chaning discoverability mode" );
+
             printString( "Changing discoverability mode" );
 
             devLocal.setDiscoverable( modeArray[ modeIndex ] );
 
             devLocal.setDiscoverable( modeArray[ modeIndex ] );
 
         } catch ( BluetoothStateException e ) {
 
         } catch ( BluetoothStateException e ) {
Line 122: Line 127:
 
==Postconditions==
 
==Postconditions==
  
When the MIDlet is started the main form with text field will be displayed.
+
When the MIDlet is started, the main form with a text field will be displayed.
To change current bluetooth device mode you should press 'Modes' softkey, then
+
To change the current bluetooth device mode, you should press the 'Modes' softkey, which displays a list of available modes.
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==
+
 
+
* 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: [[http://wiki.forum.nokia.com/index.php/Media:BtDiscoverabilityMode.zip]].
+
 
+
* 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: [[http://wiki.forum.nokia.com/index.php/Media:BtDiscoverabilityMode.diff.zip]]
+
  
[[Category:Java ME]][[Category:Code Examples]]
+
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.

Revision as of 07:46, 2 October 2012

Article Metadata
Code ExampleTested with
Devices(s): Nokia N78, Nokia 5800 XpressMusic, Nokia 6131, Nokia 701, Nokia Asha 305
CompatibilityArticle
Keywords: javax.bluetooth.BluetoothStateException, javax.bluetooth.DiscoveryAgent, javax.bluetooth.LocalDevice
Created: olkazmin (19 Feb 2009)
Last edited: hamishwillee (02 Oct 2012)

Overview

This code example demonstrates how to change device discoverability using the Java ME 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.

147 page views in the last 30 days.