×

Discussion Board

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

    Post bluetooth remote control 2

    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
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,085

    Re: bluetooth remote control 2

    On which device you are testing this application, are you able to exit the app and launch again... in that case it works ?
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

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

    Post Re: bluetooth remote control 2

    Nokia series 40 mobile 311o classic and it exits the application properly

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: bluetooth remote control 2

    From javax.bluetooth.DiscoveryAgent
    startInquiry(int accessCode, DiscoveryListener listener)
    you can get the call back for device, then put
    Use searchServices(int[] attrSet,
    UUID[] uuidSet,
    RemoteDevice btDev,
    DiscoveryListener discListener)
    throws BluetoothStateException


    with DiscoveryListener.servicesDiscovered(int transID,
    ServiceRecord[] servRecord) you can get call back for the available service and if it matchs your need do your processing.
    Thanks,
    Ekta

Similar Threads

  1. bluetooth remote control
    By bblackbelt in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2007-05-01, 19:31
  2. Bluetooth remote control
    By giladn in forum Bluetooth Technology
    Replies: 0
    Last Post: 2007-03-05, 15:14
  3. Remote control via bluetooth-capable mobile phone
    By cesarmanalo in forum Bluetooth Technology
    Replies: 2
    Last Post: 2004-08-31, 02:55
  4. Remote control/ other neat features via bluetooth
    By jockl in forum Bluetooth Technology
    Replies: 0
    Last Post: 2002-06-09, 19:11
  5. Replies: 1
    Last Post: 2002-05-08, 16:38

Posting Permissions

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