Bluetooth-код для разработчиков Android - PullRequest
0 голосов
/ 25 июля 2011

Я ссылался на код от 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) { }    


    }
}

Метод getMessage (int, int , object) в типе Handler не применим для аргументов (String, int ,int, Byte[]).

Может ли кто-нибудь, кто использовал этот код раньше, сказать мне, что мне нужно рекламировать или чего мне не хватает.Это, наверное, что-то действительно простое.Спасибо.

1 Ответ

1 голос
/ 03 августа 2011

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, который является строкой, и именно поэтому вы получаете эту ошибку изменить MESSAGE_READ переменная строка на Int

...