×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Aug 2009
    Location
    Iran - Tabriz
    Posts
    111

    javax.bluetooth.BluetoothExeption: unable to swithc master

    Hi
    i want to write bluetooth base app for send and recive string between two device . i have problem . i send string from device A and device B recive it but when i try to send answer from device B to A i get this notifier :
    javax.bluetooth.BluetoothExeption: unable to swithc master
    it is becouse this part of code :
    Code:
     StreamConnection conn =(StreamConnection) Connector.open(connString);
    now what should i do for slove this probleam ?
    thanks

  2. #2
    Regular Contributor
    Join Date
    Aug 2009
    Location
    Iran - Tabriz
    Posts
    111

    Re: javax.bluetooth.BluetoothExeption: unable to swithc master

    this is my code :
    Client Class :
    Code:
    import java.io.*;
    import javax.bluetooth.*;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.lcdui.*;
    
    /**
     *
     * @author mahdi
     */
    class Client implements CommandListener, Runnable {
    
        Display display;
        String msg;
        Midlet midlet;
        Form frm;
        Command cmdBack = new Command("Back", Command.BACK, 1);
        LocalDevice local = null;
        DiscoveryAgent agent = null;
        Thread thread;
        StreamConnection conn = null;
        OutputStream out = null;
    
        public Client(String string, Midlet midlet, Display display) {
    
            this.display = display;
            this.midlet = midlet;
            this.msg = string;
            frm = new Form("Send Form");
            frm.addCommand(cmdBack);
            frm.setCommandListener(this);
            thread = new Thread(this);
            thread.start();
    
            display.setCurrent(frm);
        }
    
        private void doDiscovery() {
            try {
                local = LocalDevice.getLocalDevice();
                agent = local.getDiscoveryAgent();
    
                String connString = agent.selectService(new UUID("86b4d249fb8844d6a756ec265dd1f6a3", false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, true);
                if (connString != null) {
    
    
                    try {
                        conn = (StreamConnection) Connector.open(connString);
                    } catch (IOException ex) {
                        frm.append(ex.toString() + "1");
                    }
    
                    try {
                        out = conn.openOutputStream();
                    } catch (IOException ex) {
                        frm.append(ex.toString() + "2");
                    }
                    try {
                        out.write(msg.getBytes());
                    } catch (IOException ex) {
                        frm.append(ex.toString() + "3");
                    }
                    try {
                        out.close();
                    } catch (IOException ex) {
                        frm.append(ex.toString() + "4");
                    }
                    try {
                        conn.close();
                    } catch (IOException ex) {
                        frm.append(ex.toString() + "5");
                    }
                    System.out.println("Message sent currectly");
    
                } else {
                    frm.append("connString == null");
                }
            } catch (BluetoothStateException ex) {
                frm.append(ex.toString() + "6");
            }
        }
    
        public void commandAction(Command c, Displayable d) {
            if (c == cmdBack) {
                display.setCurrent(midlet.tb);
            }
        }
    
        public void run() {
            doDiscovery();
        }
    }
    Server Class :
    Code:
    import java.io.*;
    import javax.bluetooth.*;
    import javax.microedition.io.Connector;
    import javax.microedition.io.StreamConnection;
    import javax.microedition.io.StreamConnectionNotifier;
    import javax.microedition.lcdui.*;
    
    /**
     *
     * @author mahdi
     */
    class Server implements  Runnable {
    
        Display display;
        String msg;
        Midlet midlet;
        Thread thread;
        private boolean endRecive = false;
    
        public Server(Midlet midlet, Display display) {
            this.display = display;
            this.midlet = midlet;
            thread = new Thread(this);
            thread.start();
        }
    
    
    
        private void recive() {
            try {
                LocalDevice local = LocalDevice.getLocalDevice();
                if (!local.setDiscoverable(DiscoveryAgent.GIAC)) {
                    midlet.tb.setString("Failed to change to the discoverable mode");
                    return;
                }
                StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + "86b4d249fb8844d6a756ec265dd1f6a3");
                StreamConnection conn = notifier.acceptAndOpen();
                InputStream in = conn.openInputStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int data;
                while ((data = in.read()) != -1) {
                    out.write(data);
                }
                midlet.tb.delete(0, midlet.tb.size());
                midlet.tb.setString(out.toString());
                in.close();
                conn.close();
                notifier.close();
                endRecive = true;
                thread.interrupt();
                
    
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
        }
    
        public void run() {
            while (endRecive != true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recive();
            }
        }
    }
    and midlet :
    Code:
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.TextBox;
    import javax.microedition.lcdui.TextField;
    import javax.microedition.midlet.*;
    
    /**
     * @author mahdi
     */
    public class Midlet extends MIDlet implements CommandListener {
    
        Display display = Display.getDisplay(this);
        TextBox tb = new TextBox("Chat", null, 100, TextField.ANY);
        Command cmdSend = new Command("Send", Command.OK, 1);
        Command cmdRecive = new Command("Recive", Command.OK, 2);
        Command cmdClear = new Command("Clear...", Command.OK, 3);
        Command cmdExit = new Command("Exit", Command.EXIT, 4);
    
        public void startApp() {
            tb.addCommand(cmdSend);
            tb.addCommand(cmdRecive);
            tb.addCommand(cmdClear);
            tb.addCommand(cmdExit);
            tb.setCommandListener(this);
            display.setCurrent(tb);
        }
    
        public void pauseApp() {
        }
    
        public void destroyApp(boolean unconditional) {
            notifyDestroyed();
        }
    
        public void commandAction(Command c, Displayable d) {
            if (c == cmdExit) {
                destroyApp(true);
            } else if (c == cmdClear) {
                tb.delete(0, tb.size());
            } else if (c == cmdSend && tb.getString().length() > 0) {
                new Client(tb.getString(), this, display);
    
            } else if (c == cmdRecive) {
                tb.setString("Wait ...");
                new Server(this, display);
    
            }
        }
    }
    please help me
    thanks

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: javax.bluetooth.BluetoothExeption: unable to swithc master

    Because your UUID is well known to the Internet, I guess you copied the code from an example. In this case, I recommend …
    Quote Originally Posted by mahdi68 View Post
    String connString = agent.selectService(new UUID([…]), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, true);
    You force your client to be the master. Have you tried false instead of true for the parameter master? See Javadoc for details. However, I wonder why that exception is raised. Is your server connected to another device already?

  4. #4
    Regular Contributor
    Join Date
    Aug 2009
    Location
    Iran - Tabriz
    Posts
    111

    Re: javax.bluetooth.BluetoothExeption: unable to swithc master

    I guess you copied the code from an example
    yes i use this examples : http://www.kjhole.com/Standards/BT/BTdownloads.html
    app work well but when i try send answer that message shown

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

    Re: javax.bluetooth.BluetoothExeption: unable to swithc master

    Many of examples out there are not very well tested. Again, if you are new to J2ME, MIDlets and Bluetooth, I recommend to start with a book or tutorials rather than code examples as you will have to debug a lot on your own. There are a lot of bugs, tweaks and lessons to be learnt. Bluetooth is complicated. I do not have a good J2ME book at hand right now, however, start with the references of this one for JSR-82 (perhaps your library has its previous edition) or read at least the PDF coming with the specification. If you like to start with tutorials, continue here…

Similar Threads

  1. bluetooth piconet, master to all slaves
    By lucerne2 in forum Mobile Java General
    Replies: 2
    Last Post: 2009-10-03, 09:38
  2. Bluetooth master
    By manubemanu in forum Bluetooth Technology
    Replies: 1
    Last Post: 2009-05-18, 14:37
  3. Javax.bluetooth VS javax.OBEX
    By werter10 in forum Bluetooth Technology
    Replies: 1
    Last Post: 2009-02-27, 17:26
  4. Unable to build the application while using javax.xml.parsers.*;
    By gsunilkumarbca in forum Mobile Java General
    Replies: 2
    Last Post: 2008-09-27, 20:21
  5. Bluetooth - master and slave
    By sopta007 in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-10-29, 06:22

Posting Permissions

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