Printer connectivity issues on iOS 14 devices

Printer connectivity issues on iOS 14 devices

Printer connectivity issues on iOS 14 devices



Audience
Tags

There is a racing condition between the initialization of MfiBtPrinterConnection and ZebraPrinterFactory's getInstance that occurs on recent iOS devices.
        connection = [[[MfiBtPrinterConnection alloc] initWithSerialNumber:self.connectivityViewController.bluetoothPrinterLabel.text] autorelease];
        BOOL didOpen = [connection open];
        if(didOpen == YES) {
                // A VERY SHORT DELAY IS NEEDED HERE TO WORK
                NSError *error = nil;
                id printer = [ZebraPrinterFactory getInstance:connection error:&error];
        }
If we wait long enough the error will be "Unable to determine printer language"
The bug may not occur if there are NSLog or other code causing delays. Sometimes the connection seems to stop and a log like this appear:
        2020-10-29 15:13:37.733311-0400 ZSDKDeveloperDemos[2200:457464] CoreAccessories accessoryInfo for departure = {
                ACCExternalAccessoryPPIDKey = "";
                ACCExternalAccessoryPrimaryUUID = "232DDB75-5A99-4076-A1B2-0F58538A0884";
                ACCExternalAccessoryProtocolDetailsKey = {
                        "com.zebra.rawport" = {
                                ACCExternalAccessoryMatchAction = 2;
                                ACCExternalAccessoryProtocolEndpointUUID = "232DDB75-5A99-4076-A1B2-0F58538A0884";
                                ACCExternalAccessoryProtocolIndex = 90;
                                ACCExternalAccessoryProtocolType = 0;
                                ACCExternalAccessorySessionUsesSocketInterfaceKey = 1;
                        };
                };
                IAPAppAccessoryCapabilitiesKey = 1;
                IAPAppAccessoryDockTypeKey = "";
                IAPAppAccessoryFirmwareRevisionKey = 001;
                IAPAppAccessoryFirmwareRevisionPendingKey = "";
                IAPAppAccessoryHardwareRevisionKey = 001;
                IAPAppAccessoryMacAddressKey = "48:A4:93:4F:F7:DF";
                IAPAppAccessoryManufacturerKey = "Zebra Technologies";
                IAPAppAccessoryModelNumberKey = ZQ521;
                IAPAppAccessoryNameKey = XX_HID_SERIAL_NUMBER;
                IAPAppAccessoryProtocolsKey = {
                        "com.zebra.rawport" = 90;
                };
                IAPAppAccessorySerialNumberKey = XX_HID_SERIAL_NUMBER;
                IAPAppAccessoryTransportType = 2;
                IAPAppAccessoryVehicleInfoInitialDataKey = {
                };
                IAPAppConnectionIDKey = 49928821;
        }
== Workaround for the racing condition ==
Add a sleep:
        connection = [[[MfiBtPrinterConnection alloc] initWithSerialNumber:self.connectivityViewController.bluetoothPrinterLabel.text] autorelease];
        BOOL didOpen = [connection open];
        if(didOpen == YES) {
                [NSThread sleepForTimeInterval:0.01f]; // Very short sleep is needed otherwise the printer is not really ready on faster iOS devices
                NSError *error = nil;
                id printer = [ZebraPrinterFactory getInstance:connection error:&error];
        }
But it does not fix the problem, sometimes it will get stuck in                 id printer = [ZebraPrinterFactory getInstance:connection error:&error];.
== Printer ==
- Model: ZQ521
- Firmware: V91.21.05Z
- PDFDirect: v206
- Configurations:
        - apl.enable "pdf"
        - ezpl.media_type "continuous"
== SDK ==
SDK: iOS v1.5.1049
== iOS models ==
- iPhone 12 PRO iOS 14.1
- iPad Air 4 with iOS 14.1
== Steps to reproduce ==
1. Pair printer with iOS device.
2. Open iOS demo
3. In StatusViewController.mm comment lines 97 and 99. Ex:
‎‎        // [self.connectivityViewController setStatus:@"Connected..." withColor:[UIColor greenColor]];
        // [self.connectivityViewController setStatus:@"Determining Printer Language..." withColor:[UIColor yellowColor]];
4. Start app using debugger
5. Press "Start" button
6. Press "Printer Status"
7. Select "Bluetooth"
8. Select printer
9. Press "Check Status"
10. Thread get stuck at line "id printer = [ZebraPrinterFactory getInstance:connection error:&error];" for a while and fail after a while