javax.bluetooth.BluetoothException:无法切换主设备

发布于 2024-09-14 22:43:46 字数 327 浏览 4 评论 0原文

我想编写蓝牙基础应用程序,用于在两个设备之间发送和接收字符串。我有问题。我从设备 A 发送字符串,设备 B 接收它,但是当我尝试从设备 B 向 A 发送答案时,我收到此通知程序:

javax.bluetooth.BluetoothException:无法切换主设备

,因为这部分代码:

 StreamConnection conn =(StreamConnection) Connector.open(connString);

现在我应该做什么来解决这个问题?

谢谢

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 :

 StreamConnection conn =(StreamConnection) Connector.open(connString);

now what should i do for slove this probleam ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2024-09-21 22:43:46

客户端类:

import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.*;


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();
}

}

服务器类:

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.*;


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();
    }
}

}

和 midlet:

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.*;


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);

    }
}
}

Client class :

import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.*;


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 :

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.*;


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 :

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.*;


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);

    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文