java-me - 如何在 j2me 中使用蓝牙 API 制作多客户端服务器?

标签 java-me bluetooth

我计划使用 J2ME 使用蓝牙 API 实现一个服务器。我希望多个客户端能够同时连接到它,但我在 NET 上找不到太多。

 UUID uuid = new UUID("1101", false);
    String SurveyAnswer="";
    //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...");
   while(true){
     StreamConnection connection = streamConnNotifier.acceptAndOpen();    
    }

如何修改这些代码以使其作为多客户端服务器工作?

最佳答案

这是一个标准问题。当 StreamConnection connection = streamConnNotifier.acceptAndOpen();返回你必须产生一个使用连接的线程。主线程然后重新进入接受并等待下一个连接。

 UUID uuid = new UUID("1101", false);     String SurveyAnswer="";     
 //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...");
 while(true){
   StreamConnection connection = streamConnNotifier.acceptAndOpen();      
   System.out.println("Client connected starting communication");
   new CommunicationThread(connection).start();   
 } 

在类 CommunicationThreads run 方法中,您可以获取流并进行通信。

关于java-me - 如何在 j2me 中使用蓝牙 API 制作多客户端服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721133/

相关文章:

java - 什么是开发黑莓应用程序的好工具包?

ios - 通过蓝牙 ios 路由音频

c - Ubuntu 上的 C 蓝牙编程

java-me - 如何停止j2me的midlet的执行?

java - Java/J2me中常用的时区格式

java - 从外部中间件获取 RecordStore 数据

http - (HttpConnection) Connector.open(url) 与触摸电话

android - listenUsingRfcommWithServiceRecord(...) 返回 null

python - Python套接字错误52(无效交换)指的是什么?

iphone - 有没有办法让 LilyPad Arduino 与 iOS 设备进行通信?