×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    4

    Post Bluetooth Remote Control1

    My code runs only once! properly

    Please give me suggestion about why this code's not working continuously.

    Client Code:

    import java.io.IOException;
    import java.io.OutputStream;

    import javax.bluetooth.BluetoothStateException;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;


    public class BTclient{

    StreamConnection conn;
    public String sendTopc(String s1)
    {

    String done = null;


    try{
    //set up the bluetooth connection:
    LocalDevice local = LocalDevice.getLocalDevice();


    DiscoveryAgent agent = local.getDiscoveryAgent();
    String connString = agent.selectService(new UUID("6040", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

    if(connString != null){

    try {
    conn = (StreamConnection) Connector.open(connString);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    try {


    OutputStream os = conn.openOutputStream();

    os.write(s1.getBytes());
    os.flush();
    conn.close();
    done="Message sent to PC";


    }
    catch(IOException e)
    {
    e.printStackTrace();
    }




    } else {
    //show the user an error if it occurs
    done="Unable to locate service";
    }
    } catch (BluetoothStateException e){
    //show the user an error if it occurs
    done="Problem in the connection";
    }
    return(done);
    }




    }

    Server Code


    package server;

    import java.awt.BorderLayout;
    import java.io.InputStream;

    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.LocalDevice;

    public class Server {
    public static final String UUID_STRING = "6040";
    public static final String SERVICE_NAME = "BTCLIENT";
    private StreamConnectionNotifier server;

    JFrame jframe;
    JTextArea textArea;

    public static void main(String[] args) {
    Server svr = new Server();
    svr.doWork();
    }

    public void doWork() {
    this.jframe = new JFrame("BT Server");
    this.jframe.setLayout(new BorderLayout());

    this.textArea = new JTextArea(6, 20);
    JScrollPane jsp = new JScrollPane(this.textArea);
    this.jframe.add(jsp, BorderLayout.CENTER);

    this.jframe.pack();
    this.jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.jframe.setVisible(true);

    startServer();
    }

    public void logMessage(String message) {
    this.textArea.setText(this.textArea.getText() + message + "\n");
    this.textArea.setCaretPosition(this.textArea.getText().length());
    }

    public void startServer() {
    LocalDevice local;
    try {
    local = LocalDevice.getLocalDevice();
    local.setDiscoverable(DiscoveryAgent.GIAC);
    //this.logMessage("max of "+ LocalDevice.getProperty("bluetooth.connected.devices.max")+ " connection(s) supported");

    String url = "btspp://localhost:" + UUID_STRING + ";name="+ SERVICE_NAME;

    server = (StreamConnectionNotifier) Connector.open(url);
    this.logMessage("waiting for connection...");

    StreamConnection conn = server.acceptAndOpen();
    this.logMessage("connection opened");

    InputStream is = conn.openInputStream();
    byte buffer[] = new byte[100];
    while (true) {
    int numChars = is.read(buffer);
    String s = new String(buffer);
    logMessage("received from mobile phone: " + s.substring(0, numChars));
    conn.close();
    startServer();
    }

    } catch (Exception e) {
    this.logMessage(e.getMessage());
    }
    }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    8

    Re: Bluetooth Remote Control1

    which package of J2SE are you using?

    I once used the Bluecove to do something like this

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    4

    Re: Bluetooth Remote Control1

    I am using jdk 6 update 17 and netbeans 6.7 and bluecove.jar file for the java bluetooth packages.

Similar Threads

  1. Remote Bluetooth Address
    By mdzahidul_haque in forum Symbian
    Replies: 5
    Last Post: 2010-04-28, 17:07
  2. Bluetooth remote
    By Nokia5700 in forum Bluetooth Technology
    Replies: 2
    Last Post: 2008-08-25, 14:07
  3. Replies: 1
    Last Post: 2008-03-20, 17:31
  4. bluetooth remote control
    By bblackbelt in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2007-05-01, 19:31
  5. Bluetooth remote control
    By giladn in forum Bluetooth Technology
    Replies: 0
    Last Post: 2007-03-05, 15:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×