我想从Android设备中的应用程序连接到远程设备(配对).远程设备是HC-05模块.
我的代码是:
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
try {
mSocket = MyDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
Toast.makeText(this, "S", Toast.LENGTH_SHORT).show();
}
ba.cancelDiscovery();
try {
mSocket.connect();
} catch (IOException e){
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Log.e("YOUR_APP_LOG_TAG1", "I got an error", e);
}
try {
mOutputStream = mSocket.getOutputStream();
mInputStream = mSocket.getInputStream();
} catch (IOException e) {
Toast.makeText(this, "io", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
但我在行mSocket.connect()中遇到错误.
错误:
读取失败的套接字可能会关闭或超时读取ret
请帮忙.
我有两个功能。我如何同时运行两个函数?我知道应该使用线程。我需要一个多线程示例。我正在使用 Visual Studio 2010