获取MAC地址(device_UUID):
InetAddress address = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(address);
byte[] macAddress = networkInterface.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int byteIndex = 0; byteIndex < macAddress.length; byteIndex++) {
sb.append(String.format("%02X", macAddress[byteIndex]));
if((byteIndex < macAddress.length - 1)){
sb.append("-");
}
}
从机器人初始化CMD,像的JSONObject:
mouseMove : {"cmd" : "mouseMove", "data" : [100, 200]}
mousePress : {"cmd" : "mousePress", "data" : [100]}
keyPress : {"cmd" : "keyPress", "data" : [100]}
创建连接之间的两个装置: 对于机器人:
BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID_PC);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(cmdJsonObject); // for example
socket.close();
对于PC:
LocalDevice localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service
String url = "btspp://localhost:" + device_UUID_Android + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);
StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();
String cmdJsonObject;
while (true) {
cmdJsonObject = dis.readUTF();
}
connection.close();
使用机器人执行命令来创建服务:
Robot robot = new Robot();
JsonObject jsonCMD = JsonObject(cmdJsonObject);
if(jsonCMD.containsKey("mouseMove")){
JsonArray jsonData = jsonCMD.getJsonArray("data");
int x = jsonData.getInt(0);
int y = jsonData.getInt(1);
robot.mouseMove(x, y);
}
我希望它可以帮助你!
感谢 @Victor黄:Send data from android bluetooth to PC with bluecove
@Faisal FEROZ:Moving the cursor in Java
谢谢您的答复。无论你的建议是我已经实施该解决方案,但我不想在计算机上运行任何其他应用程序,我的电脑应该检测Android设备作为无线鼠标,而不是将其检测为Android设备。 – Newbie
天啊!这是不可能的 – Dungnbhut
这是可能的与司机交互。 – Newbie