Closed mike1023 closed 2 years ago
I'm not sure if this has anything to do with the underlying library, but from a quick glance at the code you posted:
1) You're adding an event listener port.addDataListener()
but not really doing anything with it. Additionally, the serialEvent()
callback you have written has nothing to do with the event type you're listening for (LISTENING_EVENT_DATA_WRITTEN
). I would just remove this entire data listener. It probably doesn't make sense for your application.
2) You're setting a blocking write timeout which might be problematic when the underlying device is Bluetooth-based since the transmission time may be non-deterministic. Again, I don't think that this is necessary for your application, so I would remove it.
3) If you're sending a bunch of keys in quick succession, perhaps the underlying driver is combining them into a single Bluetooth packet and only the first key is getting recognized by the iPhone? If that's the case, you may need to wait for a confirmation status message from the first keypress (if your device sends such messages).
4) Barring all of the above, you may need to write some manual test cases to see if you can reproducibly figure out what works and what doesn't (for example, write one long message string at one time that contains 2-3 commands inside of it, write 2-3 commands in a row in quick succession, etc.).
hello, thanks for your response, i have removed addDataListener()
and blocking write timeout
.
for third point you mentioned, if i send a bunch of keys in quick succession, it is worked sometimes, if the driver only recognized the first key, it should never worked.
and, how can i get the status from my device whether it have received all message?
i write a loop to send a single key press, it works well.
//0C00A10100005000000000000C00A1010000000000000000 means press down "right arrow" and then lift up it.
for (int i = 0; i < 20; i++) {
byte[] data = hexStringToByteArray("0C00A10100005000000000000C00A1010000000000000000");
out.write(data);
out.flush();
}
@hedgecrw i found the root cause, i separate the command to several parts, and after send one, then i make program sleep for 200ms.
command = "0C00A1010000390000000000 0C00A1010000392C00000000 0C00A1010000390000000000 0C00A1010000000000000000";
String[] strArr = command.split(" ");
ArrayList byteArr = new ArrayList();
for (int i = 0; i < strArr.length; i++) {
String s = strArr[i];
byte[] data = hexStringToByteArray(s);
byteArr.add(data);
}
for (int i = 0; i < byteArr.size(); i++) {
out.write((byte[]) byteArr.get(i));
out.flush();
Thread.sleep(200);
}
i think maybe bluetooth data need a delay.
i buy a bluetooth device with HID module, i use it to simulate a bluetooth keyboard, now i want to use it to control my iPhone with VoiceOver. it needs to send some key message. such as "->" to select next item, "<-" to select preview item, "CapsLock + SpaceBar" to activate an item.
now, if i send a single key, it works well, such as "<-" or "->", but if i send two or more key, it is unstable, sometimes work, sometimes not work.
here is some useful link: https://support.apple.com/en-au/guide/iphone/iph6c494dc6/ios
OS system: macOS Monterey 12.4(Intel) Java SDK: 1.8 jSerialComm:2.9.2