by lawrie » Sun Sep 16, 2007 5:11 pm
I do not have Andre Nijholt's email address - I think Brian has it.
The program below works for me for changing the name of the NXT.
I am not sure if what I have done is correct, but it seems to work, apart from, for some reason, you have to press ENTER twice to select the SetName text box.
import javax.microedition.lcdui.*;
import lejos.nxt.comm.*;
/**
*
* @author Lawrie Griffiths
*/
public class SetName implements CommandListener {
private static final int CMDID_BACK_TO_MAIN = 1;
private static final int CMDID_EXIT_APP = 2;
private static final Command BACK_COMMAND = new Command(CMDID_BACK_TO_MAIN, Command.BACK, 0);
private static final Command EXIT_COMMAND = new Command(CMDID_EXIT_APP, Command.STOP, 2);
private List menu = new List("Set Name", Choice.IMPLICIT);
private Ticker ticker = new Ticker("Tools");
private TextBox input = new TextBox("Enter Name:", "", 16, TextField.ANY);
private Alert exitAlert = new Alert("Exit");
private Display display;
/**
* Start application
*
* @param polling Select method for button handling
*/
public void startApp(boolean polling) {
// Create main menu
menu = new List("Tools", Choice.IMPLICIT);
menu.append("Set Name", null);
menu.setSelectedIndex(0, true);
menu.addCommand(EXIT_COMMAND);
menu.setCommandListener(this);
menu.setTicker(ticker);
// Set textbox properties
input.addCommand(BACK_COMMAND);
input.setCommandListener(this);
// Start displaying main menu and handling buttons
display = Display.getDisplay();
display.setCurrent(menu);
display.show(polling);
}
/**
* Handle events.
*/
public void commandAction(Command c, Displayable d) {
if (c.getCommandId() == CMDID_BACK_TO_MAIN) {
if (d == input) {
// Name has changed
String nameString = input.getText();
byte [] nameBytes = new byte[16];
for(int i=0;i<nameString.length();i++) {
nameBytes[i] = (byte) nameString.charAt(i);
}
Bluetooth.setFriendlyName(nameBytes);
}
// Display main menu again
display.setCurrent(menu);
} else if (c.getCommandId() == CMDID_EXIT_APP) {
// Request to exit application
exitAlert.setType(Alert.ALERT_TYPE_CONFIRMATION);
exitAlert.setString("Exit Lejos?");
exitAlert.setCommandListener(this);
display.setCurrent(exitAlert);
} else {
// Handle system commands
if (d == exitAlert) {
if (exitAlert.getConfirmation()) {
display.quit();
} else {
display.setCurrent(menu);
}
} else if (d == menu) {
List list = (List) display.getCurrent();
if (list.getSelectedIndex() == 0) {
display.setCurrent(input);
}
}
}
}
public static void main(String[] args) {
new SetName().startApp(true);
}
}