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?
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.