2013-08-20 166 views
0

接收數據我是新到Android。我的應用程序正在使用藍牙與嵌入式主板進行通話。 我正在使用android藍牙聊天示例來打開藍牙套接字並啓動線程。無法通過藍牙

private class ConnectedThread extends Thread 
{ 
    public ConnectedThread(BluetoothSocket socket) 
    { 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the input and output streams, using temp objects because 
     // member streams are final 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     byte[] buffer ; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
       // Read from the InputStream 
       buffer = new byte[1024]; 
       bytes = mmInStream.read(buffer); 
       Log.d("MR", "input stream :"+(new String(buffer))); 
       // Send the obtained bytes to the UI activity 
       mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    /* Call this from the main activity to send data to the remote device */ 
    public void write(byte[] bytes) { 
     try { 
      //a delay of 20ms occurs after each flush... 
      mmOutStream.write(bytes); 
      mmOutStream.flush(); 
     } catch (IOException e) { } 
    } 

    /* Call this from the main activity to shutdown the connection */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { } 
    } 
} 

,我有一個消息處理器

Handler mHandler = new Handler(){ 
    @Override 
    public void handleMessage(Message msg) 
    { 
     // TODO Auto-generated method stub 
     Log.i(tag, "in handler"); 
     super.handleMessage(msg);  
     switch(msg.what){ 
     case SUCCESS_CONNECT: 
      // DO something 
      ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj); 
      //Toast.makeText(getApplicationContext(), "CONNECT", 0).show(); 
      String s = "successfully connected"; 
      //connectedThread.write(s.getBytes()); 
      connectedThread.write(s.getBytes()); 
      Log.i(tag, "connected"); 
      break; 
     case MESSAGE_READ: 
      byte[] readBuf = (byte[])msg.obj; 
      //String string = new String(readBuf, 0, msg.arg1); 
      Toast.makeText(getApplicationContext(), "Test", 0).show(); 
      // Create the text view 
      //TextView textView = (TextView)findViewById(R.id.rcvedMsg); 
      //textView.setTextSize(40); 
      //textView.setText(string);  
      break; 
     case RECIEVE_MESSAGE: 
      byte[] readmsgBuf = (byte[])msg.obj; 
      String string = new String(readmsgBuf, 0, msg.arg1); 
      Toast.makeText(getApplicationContext(), "Test", 0).show(); 
      // Create the text view 
      //TextView textView = (TextView)findViewById(R.id.rcvedMsg); 
      //textView.setTextSize(40); 
      //textView.setText(string);  
      break;    
     } 
    } 

我無法接收任何數據從嵌入式設備背面。嵌入式設備正在運行rfcomm服務器,它可以從我的android應用程序接收數據。在連接時,服務器肯定會發送數據。

回答

0

你需要使用,

connectedThread.start(); 
在SUCCESS_CONNECT