can anyone help me in setting up a connection from my phone with another bluetooth device. The mobile application is in java
Here is the code:
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.Connector;
public class BluetoothDeviceDiscovery extends MIDlet implements CommandListener,
DiscoveryListener {
private Display display;
/** * For displaying found devices. */
private List listDev;
private Command cmdExit;
private Command cmdOk;
private Command cmdDiscover;
private Command cmdStopDiscover;
private Command buyCommand;
private LocalDevice devLocal;
private DiscoveryAgent discoverAgent;
private static final boolean masterValue= true;
ServiceRecord services;
OutputStream os = null;
StreamConnection conn = null;
String addr;
/** * Constructor. Constructs the object and initializes displayables. /
public BluetoothDeviceDiscovery() {
InitializeComponents();
} /* * Initializes a ListBox object and adds softkeys. */
protected void InitializeComponents() {
display = Display.getDisplay( this );
//initializing device list
listDev = new List( "Device list", List.IMPLICIT );
cmdExit = new Command( "Exit", Command.EXIT, 1 );
cmdDiscover = new Command( "Start", Command.SCREEN, 1);
cmdStopDiscover = new Command( "Stop", Command.SCREEN, 1);
cmdOk = new Command( "OK", Command.SCREEN, 1);
buyCommand = new Command("Buy", Command.SCREEN, 1);
listDev.addCommand( cmdExit );
listDev.addCommand( cmdDiscover );
listDev.setCommandListener( this );
} /** * From MIDlet. * Called when MIDlet is started. * @throws javax.microedition.midlet.MIDletStateChangeException /
public void startApp() throws MIDletStateChangeException {
display.setCurrent( listDev );
} /* * From MIDlet. * Called to signal the MIDlet to enter the Paused state. /
public void pauseApp() { //No implementation required
} /* * From MIDlet. * Called to signal the MIDlet to terminate. * @param unconditional whether the MIDlet has to be unconditionally * terminated * @throws javax.microedition.midlet.MIDletStateChangeException /
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
exitMIDlet();
}
/* * 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 == cmdExit ) {
exitMIDlet();
}
if( command == cmdDiscover ) {
int err = 0;
err = initLocalDevice();
if( err != 0 ){
return;
}
err = startDeviceSearch();
if( err != 0) {
return;
} //we do not need a start soft key when in descovery mode
listDev.removeCommand( cmdDiscover ); //now we add stop soft key
listDev.addCommand( cmdStopDiscover );
} if( command == cmdStopDiscover ) {
stopDiscover();
//we remove stop softkey
listDev.removeCommand( cmdStopDiscover ); //adding start soft key
//listDev.addCommand( cmdOk );
//if( command == cmdOk ) {
//listDev.removeCommand( cmdOk );
//listDev.addCommand( buyCommand );
//int securityValue=0x00;
//String url;
//url= services.getConnectionURL( securityValue, masterValue);
try {
conn = (StreamConnection) Connector.open ("btspp://addr:1", Connector.WRITE);
os = conn.openOutputStream();
os.write('0');
//is = conn.openInputStream();
//byte buffer[] = new byte[24];
//is.read(buffer);
//message = new String(buffer);
//System.out.println(message);
//listDev.append(new StringItem(null, "\n" + message));
} catch (IOException io) {
// handle exception
//listDev.append(new StringItem(null, "\n" + "Error"));
} finally {
if (conn != null) {
try {
conn.close();
} catch (IOException ignored) {}
}
if (os != null) {
try {
os.close();
} catch (IOException ignored) {}
}
//if (is != null) {
//try {
// is.close();
//} catch (IOException ignored) {}
//}
}
////////}
}
}
protected void exitMIDlet() {
stopDiscover();
notifyDestroyed(); } /** * Gets a reference to LocalDevice and saves it to devLocal variable. * @return '0' if local device reference getting succeded. '-1' on exception. /
protected int initLocalDevice() {
try {
devLocal = LocalDevice.getLocalDevice();
} catch (BluetoothStateException e) {
showErrAlert( "Error getting local device!" );
return -1;
}
return 0;
} /* * Clears device listbox, retreves DiscoveryAgent instance and saves it to * discoveryAgent variable. Starts device inquiry mode with access code * General/Unlimited Inquiry Access Code (GIAC). * @return '0' function execution was successfull. * '-1' if startInquiry threw an exception. * @see JSR82 for more info. */
protected int startDeviceSearch() {
discoverAgent = devLocal.getDiscoveryAgent();
try {
listDev.deleteAll();
discoverAgent.startInquiry( DiscoveryAgent.GIAC,
this );
} catch( BluetoothStateException e ) {
showErrAlert( "Error starting device enquiry!" );