Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether Apple Pencil is connected to an iPad Pro

Is there an API that allows you to determine whether the Apple Pencil is connected to an iPad Pro? Looking over the 9.1 SDK I don't see anything that directly does this. Or perhaps this can be done using the Bluetooth API.

like image 282
combinatorial Avatar asked Sep 12 '15 18:09

combinatorial


People also ask

How do I know if my Apple Pencil is connected to my iPad?

Go to Settings > Bluetooth and make sure that Bluetooth is turned on. On the same screen, look under My Devices for your Apple Pencil. If you see it, tap .

How do I check Apple Pencil status?

When you attach an Apple Pencil (2nd generation) to your iPad, you see the charge status on the screen for a moment. To see how much charge your Apple Pencil (1st or 2nd generation) has left while you're using it, check the Today View on your iPad. Just swipe from left to right on the Home screen or Lock screen.

Does Apple Pencil automatically connect?

The Pencil ought to automatically reconnect after the iPad or the Bluetooth module is restarted. It doesn't. Hopefully a future iteration of the Apple Pencil, perhaps using a W series chip, will correct this. If you have Airplane Mode enabled, that will un pair the pencil.


1 Answers

I can't find any actual documentation on the Apple Pencil's Bluetooth implementation (and I don't believe any exists), but the following code Works for Me™.

It checks for connected devices that advertise themselves as supporting the "Device Information" service and then if any of these have the name "Apple Pencil".

PencilDetector.h

@import CoreBluetooth

@interface PencilDetector : NSObject <CBCentralManagerDelegate>

- (instancetype)init;

@end

PencilDetector.m

#include "PencilDetector.h"

@interface PencilDetector ()

@end

@implementation PencilDetector
{
  CBCentralManager* m_centralManager;
}

- (instancetype)init
{
  self = [super init];
  if (self != nil) {
    // Save a reference to the central manager. Without doing this, we never get
    // the call to centralManagerDidUpdateState method.
    m_centralManager = [[CBCentralManager alloc] initWithDelegate:self
                                                            queue:nil
                                                          options:nil];
  }

  return self;
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
  if ([central state] == CBCentralManagerStatePoweredOn)
  {
    // Device information UUID
    NSArray* myArray = [NSArray arrayWithObject:[CBUUID UUIDWithString:@"180A"]];

    NSArray* peripherals =
      [m_centralManager retrieveConnectedPeripheralsWithServices:myArray];
    for (CBPeripheral* peripheral in peripherals)
    {
        if ([[peripheral name] isEqualToString:@"Apple Pencil"])
        {
            // The Apple pencil is connected
        }
    }
  }
}

@end

In practice, the following, simpler, synchronous code, which doesn't wait for the central manager to be powered on before checking for connected devices seems to work just as well in my testing. However, the documentation states that you shouldn't call any methods on the manager until the state has updated to be CBCentralManagerStatePoweredOn, so the longer code is probably safer.

Anywhere you like

m_centralManager = [[CBCentralManager alloc] initWithDelegate:nil
                                                        queue:nil
                                                      options:nil];

// Device information UUID
NSArray* myArray = [NSArray arrayWithObject:[CBUUID UUIDWithString:@"180A"]];

NSArray* peripherals =
  [m_centralManager retrieveConnectedPeripheralsWithServices:myArray];
for (CBPeripheral* peripheral in peripherals)
{
  if ([[peripheral name] isEqualToString:@"Apple Pencil"])
  {
    // The Apple pencil is connected
  }
}
like image 109
Rich Avatar answered Oct 01 '22 11:10

Rich