2014-12-05 76 views
0

當我在while循環中放入inLoop()方法時,服務器從不運行。流在循環中寫入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 2014-12-05 08:06:50

+0

@greenapps - '如果流不能重新打開,那你爲什麼要這麼做?' - 我想在每次迭代中處理相同的字節。所以我就是這樣做的。請以任何方式告訴我們如何在循環中連續處理字節而不會重新打開錯誤。 – 2014-12-05 08:26:34

+0

爲什麼不止一次處理'相同的字節'? – greenapps 2014-12-05 08:49:34

回答

0

採取流出來的doLoop()的打開和關閉。在while開始循環之前打開流。從不關閉流。或者當循環完成時。