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

时间:2011-04-19 18:46:34

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

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

1 个答案:

答案 0 :(得分:0)

这是一个标准问题。当StreamConnection连接= 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类运行方法中,您可以获取流并进行通信。