獲取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