iOS key code capture from external bluetooth scanner

时光总嘲笑我的痴心妄想 提交于 2019-12-05 07:08:49

问题


I'm trying to scan a 2D barcode in iOS that contains non-printable characters. I have a multiple scanners that I would like to support. When connected via Serial Port Profile (SPP) using an SDK I can read all of that data just fine. One of the devices I would like to support only has Human Interface Device (HID) support (external keyboard).

When I use the scanner in HID mode to populate a UITextField the unprintable characters are stripped out. I've connected the device to my laptop and used a key code capturing device to see that the data is actually being sent.

Is there a way to populate a UITextField with non-printable characters that come from a bluetooth device connected as a HID?


回答1:


I found out how to receive non-printable key codes from a bluetooth device connected to iOS in HID mode.

For reference, a 2D barcode has the general format:

[)><RS>'01'<GS>'9612345'<GS>'111'<GS>'000'<GS>'012345678901234'<GS>'FDEB'<GS><GS><GS><GS><GS>'25'<GS>'Y'<GS>'123 1ST AVE'<GS>'SEATTLE'<GS>'WA'<RS><EOT>

Where <RS> is char(30) or sequence ctrl-^, <GS> is char(29) or sequence ctrl-], and <EOT> is char(4) or ctrl-d, which are ASCII control codes.

In iOS 7 and above you can capture Key Down events from a HID bluetooth device using UIKeyCommand. UIKeyCommand is intended for capturing things like Command-A from a bluetooth keyboard, but it can also be used to map ASCII Sequences. The trick is to map the key code sequence as opposed to the ASCII code. For example in your view controller you can:

- (NSArray *) keyCommands {
    // <RS> - char(30): ctrl-shift-6 (or ctrl-^)
    UIKeyCommand *rsCommand = [UIKeyCommand keyCommandWithInput:@"6" modifierFlags:UIKeyModifierShift|UIKeyModifierControl action:@selector(rsKey:)];
    // <GS> - char(29): ctrl-]
    UIKeyCommand *gsCommand = [UIKeyCommand keyCommandWithInput:@"]" modifierFlags:UIKeyModifierControl action:@selector(gsKey:)];
    // <EOT> - char(4): ctrl-d
    UIKeyCommand *eotCommand = [UIKeyCommand keyCommandWithInput:@"D" modifierFlags:UIKeyModifierControl action:@selector(eotKey:)];
    return [[NSArray alloc] initWithObjects:rsCommand, gsCommand, eotCommand, nil];
}

- (void) rsKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<RS> character received");
}

- (void) gsKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<GS> character received");
}

- (void) eotKey: (UIKeyCommand *) keyCommand {
    NSLog(@"<EOT> character received");
}

I hope this helps.



来源:https://stackoverflow.com/questions/25086597/ios-key-code-capture-from-external-bluetooth-scanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!