Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android蓝牙发送文本_Android_Bluetooth - Fatal编程技术网

Android蓝牙发送文本

Android蓝牙发送文本,android,bluetooth,Android,Bluetooth,我在从android向PC发送消息时遇到问题。当我退出android应用程序或关闭套接字时,会发送消息。我可以毫无问题地从PC向android发送消息。我应该总是关闭连接,然后再连接吗 public boolean sendData(String msg) { if (socket != null) { try { socket.getOutputStream().write(msg.getBytes()); socket.

我在从android向PC发送消息时遇到问题。当我退出android应用程序或关闭套接字时,会发送消息。我可以毫无问题地从PC向android发送消息。我应该总是关闭连接,然后再连接吗

public boolean sendData(String msg) {
    if (socket != null) {
        try {
            socket.getOutputStream().write(msg.getBytes());
            socket.getOutputStream().flush();
            //socket.getOutputStream().close();
            //socket.close();
            return true;
        } catch (IOException e) {
            socket = null;
            return false;
        }
    } else {
        return false;
    }
}

问题解决了。这是个人电脑应用的问题。应用程序正在等待换行(\r\n)。

您能在这里发布android和pc代码吗?我也试着这么做,但我对这件事不太了解。多谢