0

所以我创建了一个应用程序,其中有一个 GUI 类。此应用程序将在激活切换按钮时侦听来自我创建的另一个应用程序的 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;
}

}
4

1 回答 1

2

发送地址和邮政编码时,请在它们之间发送分隔符。它可以是任何既不会出现在地址也不会出现在邮政编码中的字符,甚至是字符序列,以更加安全。例如:!@#@!您应该一次调用将数据发送到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.
}
于 2012-12-03T19:24:08.290 回答