Skip to content

Monitoring Connection State

adonitRDSW edited this page Dec 11, 2017 · 2 revisions

An application can always discover the current connection state of the SDK by using the connectionStatus property on the JotStylusManager.

JotConnectionStatus currentStatus = [[JotStylusManager sharedInstance] connectionStatus];

You can also be notified of changes to the connection status by adding an observer for the JotStylusManagerDidChangeConnectionStatus notification on the default NSNotificationCenter.

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionChange:)
                                                 name:JotStylusManagerDidChangeConnectionStatus object:nil];

One important thing to note is that when you receive one of these notifications, you should use the JotStylusManagerDidChangeConnectionStatusStatusKey to get the connection state associated with the notification. This may differ from the JotStylusManager’s connectionStatus, especially if the connection state is changing very rapidly.

The example code below sets the title of a button based on a connection status notification.

- (void)connectionChange:(NSNotification *)note
{
    NSNumber *statusNumber = note.userInfo[JotStylusManagerDidChangeConnectionStatusStatusKey];
    
    switch([statusNumber unsignedIntegerValue]) {
        case JotConnectionStatusOff:
            [settingsButton setTitle:@"Off" forState:UIControlStateNormal];
        case JotConnectionStatusScanning:
            [settingsButton setTitle:@"Scanning" forState:UIControlStateNormal];
        case JotConnectionStatusPairing:
            [settingsButton setTitle:@"Pairing" forState:UIControlStateNormal];
        case JotConnectionStatusConnected:
            [settingsButton setTitle:@"Connected" forState:UIControlStateNormal];
        case JotConnectionStatusDisconnected:
            [settingsButton setTitle:@"Disconnected" forState:UIControlStateNormal];
        case JotConnectionStatusStylusNotSupportThePlatform:
        case JotConnectionStatusSwapStylusNotSupportThePlatform:
            show message @"This stylus is compatible with iPad Pro only."
        default:
            [settingsButton setTitle:@"" forState:UIControlStateNormal];
    }
}

Also consider using the Connection Status View Controller which automatically monitors and displays the current connection status as well as provides a launching point for connecting and configuring Jots.

Connection Unexpected State Handling

When pixel connected to iPad pro series, SDK will send a recommend string notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showRecommendString:) 
                                                       name:JotStylusNotificationRecommend object:nil];

- (void)showRecommendString:(NSNotification *)note
{
    NSString *recommend = note.userInfo[JotStylusNotificationRecommendKey];
    [JotTouchStatusHUD showJotHUDInView:self.view topLineMessage:recommend bottomeLineMessage:@""];
}