Hi guys,
I am new to bluetooth communication and after a period of learning I have a small program in hand.
The basic idea is that I have a J2SE program running on the PC as a server, at the same time I have a J2ME program running in my cell phone(Nokia 5200, the platform is S40) as a client. First, the server will send a welcome message to the client, then after receiving it, the client will send a message to server, that is it.
The IDE I use is Netbeans 6.5.1 with "BlueCove version 2.1.0 on widcomm". The emulator is WTK 2.5 or Nokia S40.
The strange part is that the program seems fine, and after I deploy the client to the cellphone, sometimes the client can find the server and finish the task as I designed, everything is just fine. But most times, the cellphone can find the server but they just can not do anything, especially the server!!! It looks that the server doesn't find the client, I really don't know how to deal with it.
Is it possible that the problem is the driver of the bluetooth stack? Looking forward to your insight comments,and thanks in advance!
I have to put my client side code in another thread named: Strange question(continue): J2ME Client communication with J2SE Server through bluetooth. since it's too long for a thread.
Here are my codes(hope will help)
PHP Code:Server side
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.microedition.io.*;
import java.io.*;
import javax.bluetooth.*;
/**
*
* @author Administrator
*/
public class J2SEServer implements ActionListener, Runnable{
public LocalDevice localdevice;
public DiscoveryAgent agent;
public Boolean mServerState = false; // stop is default state
public Thread mServer = null;
public StreamConnectionNotifier btServerNotifier = null;
public UUID uuid = null;
JLabel spacerlabel = new JLabel(" ");
JButton startButton = new JButton("StartServer");
JTextArea textarea = new JTextArea("",20, 40);
JButton endButton = new JButton("EndServer");
public J2SEServer(){
//Give it the Java look and feel
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SPPServer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(textarea);
textarea.setEditable(false);
Container cp = frame.getContentPane();
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS));
startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
startButton.addActionListener(this);
cp.add(startButton);
endButton.setAlignmentX(Component.CENTER_ALIGNMENT);
endButton.addActionListener(this);
cp.add(endButton);
spacerlabel.setAlignmentX(Component.CENTER_ALIGNMENT);
cp.add(spacerlabel);
scrollPane.setAlignmentX(Component.CENTER_ALIGNMENT);
cp.add(scrollPane);
frame.pack();
frame.setVisible(true);
updateStatus("[server:] FileServer Application started");
updateStatus("[server:] Press the \"Start Server\" button to wait for client devices");
}
public void startServer() {
if (mServer !=null)
return;
mServer = new Thread(this);
mServer.start();
}
public void endServer() {
if (mServer == null)
return;
try {
mServer.join();
} catch (Exception ex) {
ex.printStackTrace();
}
mServer = null;
}
public void run(){
try {
uuid = new UUID("F0E0D0C0B0A000908070605040302010", false);
localdevice = LocalDevice.getLocalDevice();
localdevice.setDiscoverable(DiscoveryAgent.GIAC);
}catch (Exception e){
updateStatus("Cant init set discvover");
e.printStackTrace();
}
String url = "btspp://localhost:"+uuid+";name="+localdevice.getFriendlyName()+";authenticate=false";
String URL = url;
updateStatus("Server url:"+url);
try{
btServerNotifier = (StreamConnectionNotifier)Connector.open(URL);
} catch ( Exception e) {
updateStatus("Failed to start the server");
e.printStackTrace();
}
ServiceRecord record = null;
/* Get the ServiceRecord associated with the notifier */
try {
record = localdevice.getRecord(btServerNotifier);
}catch (IllegalArgumentException iae){
updateStatus("failed to get the ServiceRecord");
iae.printStackTrace();
}
/*
* Manipulate the serviceRecord to meet our needs.
*/
DataElement elm = null;
//set service description
elm = new DataElement(DataElement.STRING,"BT service");
record.setAttributeValue(0x100, elm);
//set service provider name
elm = new DataElement(DataElement.STRING,"Alfred");
record.setAttributeValue(0x102, elm);
//Update the record, else changes are lost
try{
localdevice.updateRecord(record);
}catch(ServiceRegistrationException e){
e.printStackTrace();
}
//open the StreamConnection, the default value of mServerState is false
while (mServerState)
{
StreamConnection conn = null;
try {
updateStatus("[server:] Now waiting for a client to connect");
conn = btServerNotifier.acceptAndOpen();
} catch (IOException e) {
updateStatus("can not go to acceptAndOpen");
e.printStackTrace();
}
if (conn != null) processConnection(conn);
}
}
public void processConnection(StreamConnection conn) {
updateStatus("[server:] A client is now connected");
String welcome = "Welcome to Canada!";
try {
OutputStream out = conn.openOutputStream();
out.write(welcome.getBytes());
out.flush();
out.close();
updateStatus("sent out :"+welcome);
System.out.println(welcome);
} catch (Exception e){
e.printStackTrace();
}
try{
InputStream in = conn.openInputStream();
ByteArrayOutputStream incomemessage = new ByteArrayOutputStream();
int mark;
while ((mark = in.read()) != -1) {
incomemessage.write(mark);
}
updateStatus(incomemessage.toString()+" from alfred");
System.out.println(incomemessage.toString());
incomemessage.close();
in.close();
}catch(Exception e){
updateStatus("can not finished connection.");
e.printStackTrace();
}
try {
conn.close();
updateStatus("[server:] Finished connection");
}catch (Exception e ){
updateStatus("can not finished connection.");
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand()).equals("StartServer")) {
startButton.setEnabled(false);
mServerState = true; // set server state started
startServer();
}
if ((e.getActionCommand()).equals("EndServer")) {
endButton.setEnabled(false);
startButton.setEnabled(true);
mServerState = false;
updateStatus("now mServerState is false");
endServer();
}
}
public void updateStatus(String message){
textarea.append("\n" + message);
}
public static void main(String[] args) {
new J2SEServer();
}
}

Reply With Quote

