2011-07-25 61 views
0

我引用了Android Developer.的代碼編譯完代碼並且弄錯了一些錯誤之後,我無法弄清楚這一點。藍牙Android開發者代碼

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.bluetooth.BluetoothSocket; 
import android.os.Handler; 
import junit.framework.TestCase; 

public class ConnectedThread extends Thread {  
    private static final String MESSAGE_READ = null; 
    private final BluetoothSocket mmSocket;  
    private final InputStream mmInStream;  
    private final OutputStream mmOutStream; 
    private Handler mHandler = new Handler(); 
    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 = new byte[1024]; 
     // 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     
       bytes = mmInStream.read(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 {    
      mmOutStream.write(bytes);   
      } 
     catch (IOException e) { }  
      }  
    /* Call this from the main Activity to shutdown the connection */  
    public void cancel() {   
     try {    
      mmSocket.close();   
      } catch (IOException e) { }  


    } 
} 

在類型處理程序的方法obtainMessage (int, int , object)不適用的參數(String, int ,int, Byte[])

可能有人在使用此代碼之前可能會告訴我我需要什麼廣告或缺少什麼。它可能很簡單。謝謝。

回答

1

handler.obtainMessage(int) 
handler.obtainMessage(int, object) 
handler.obtainMessage(int, int, int) 
handler.obtainMessage(int, int, int, object); 

你有obtainMessage接受變元的傳入handler.obtainMessage()MESSAGE_READ變量,所以它看起來像

handler.obtainMessage(String, int, int, object); 

MESSAGE_READ這是字符串而這就是爲什麼你收到此錯誤 變化MESSAGE_READ變量String to Int