2017-10-12 221 views
0

我想讓我的android設備充當電腦或任何其他使用藍牙隱藏或任何配置文件的設備的輸入設備。如何在android中使用藍牙HID配置文件?

只要我通過藍牙連接客戶端設備,我應該可以使用我的android設備作爲鼠標或鍵盤,就像無線鍵盤或鼠標一樣。

經過大量的研究,我才知道android不支持HID配置文件,所以我如何才能做到這一點,有沒有辦法做到這一點,我有任何幫助的根源設備將不勝感激。

編輯:電腦或任何其他應該檢測到Android設備作爲無線鼠標,而不是檢測到它爲Android設備,以便我不需要安裝任何其他應用程序控制設備的一面。

謝謝。

回答

0

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

+0

謝謝您的答覆。無論你的建議是我已經實施該解決方案,但我不想在計算機上運行任何其他應用程序,我的電腦應該檢測Android設備作爲無線鼠標,而不是將其檢測爲Android設備。 – Newbie

+0

天啊!這是不可能的 – Dungnbhut

+0

這是可能的與司機交互。 – Newbie