蓝牙 - 在Android上同时从多个设备接收数据

时间:2015-04-18 14:54:58

标签: android bluetooth

我正在尝试同时从多个设备接收数据,我正在使用createInsecureRfcommSocketToServiceRecord()和SPP UUID 00001101-0000-1000-8000-00805F9B34FB连接到非Android设备。

所以我正在运行3个ConnectedThread实例,我可以写入所有设备,但我无法同时从2个设备接收。

示例:我正在使用HyperTerminal连接到2 Pc,如果我同时在两者上发送一个txt文件,我将只在我的Android设备上收到一个,另一个被忽略。

我正在寻找这个库:http://arissa34.github.io/Android-Multi-Bluetooth-Library/似乎我必须在我的Android手机上运行服务器。

我怎样才能做到这一点?

最好的问候。

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

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

}

0 个答案:

没有答案