Discussion Board

Results 1 to 5 of 5

Thread: blue tooth

  1. #1
    Registered User vivhari's Avatar
    Join Date
    Jan 2009
    Posts
    13
    Hi everyone,

    I hav done a bluetooth app for my 3110c,

    But after selecting client,server it does nt respond.

    I hav done it using, RFCOMM.

    Please help

  2. #2
    Nokia Developer Champion jitu_goldie's Avatar
    Join Date
    Sep 2008
    Location
    Noida, U.P.
    Posts
    1,330
    Hi,
    Please explain ur problem in detail. We dont know what is ur server or client code then how can we go through ur problem.
    thanks,
    jitu_goldie..

    KEEP TRYING..

  3. #3
    Registered User vivhari's Avatar
    Join Date
    Jan 2009
    Posts
    13
    Sorry for not providing my code:


    This is a wrapper class that uses the features that j2me provides an easy interface for
    just sending and receiving.... (I got this from an open source site and just understood thro which i hav just used its functions in the next class)

    //=====================================================================
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.bluetooth.BluetoothStateException;
    import javax.bluetooth.DeviceClass;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;

    public class ClientServer implements DiscoveryListener{

    /** Creates a new instance of ClientServer */
    UUID RFCOMM_UUID = new UUID(0x0003);
    private String m_ServerUrl = "btspp://localhost:" + RFCOMM_UUID + ";name=rfcommtest;authorize=true";
    private StreamConnection m_StrmConn = null;
    private LocalDevice m_LclDevice = null;
    private InputStream m_Input=null;
    private OutputStream m_Output=null;
    private StreamConnectionNotifier m_StrmNotf=null;
    public boolean m_bIsServer=false,m_bServerFound=false,m_bInitServer=false,m_bInitClient=false;
    private static String m_strUrl;
    private final String SEVER_RESPONSE= "RUN_THE_GAME",CLIENT_RESPONSE="CLIENT_IS_READY";
    private DiscoveryAgent m_DscrAgent=null;

    public ClientServer(boolean isServer)
    {
    m_bIsServer = isServer;

    if(m_bIsServer)
    {
    InitServer();
    }
    else
    {
    InitClient();
    }



    }

    private void InitServer()
    {

    m_strUrl= "btspp://localhost:" + RFCOMM_UUID + ";name=rfcommtest;authorize=true";

    // m_StrmConn = BTFACADE.waitForClient(SERVICE_NBR);

    try
    {
    m_LclDevice = LocalDevice.getLocalDevice();

    m_LclDevice.setDiscoverable(DiscoveryAgent.GIAC);

    m_StrmNotf = (StreamConnectionNotifier)Connector.open(m_strUrl);

    m_StrmConn = m_StrmNotf.acceptAndOpen();

    m_bInitServer = true;

    m_Output = m_StrmConn.openOutputStream();
    m_Input = m_StrmConn.openInputStream();
    }
    catch (BluetoothStateException e)
    {
    System.err.println( "BluetoothStateException: " + e.getMessage() );
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }
    catch(Exception e)
    {
    System.err.println( "Exception: " + e.getMessage() );
    }

    }

    private void InitClient()
    {
    SearchAvailDevices();

    }

    public void SearchAvailDevices()
    {
    try
    {
    //First get the local device and obtain the discovery agent.
    m_LclDevice = LocalDevice.getLocalDevice();

    m_DscrAgent= m_LclDevice.getDiscoveryAgent();

    m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
    }
    catch (BluetoothStateException ex)
    {
    System.out.println("Problem in searching the blue tooth devices");
    ex.printStackTrace();
    }

    }


    public void SendMessages(String v_strData)
    {
    if((m_bInitClient) || (m_bInitServer) )
    {
    try
    {
    m_Output.write(v_strData.length());
    m_Output.write(v_strData.getBytes());

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

    }
    }


    public String RecieveMessages()
    {
    byte[] data = null;

    try
    {

    int length = m_Input.read();
    data= new byte[length];
    length = 0;

    while (length != data.length)
    {
    int ch = m_Input.read(data, length, data.length - length);

    if (ch == -1)
    {
    throw new IOException("Can't read data");
    }
    length += ch;
    }


    }
    catch (IOException e)
    {
    System.err.println(e);
    }


    return new String(data);
    }


    /*********************************************************************************************
    * below are the pure virtual methods of discoverlistern
    *
    *
    *******************************************************************************************/


    public void inquiryCompleted(int discType)
    {
    System.out.println("InquiryCompleted");

    }

    //called when service search gets complete
    public void serviceSearchCompleted(int transID, int respCode)
    {
    if(m_bServerFound)
    {
    try
    { //lets the communication start by setting the url and send client reponse
    m_StrmConn = (StreamConnection) Connector.open(m_strUrl);

    m_Output = m_StrmConn.openOutputStream();
    m_Input = m_StrmConn.openInputStream();

    m_Output.write(CLIENT_RESPONSE.length());
    m_Output.write(CLIENT_RESPONSE.getBytes());

    System.out.println("serviceSearchCompleted");
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }

    }
    }

    void CloseAll()
    {
    try
    {
    if(m_Output!=null)
    m_Output.close();

    if( m_Input!=null)
    m_Input.close();
    }
    catch (IOException ex)
    {
    ex.printStackTrace();
    }

    }



    //called when service found during service search
    public void servicesDiscovered(int transID, ServiceRecord[] records)
    {

    for (int i = 0; i < records.length; i++)
    {
    m_strUrl = records[i].getConnectionURL(ServiceRecord.AUTHENTICATE_ENCRYPT, false);

    System.out.println(m_strUrl);
    if(m_strUrl.startsWith("btspp")) //we have found our service protocol
    {
    m_bServerFound = true;
    m_bInitClient=true;
    break;
    }

    }


    }

    //Called when device is found during inquiry
    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
    UUID uuidSet[] = new UUID[1];
    uuidSet[0] = RFCOMM_UUID;
    int searchID = m_DscrAgent.searchServices(null,uuidSet,btDevice,this);
    }
    catch (Exception e)
    {
    System.out.println("Device Discovered Error: " + e);
    }


    }

    }
    //==========================================================================

  4. #4
    Registered User vivhari's Avatar
    Join Date
    Jan 2009
    Posts
    13
    This is the class that uses the above class to provide the functionality through GUI


    //==========================================================================

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    private Command exitCommand;
    private Command okCommand;
    private Command okCommand1;
    private Command itemCommand;
    private Form form;
    private ChoiceGroup choiceGroup;
    private Form form1;
    private StringItem stringItem;

    private ClientServer m_BlueObj;
    private boolean m_bRunThread=false;
    private boolean m_bIsServer=false;
    private int curr_board = 0;


    public HelloMIDlet() {
    }

    private void initialize() {

    }


    public void startMIDlet() {
    switchDisplayable(null, getForm());

    }



    public void resumeMIDlet() {

    }

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
    Display display = getDisplay();
    if (alert == null) {
    display.setCurrent(nextDisplayable);
    } else {
    display.setCurrent(alert, nextDisplayable);
    }

    }


    public void commandAction(Command command, Displayable displayable) {
    if (displayable == form) {
    if (command == exitCommand) {
    exitMIDlet();

    } else if (command == okCommand) {

    switchDisplayable(null, getForm1());

    String s1 = null;
    switch(choiceGroup.getSelectedIndex()+1)
    {
    case 0:

    break;
    case 1:

    m_bRunThread=true;
    m_bIsServer=true;
    createCliSer();
    if(m_BlueObj != null)
    {
    m_BlueObj.SendMessages("Hi this is Server");
    s1 = m_BlueObj.RecieveMessages();
    if(s1 != null || !s1.equals(""))
    stringItem.setText(s1);
    else
    stringItem.setText("Error");
    }



    break;
    case 2:

    Thread thread = new Thread(); thread.start();
    m_bRunThread=true;
    m_bIsServer=false;
    createCliSer();
    if(m_BlueObj != null)
    {
    s1 = m_BlueObj.RecieveMessages();
    m_BlueObj.SendMessages("Hi this is client");

    if(s1 != null || !s1.equals(""))
    stringItem.setText(s1);
    else
    stringItem.setText("Error");
    }

    // Do nothing
    // Insert post-action code here
    break;

    }
    }
    } else if (displayable == form1) {
    if (command == okCommand1) {
    // write pre-action user code here
    switchDisplayable(null, getForm());
    // write post-action user code here
    }
    }

    }

    public Command getExitCommand() {
    if (exitCommand == null) {
    // write pre-init user code here
    exitCommand = new Command("Exit", Command.EXIT, 0);
    // write post-init user code here
    }
    return exitCommand;
    }

    public Form getForm() {
    if (form == null) {

    form = new Form("Welcome", new Item[] { getChoiceGroup() });
    form.addCommand(getExitCommand());
    form.addCommand(getOkCommand());
    form.setCommandListener(this);

    }
    return form;
    }





    public Command getOkCommand() {
    if (okCommand == null) {

    okCommand = new Command("Ok", Command.OK, 0);

    }
    return okCommand;
    }

    public ChoiceGroup getChoiceGroup() {
    if (choiceGroup == null) {

    choiceGroup = new ChoiceGroup("Select", Choice.EXCLUSIVE);
    choiceGroup.append("Server", null);
    choiceGroup.append("Client", null);
    choiceGroup.setSelectedFlags(new boolean[] { false, false });
    choiceGroup.setFont(0, null);
    choiceGroup.setFont(1, null);

    }
    return choiceGroup;
    }

    public Command getOkCommand1() {
    if (okCommand1 == null) {

    okCommand1 = new Command("Ok", Command.OK, 0);

    }
    return okCommand1;
    }


    public Form getForm1() {
    if (form1 == null) {
    form1 = new Form("form1", new Item[] { getStringItem() });
    form1.addCommand(getOkCommand1());
    form1.setCommandListener(this);

    }
    return form1;
    }

    public Command getItemCommand() {
    if (itemCommand == null) {
    // write pre-init user code here
    itemCommand = new Command("Item", Command.ITEM, 0);



    // write post-init user code here
    }
    return itemCommand;
    }



    public StringItem getStringItem() {
    if (stringItem == null) {
    // write pre-init user code here
    stringItem = new StringItem("Answer:", null);
    // write post-init user code here
    }
    return stringItem;
    }

    public Display getDisplay () {
    return Display.getDisplay(this);
    }


    public void exitMIDlet() {
    switchDisplayable (null, null);
    destroyApp(true);
    notifyDestroyed();
    }


    public void startApp() {
    if (midletPaused) {
    resumeMIDlet ();
    } else {
    initialize ();
    startMIDlet ();
    }
    midletPaused = false;
    }


    public void pauseApp() {
    midletPaused = true;
    }


    public void destroyApp(boolean unconditional) {

    m_bRunThread=false;
    m_BlueObj.CloseAll();
    }

    public void createCliSer()
    {

    try
    {
    if(m_BlueObj==null)
    {
    m_BlueObj=new ClientServer(m_bIsServer);
    }


    }
    catch(Exception ex)
    {
    System.out.println(ex.getMessage());
    }


    }

    }


    //==========================================================================


    I have run this code with 2 mobiles,

    It asked to switch on, pair both the mobiles - I hav done

    After selecting Server in one and client in other, both the mobiles are stuck!!!

    Please Help!!

  5. #5
    Registered User vivhari's Avatar
    Join Date
    Jan 2009
    Posts
    13
    Hi anyone, please help me!!!

Similar Threads

  1. Find Blue Tooth Car Kit
    By Axonn in forum Symbian C++
    Replies: 0
    Last Post: 2005-12-07, 09:54
  2. Blue Tooth on 9.1 Emulator
    By Axonn in forum Symbian C++
    Replies: 0
    Last Post: 2005-11-24, 07:12
  3. blue tooth j2me
    By tomereg in forum Symbian C++
    Replies: 0
    Last Post: 2004-02-12, 14:54
  4. Blue tooth synchronisation with Nokia data suite
    By henkkraa in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-09-24, 11:15
  5. Blue tooth and PC Suite problems? I think I worked it out.
    By Erikz in forum Bluetooth Technology
    Replies: 5
    Last Post: 2003-06-17, 16:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved