通过蓝牙从 android 设备向笔记本电脑发送字符串
Sending string from android device to laptop via bluetooth
我正在从事一个项目,该项目涉及将字符串从 android phone 发送到我的笔记本电脑 运行 32 位 windows VM (VMWare Fusion)。在对如何做这样的事情进行了一些搜索之后,我让客户端 (phone) 开始工作,但是对于服务器(笔记本电脑)端,它似乎从来没有收到任何东西。
我正在与 VM 共享蓝牙,并且设备已与主机配对,但当我发送数据时它会连接到主机,但 VM 运行 "server" 似乎从来没有收到。
我从 here 那里得到了接收器的代码,但只是为了澄清,这里是我用于 receiver/server/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();
}
如果您需要客户端的代码 (phone),我可以 post 如果被问到
搞清楚了,原来是在 VM 端,所以我只是正常安装 windows 而不是 VM,然后修复了它
我正在从事一个项目,该项目涉及将字符串从 android phone 发送到我的笔记本电脑 运行 32 位 windows VM (VMWare Fusion)。在对如何做这样的事情进行了一些搜索之后,我让客户端 (phone) 开始工作,但是对于服务器(笔记本电脑)端,它似乎从来没有收到任何东西。
我正在与 VM 共享蓝牙,并且设备已与主机配对,但当我发送数据时它会连接到主机,但 VM 运行 "server" 似乎从来没有收到。
我从 here 那里得到了接收器的代码,但只是为了澄清,这里是我用于 receiver/server/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();
}
如果您需要客户端的代码 (phone),我可以 post 如果被问到
搞清楚了,原来是在 VM 端,所以我只是正常安装 windows 而不是 VM,然后修复了它