2014-12-05 2 views
0

inLoop() 메서드를 while 루프에 넣으면 서버가 실행되지 않습니다.루프에서 OutputStream에 바이트를 쓰는 동안 스트림을 다시 열 수 없습니다.

종료되지만 서버가 계속 클라이언트에 계속해서 바이트를 보내길 원합니다.

어떻게하면됩니까?

SimpleSPPServer -

public class SimpleSPPServer { 
    //start server 
    private void startServer() throws IOException{ 

     //Create a UUID for SPP 
     UUID uuid = new UUID("1101", true); 
     //Create the servicve url 

     String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server"; 


     //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(); 

     while(true) 
     { 
      inLoop(connection); 
     } 
    } 

    public void inLoop(StreamConnection connection) throws IOException 
    { 
     RemoteDevice dev = RemoteDevice.getRemoteDevice(connection); 
     System.out.println("Remote device address: "+dev.getBluetoothAddress()); 
     System.out.println("Remote device name: "+dev.getFriendlyName(true)); 

     //send response to spp client 
     OutputStream outStream=connection.openDataOutputStream(); 

     DataOutputStream mmOutStream = new DataOutputStream(outStream); 

     byte[] bb1 = new byte[] { 
       (byte)0x7D, (byte)0x43, (byte)0x92, (byte)0x05, (byte)0x0E, 
       (byte)0x2E, (byte)0x0A }; 

     System.out.println("Writing Bytes..."); 

      try 
      { 
       //mmOutStream.writeByte(bb[k]); 
       mmOutStream.write(bb1, 0, bb1.length); 

       Thread.sleep(5000); 

       //mmOutStream.write(bb2, 0, bb2.length); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      catch (NullPointerException e) 
      { 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(mmOutStream)); 
     try { 
      mmOutStream.flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     pWriter.flush(); 

     pWriter.close(); 

    } 

    public static void main(String[] args) throws IOException { 

     //display local device address and name 
     LocalDevice localDevice = LocalDevice.getLocalDevice(); 
     System.out.println("Address: "+localDevice.getBluetoothAddress()); 
     System.out.println("Name: "+localDevice.getFriendlyName()); 

     SimpleSPPServer sampleSPPServer=new SimpleSPPServer(); 
     sampleSPPServer.startServer(); 

    } 

예외 -

Exception in thread "main" java.io.IOException: Stream cannot be reopened 
    at com.intel.bluetooth.BluetoothRFCommConnection.openOutputStream(BluetoothRFCommConnection.java:190) 
    at com.intel.bluetooth.BluetoothRFCommConnection.openDataOutputStream(BluetoothRFCommConnection.java:213) 
    at SimpleSPPServer.inLoop(SimpleSPPServer.java:52) 
    at SimpleSPPServer.startServer(SimpleSPPServer.java:41) 
    at SimpleSPPServer.main(SimpleSPPServer.java:104) 
+0

스트림을 다시 열 수없는 경우 왜 그렇게할까요? – greenapps

+0

@greenapps -'스트림을 다시 열 수 없으면 왜 그렇게할까요? '- 각 반복마다 같은 바이트를 처리하고 싶습니다. 그래서 저는 그렇게하고 있습니다. 다시 열지 않고 루프에서 바이트를 계속 처리하도록하십시오. –

+0

'동일한 바이트'를 두 번 이상 처리하는 이유는 무엇입니까? – greenapps

답변

0

doLoop() 중에서 스트림의 개폐를 가지고. while 루프를 시작하기 전에 스트림을 엽니 다. 결코 스트림을 닫지 마십시오. 또는 루프가 완료되었을 때.

관련 문제