2016-11-19 1 views
2

저는 안드로이드 장치 (클라이언트)와 통신하는 간단한 자바 서버를 만들고 있습니다. 현재 내 전화 (클라이언트)에서 블루투스를 통해 내 PC (서버)로 메시지를 보낼 수 있습니다. 문제는 내가 서버에서 다시 클라이언트으로 메시지를 보낼 수 없다는 것입니다. 나는 블루 케이브 라이브러리를 사용하고 있습니다. 여기 여전히 내 나는 또한 PrintWriter 업데이트 된 코드를 사용하려고자바 블루투스 서버가 클라이언트에게 메시지를 다시 보내십시오.

public class MainTest { 
    UUID uuid = new UUID("8848",true); 
    public static void main(String[] args) { 
     LocalDevice local = null; 
     try { 
      local = LocalDevice.getLocalDevice(); 
     } catch (BluetoothStateException e) { 
      e.printStackTrace(); 
     } 
     System.out.println("Serverted:\n" +local.getBluetoothAddress() +"\n"+local.getFriendlyName()); 
     MainTest ff = new MainTest(); 
     while (true) { 
      ff.startserver(); 
     } 
    } 

    public void startserver() { 
     try { 
      String url = "btspp://localhost:" + uuid + ";name=File Server"; 
      StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open(url); 

      StreamConnection con = service.acceptAndOpen(); 
      OutputStream dos = con.openOutputStream(); 
      InputStream dis = con.openInputStream(); 

      while (true) { 
       byte buffer[] = new byte[1024]; 
       int bytes_read = dis.read(buffer); 
       String received = new String(buffer, 0, bytes_read); 
       System.out.println("Message:"+ received); 

       if("a".equals(received)) { 
        dos.write("sdfsd".getBytes()); 
        dos.flush(); 
       } 
      } 
      // con.close(); 
     } catch (IOException e) { 
      System.err.print(e.toString()); 
     } 
    } 

코드,하지만 응답 ...

public static void startserver() { 
     try { 
      String url = "btspp://localhost:" + uuid + ";name=TTT"; 
      StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open(url); 

      StreamConnection con = service.acceptAndOpen(); 
      DataOutputStream dos = con.openDataOutputStream(); 
      InputStream dis = con.openInputStream(); 
      PrintWriter pWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(dos)), true); 
      while (true) { 
       byte buffer[] = new byte[10]; 
       int bytes_read = dis.read(buffer); 
       String received = new String(buffer, 0, bytes_read); 
       System.out.println("Message:"+ received); 

       pWriter.write("testString"); 
       pWriter.flush(); 

      } 
      // pWriter.close(); 
      // con.close(); 


      // con.close(); 
     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
+0

[Java 서버에서 Android 클라이언트로 블루투스를 통해 텍스트 보내기] 가능한 복제본 (http://stackoverflow.com/questions/10929767/send-text-through-bluetooth-from-java-server-to-android-client)) –

답변

관련 문제