获取 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("-");
}
}
从android初始化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();
对于电脑:
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 Wong:Send data from android bluetooth to PC with bluecove
@Faisal Feroz:Moving the cursor in Java