【发布时间】:2018-06-21 10:36:11
【问题描述】:
我正在从事一个项目,该项目涉及从 android 手机向运行 32 位 windows VM (VMWare Fusion) 的笔记本电脑发送字符串。在对如何做这样的事情进行了一些搜索之后,我让客户端(电话)工作,但是对于服务器(笔记本电脑)端,它似乎从来没有收到任何东西。
我正在与虚拟机共享蓝牙,并且设备已与主机配对,但是当我发送数据时它连接到主机,但运行“服务器”的虚拟机似乎从未收到它。
我从here 获得了接收器的代码,但只是为了澄清,这是我用于接收器/服务器/VM 的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class server {
// TODO: Update this to use the window, instead of the console
public void startServer() throws IOException {
// Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//UUID uuid = new UUID("00001101-0000-1000-8000-00805F9B34FB", false);
// Create the servicve url
String connectionString = "btspp://localhost:" + uuid + ";name=SpudSPPServer";
// open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString);
// Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect…");
StreamConnection connection = streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: " + dev.getBluetoothAddress());
System.out.println("Remote device name: " + dev.getFriendlyName(true));
// read string from spp client
InputStream inStream = connection.openInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inStream));
String lineRead = bReader.readLine();
System.out.println(lineRead);
// send response to spp client
OutputStream outStream = connection.openOutputStream();
PrintWriter pWriter = new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("Response String from SPP Server\r\n");
pWriter.flush();
pWriter.close();
streamConnNotifier.close();
}
}
&在主文件中:
public static void main(String args[]) throws IOException {
display d = new display();
server blueToothServer = new server();
d.makePanel();
if (selectFile.fileMissing()) {
d.feed.setText("File missing, creating new file");
selectFile.makeFile();
d.feed.setText("File created! Awaiting data...");
} else {
d.feed.setText("File found! Awaiting data...");
d.MACAddress.setText("MAC Address: " + LocalDevice.getLocalDevice().getBluetoothAddress().replace("-", ":").toUpperCase());
}
blueToothServer.startServer();
}
如果您需要客户(电话)的代码,如果需要,我可以发布
【问题讨论】:
-
仅用于 mote 信息:当它尝试发送字符串时:它要么什么都不做,要么抛出一个损坏的管道错误
标签: java android bluetooth bluecove