ftr
unread,Apr 25, 2008, 12:23:36 PM4/25/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to bluecove-users
I'm trying to write a bluetooth server that simply reads in a string.
When running the server, no exception are thrown, however it seems as
if my phone and my pc don't connect to each other, as no message gets
sent over.
The server code is below (its a tweak of a J2ME server I was playing
with a while back)
------------------------------------------------------------------
package server;
import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
public class blueServer implements Runnable{
public static final UUID uuid = new
UUID("27012f0c68af4fbf8dbe6bbaf7aa432a", false);
StreamConnectionNotifier notifier;
StreamConnection conn;
LocalDevice localDevice;
ServiceRecord serviceRecord;
OutputStream output;
private boolean reqStart;
private boolean isInit;
private static String serverUrl = "btspp://localhost:" + uuid +
";name=rfcommtest;authorize=true";
public static void main(String[]args){
blueServer bs = new blueServer();
}
public blueServer() {
isInit = false;
Thread thread = new Thread(this);
thread.start();
}
public void run() {
if (!isInit)
{
// Initialization is done in the thread to avoid dead lock
'isInit'
//ensures it is done once only
try
{
conn = null;
localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable( DiscoveryAgent.GIAC );
notifier =
(StreamConnectionNotifier)Connector.open(serverUrl);
}//end try
catch (BluetoothStateException e)
{
System.err.println( "BluetoothStateException: " +
e.getMessage() );
}//end catch
catch (IOException e)
{
System.err.println( "IOException: " +
e.getMessage() );
}//end catch
isInit=true;
System.out.println( "Starting Echo Server" );
}//end if
try
{
System.out.println("\n\nServer Running...");
// Pauses thread until Transmission occurs
conn = notifier.acceptAndOpen();
System.out.println("accepted, opened");
// Read Data Transmission
String msg = readData(conn);
System.out.println("recieved"+msg);
// Send Back a Message
msg = "Hello Back from Server";
/*output = conn.openOutputStream();
output.write(msg.length()); // length is 1 byte
output.write(msg.getBytes());
output.close();*/
run();
}//end try
catch (Exception ex)
{
System.err.println("Bluetooth Server Running Error: " +
ex);
}//end catch
}//end run()
public final static String readData(StreamConnection conn) {
InputStream input = null;
byte[] data = null;
try
{
input = conn.openInputStream();
// Probably want to throw an exception if length is not
greater then 0
int length = input.read();
data= new byte[length];
length = 0;
// Assemble data
while (length != data.length)
{
int ch = input.read(data, length, data.length -
length);
if (ch == -1)
{
throw new IOException("Can't read data");
}//end if
length += ch;
}
}
catch (IOException e)
{
System.err.println(e);
}
finally
{
// close input stream
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
}
}
}
return new String(data);
}
}
------------------------------------------------------------------
The code in the J2ME MIDlet that connects to this is below:
-------------------------------------------------------------------
package connection;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
import remotemidlet.*;
public class Bluetooth implements DiscoveryListener{
public static final UUID uuid=new
UUID("27012f0c68af4fbf8dbe6bbaf7aa432a", false);
private DiscoveryAgent discoveryAgent;
private RemoteDevice[] remoteDevices;
private UUID[] uuidSet;
private String serviceUrl;
//String msg;
boolean connected;
public Bluetooth(RemoteMidlet remote1){
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
}//end try
catch (Exception e)
{
System.out.println(e);
}//end catch
}//end Bluetooth
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass
cod) {
try
{
// Get Device Info
System.out.println("Device Discovered");
System.out.println("Major Device Class: " +
cod.getMajorDeviceClass() + " Minor Device Class: " +
cod.getMinorDeviceClass());
System.out.println("Bluetooth Address: " +
btDevice.getBluetoothAddress());
System.out.println("Bluetooth Friendly Name: " +
btDevice.getFriendlyName(true));
// Search for Services
uuidSet = new UUID[1];
uuidSet[0] = uuid;
int searchID =
discoveryAgent.searchServices(null,uuidSet,btDevice,this);
}//end try
catch (Exception e)
{
System.out.println("Device Discovered Error: " + e);
}//end catch
}//end deviceDiscovered(RemoteDevice, DeviceClass)
public void inquiryCompleted(int discType) {
System.out.println("InquiryCompleted");
}//end inquiryCompleted(int)
public void servicesDiscovered(int transID, ServiceRecord[]
servRecord) {
System.out.println("ServicesDiscovered");
for(int i=0;i<servRecord.length;i++)
{
serviceUrl = servRecord[i].getConnectionURL(0,false);
}//end for
}//end servicesDiscovered (int, ServiceRecord[])
public void serviceSearchCompleted(int transID, int responseCode)
{
if(responseCode == SERVICE_SEARCH_ERROR)
System.out.println("SERVICE_SEARCH_ERROR\n");
if(responseCode == SERVICE_SEARCH_COMPLETED)
{
connected=true;
System.out.println("SERVICE_SEARCH_COMPLETED\n");
System.out.println("Service URL: " + serviceUrl);
System.out.println("connected");
}//end if
if(responseCode == SERVICE_SEARCH_TERMINATED)
System.out.println("SERVICE_SEARCH_TERMINATED\n");
if(responseCode == SERVICE_SEARCH_NO_RECORDS)
System.out.println("SERVICE_SEARCH_NO_RECORDS\n");
if(responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE
\n");
}//end serviceSearchCompleted(int, int)
public void sendMessage(String msg){
System.out.println("sendMessage called");
if(connected==true)
{
System.out.println("conected==true");
StreamConnection conn = null;
try
{
System.out.println("attempting to send");
conn = (StreamConnection)Connector.open(serviceUrl);
OutputStream output =
conn.openOutputStream();
output.write(msg.length());
output.write(msg.getBytes());
output.close();
}//end try
catch (Exception ex)
{
System.out.println(ex);
} //end catch
finally
{
try
{
System.out.println("Attempting to close the
connection");
conn.close();
connected=false;
}//end try
catch (IOException ioe)
{
System.out.println("Error Closing connection " +
ioe);
}//end catch
}//end finally
}//end if
else
{
System.out.println("Not connected, cannot send. Attempting
to conenct");
reconnect(msg);
}//end else
}//end sendMessage
public void reconnect(String message){
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
}//end try
catch (Exception e)
{
System.out.println(e);
}//end catch
System.out.println(connected);
sendMessage(message);
}//end reconnect()
}//end class Bluetooth
------------------------------------------------
can anyone see whats going wrong? I tried googling for help on
bluetooth servers but the only things i see are usualyl for OBEX,
which seems to be far more complicated than what i need...