0

객체 입력 스트림과 객체 출력 스트림을 처음 사용하지만 Bluetooth를 통해 문자열을 보내야합니다. 연결을 시도 할 때마다 전화가 모두 멈추고 충돌이 발생합니다. 에가 .flush 사용하여 수행 할 수 있습니다객체 입력/출력 스트림으로 인해 프로그램이 중단됨

private class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final ObjectInputStream mmInStream; 
     private final ObjectOutputStream mmOutStream; 
     private FileOutputStream mmFileOut = null; 


     public ConnectedThread(BluetoothSocket socket, String socketType) { 
      Log.d(TAG, "create ConnectedThread: " + socketType); 
      mmSocket = socket; 
      ObjectInputStream tmpIn = null; 
      ObjectOutputStream tmpOut = null; 

      // Get the BluetoothSocket input and output streams 

      try { 
       //input stream 
       //mmFileIn = new FileInputStream("t.tmp"); 
       tmpIn = new ObjectInputStream(socket.getInputStream()); 

       //output stream 
       mmFileOut = new FileOutputStream("t.tmp"); 
       tmpOut.flush(); 
       tmpOut = new ObjectOutputStream(mmFileOut); 
       tmpOut.writeObject(socket.getOutputStream()); 


      }catch (FileNotFoundException fnfe){ 
       System.out.println("FileOutPutStream: "+ fnfe); 
      }catch (IOException ie){ 
       System.out.print("ObjectOutputStream: " + ie); 
      }catch (Exception e){ 
       System.out.print(e); 
      } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 
     } 

     public void run() { 
      Log.i(TAG, "BEGIN mConnectedThread"); 
      byte[] buffer = new byte[1024]; 
      int bytes; 

      // Keep listening to the InputStream while connected 
      while (true) { 
       try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer); 

        // Send the obtained bytes to the UI Activity 
        mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer) 
          .sendToTarget(); 
       } catch (IOException e) { 
        Log.e(TAG, "disconnected", e); 
        connectionLost(); 
        // Start the service over to restart listening mode 
        BluetoothChatService.this.start(); 
        break; 
       } 
      } 
     } 

     /** 
     * Write to the connected OutStream. 
     * 
     * @param buffer The bytes to write 
     */ 
     public void write(byte[] buffer) { 
      try { 
       mmOutStream.write(buffer); 

       // Share the sent message back to the UI Activity 
       mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer) 
         .sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "Exception during write", e); 
      } 
     } 

     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { 
       Log.e(TAG, "close() of connect socket failed", e); 
      } 
     } 
    } 

어디 선가 읽고() : 여기

tmpIn = new ObjectInputStream(socket.getInputStream()); 내 연결 스레드 : 나는 디버거하고 프로그램 동결되기 전에에서 중지 마지막 줄을 사용 내 개체 출력 스트림을 올바르게 사용하고 있습니까?

답변

2

생성자의 코드는 UI 스레드에서 실행됩니다. 그것을()로 이동하십시오.

+0

내가 코드를 run()으로 옮겨야한다는 것이 맞습니다. 그러나 ObjectInput/OutputStream을 사용하는 대신이 작업이 끝났습니다. http://stackoverflow.com/questions/28074603/jsonstring-wont-convert-to-jsonarray – MasterProgrammer200

관련 문제