在我的Android应用程序中使用read方法

时间:2012-12-03 18:35:45

标签: android multithreading bluetooth

所以我创建了一个我有GUI类的应用程序。此应用程序将侦听来自激活togglebutton时创建的另一个应用程序的2个传入字符串。我简直无法理解的是如何读取传入的字节并将它们通过我的代码传递回我们在GUI类中使用的位置。希望任何人都能提供帮助。

当我发送我的两个字符串时,我得到两个字节,然后发送第一个,获取“%”字符串的字节,将其作为分隔符发送,然后发送第二个字符串。

public void ListenForAddress(View view)
{
    on = ((ToggleButton) view).isChecked();

    if(on)
    {
        Address address = reciever.RecieveObject();
        Intent intent = new Intent(this, Screen3.class);
        String adressStr = address.Address;
        intent.putExtra("ADRESS_MESSAGE", adressStr);
        String postalcodeStr = address.Postalcode;
        intent.putExtra("POSTALCODE_MESSAGE", postalcodeStr);
        intent.putExtra("ONE", 1);
        startActivity(intent);
    }
    else
    {
        reciever.closeReception();
    }   
}

正如您所看到的,我创建了一个接收器,并且我调用了一个名为RecieveObject()的方法,该方法如下所示。

public Address RecieveObject() 
{
    accThread = new AcceptThread();
    accThread.start();
    return null;
}

下一步是创建连接并启动管理线程的线程

public class AcceptThread extends Thread {


public AcceptThread() {
    BluetoothServerSocket tmp = null;
    try {
    tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Server", MY_UUID);
    } catch (IOException e) { }
    mmServerSocket = tmp;
}

public void run() {
    BluetoothSocket socket = null;
    while (true) {
        try {
            socket = mmServerSocket.accept();
        } catch (IOException e) {
            break;
        }
        // If a connection was accepted
        if (socket != null) {
            mConnSock = new manageConnectedSocket(socket);
            mConnSock.read();
            try {
                mmServerSocket.close();
            } catch (IOException e) {
            }
            break;
        }
    }
}
}

最后一步是处理数据的线程

public class manageConnectedSocket extends Thread {

public manageConnectedSocket(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;

    try {
        tmpIn = socket.getInputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
}

public Address read() {
    byte[] buffer = new byte[1024];
    int bytes; 
    Address address = new Address("", "");
    int count = 0;

    while(count<2)
    {
        try {
            bytes = mmInStream.read(buffer);

            if(count==0)
            {
                address.Address = new String(buffer);
            }
            else
            {
                address.Postalcode = new String(buffer);
            }
            buffer = new byte[1024];

        } catch (IOException e) {
            break;
        }
    }
    return address;
}

}

1 个答案:

答案 0 :(得分:1)

发送地址和邮政编码时,请在它们之间发送分隔符。它可以是任何在地址和邮政编码中都不会出现的字符,或者甚至是一系列字符,都是非常安全的。例如:!@#@!您应该通过一次调用将数据发送到write

所以你以这种方式发送地址:

String data = address + "!@#@!" + postalCode;
byte[] bytes = data.getBytes();
socket.getOutputStream().write(bytes);

读取地址时,不应该调用检索地址的方法,因为它会阻塞你的线程。而是在地址准备好时从连接线程调用方法。以这种方式启动therad:

manageConnectedSocket(socket).start();

在它的run方法中,阅读字符串:

public void run() {
    byte[] data = new byte[1024]; //You can make this array larger, if you think it won't have enough space to contain some addresses.
    socket.getInputStream().read(data); //This may block the thread, if no data is currently available.
    String strings = new String(data);
    int seperatorIndex = strings.indexOf("!@#@!");
    String addressStr = strings.substring(0, seperatorIndex);
    String postalCode = strings.substring(seperatorIndex + 5); //5 is the length of "!@#@!".
    Address address = new Address(addressStr, postalCode);
    makeUseOfAddress(address); //Show it to the user, for example. Remember it's called from this thread, so if you want to interact with your UI, you have to run the interacting code on the UI thread and not here.
}