如何向蓝牙设备发送消息?

发布于 2024-10-30 14:57:35 字数 4128 浏览 1 评论 0原文

使用j2me中的蓝牙API,我想向另一部手机发送消息。我已经能够发现相应设备上的设备和服务。但是,当我尝试从服务器向客户端发送消息时,我也能够连接到服务。消息已写入,但客户端似乎没有收到它..

    public void startServer() throws IOException {
                UUID uuid = new UUID("1101", false);
                //Create the service url
                String connectionString = "btspp://localhost:" + uuid + ";name=xyz";
                //open server url
                StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString);
                //Wait for client connection
                System.out.println("\nServer Started. Waiting for clients to connect...");
                StreamConnection connection = streamConnNotifier.acceptAndOpen();
                RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
                System.out.println("Remote device address: " + dev.getBluetoothAddress());
                System.out.println("Remote device name: " + dev.getFriendlyName(true));
                Survey.setTitle(dev.getFriendlyName(true));
                //read string from spp client
                try {
                      DataInputStream in =  connection.openDataInputStream();
                      OutputStream writer=connection.openDataOutputStream();


                      String str="";
                        TextField  textfield;
                        for (int i=0;i<questions.size();i++){
                            textfield = (TextField) questions.elementAt(i);
                            str += formatSurvey(textfield,i)+"&";

                        }
                     writer.write(str.getBytes(), 0, str.getBytes().length);
                     writer.flush();
                    System.out.println("Written to client "+str);

                    System.out.println("Reading  "+in.readUTF());
                    try {
                        displaySurveyresults(str);
                    }
                    catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                      streamConnNotifier.close();
                }
                catch(Exception e){
                    System.err.println(e.getMessage());
                }
          }



     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            switchDisplayable(null , getList1());
            list1.append(servRecord.toString(), null);
            System.out.println("Service discovered..."+servRecord.toString());
            for (int i=0;i<servRecord.length;i++){
                try {
                    System.out.println("Test1");
                    //StreamConnection con = (StreamConnection) Connector.open(servRecord[i].getConnectionURL(0 , false));
                    String connURL = servRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    // Open connection
                    StreamConnection con = (StreamConnection) Connector.open(connURL);

                    System.out.println("Test2");
                    DataInputStream in =  con.openDataInputStream();
                    System.out.println("Test3"+in.readUTF());
                    //con.openDataOutputStream().write(142);
                    System.out.println("Test4   "+in.available());
                    byte[] bte=new byte[in.available()];
                    System.out.println("Test5  "+bte.length);
                    in.read(bte);
                    System.out.println("Test6");
                    for (int l=0;l<bte.length;l++){
                        System.out.println(bte[i]);
                        System.out.println("Test7");
                        stringItem.setText(stringItem.getText()+1 + bte[i]);
                    }
                    OutputStream outStream=con.openOutputStream();
                    OutputStreamWriter writer = new OutputStreamWriter(outStream);
                    writer.write("Vimal");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
}

我是否在某个地方犯了错误,因为这些是来自网络的代码?

Using the bluetooth API in j2me, I want to send a message to another mobile phone. I have been able to discover devices and services on the corresponding devices. I have also been able to connect to the services however when I try to send a message from the server to the client. The message is written but the client does not seem to receive it ..

    public void startServer() throws IOException {
                UUID uuid = new UUID("1101", false);
                //Create the service url
                String connectionString = "btspp://localhost:" + uuid + ";name=xyz";
                //open server url
                StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString);
                //Wait for client connection
                System.out.println("\nServer Started. Waiting for clients to connect...");
                StreamConnection connection = streamConnNotifier.acceptAndOpen();
                RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
                System.out.println("Remote device address: " + dev.getBluetoothAddress());
                System.out.println("Remote device name: " + dev.getFriendlyName(true));
                Survey.setTitle(dev.getFriendlyName(true));
                //read string from spp client
                try {
                      DataInputStream in =  connection.openDataInputStream();
                      OutputStream writer=connection.openDataOutputStream();


                      String str="";
                        TextField  textfield;
                        for (int i=0;i<questions.size();i++){
                            textfield = (TextField) questions.elementAt(i);
                            str += formatSurvey(textfield,i)+"&";

                        }
                     writer.write(str.getBytes(), 0, str.getBytes().length);
                     writer.flush();
                    System.out.println("Written to client "+str);

                    System.out.println("Reading  "+in.readUTF());
                    try {
                        displaySurveyresults(str);
                    }
                    catch(Exception e){
                        System.out.println(e.getMessage());
                    }
                      streamConnNotifier.close();
                }
                catch(Exception e){
                    System.err.println(e.getMessage());
                }
          }



     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            switchDisplayable(null , getList1());
            list1.append(servRecord.toString(), null);
            System.out.println("Service discovered..."+servRecord.toString());
            for (int i=0;i<servRecord.length;i++){
                try {
                    System.out.println("Test1");
                    //StreamConnection con = (StreamConnection) Connector.open(servRecord[i].getConnectionURL(0 , false));
                    String connURL = servRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                    // Open connection
                    StreamConnection con = (StreamConnection) Connector.open(connURL);

                    System.out.println("Test2");
                    DataInputStream in =  con.openDataInputStream();
                    System.out.println("Test3"+in.readUTF());
                    //con.openDataOutputStream().write(142);
                    System.out.println("Test4   "+in.available());
                    byte[] bte=new byte[in.available()];
                    System.out.println("Test5  "+bte.length);
                    in.read(bte);
                    System.out.println("Test6");
                    for (int l=0;l<bte.length;l++){
                        System.out.println(bte[i]);
                        System.out.println("Test7");
                        stringItem.setText(stringItem.getText()+1 + bte[i]);
                    }
                    OutputStream outStream=con.openOutputStream();
                    OutputStreamWriter writer = new OutputStreamWriter(outStream);
                    writer.write("Vimal");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
}

have I erred somewhere bcause these are codes from the Net?

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

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

发布评论

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

评论(1

热风软妹 2024-11-06 14:57:35

尝试将 new UUID("1101", false); 替换为 new UUID(0x1101);

Try replacing new UUID("1101", false); with new UUID(0x1101);.

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