0

AndroidDeveloperのコードを参照しましたコードをコンパイルし、いくつかのエラーをいじくり回した後、私はこれを理解できませんでした。

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のメソッドgetMessage(int, int , object)は、引数には適用できません(String, int ,int, Byte[])

以前にこのコードを使用したことがある人が、広告が必要なものや不足しているものを教えてもらえますか?それはおそらく本当に単純なものです。ありがとうございました。

4

1 に答える 1

1

getMessageは次のような引数を受け入れます

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をIntに変更することです。MESSAGE_READ

于 2011-08-03T11:59:58.393 に答える