0

我引用了Android Developer 的代码。在编译了代码并解决了一些错误之后,我无法弄清楚这一点。

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) { }    


    }
}

Handler 类型中的方法 gainMessage(int, int , object)不适用于 arguments (String, int ,int, Byte[])

以前使用过此代码的人能否告诉我我需要做哪些广告或我缺少什么。它可能非常简单。谢谢你。

4

1 回答 1

1

gainMessage 接受参数,如

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

你有传递MESSAGE_READ变量 handler.obtainMessage() 所以它看起来像

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

MESSAGE_READ这是 String ,这就是为什么你得到这个错误 MESSAGE_READ变量 String 更改为 Int

于 2011-08-03T11:59:58.393 回答