Как сделать многопользовательский сервер с использованием Bluetooth API в j2me? - PullRequest
3 голосов
/ 19 апреля 2011

Я планирую внедрить сервер, используя Bluetooth API, используя J2ME. Я хочу, чтобы несколько клиентов могли подключаться к нему одновременно, но я не мог найти много в сети.

 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 голосов
/ 10 августа 2011

Это стандартная проблема.Когда соединение 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 вы можете получить потоки и установить связь.

...