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.
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 .
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.
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.
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".
@import CoreBluetooth
@interface PencilDetector : NSObject <CBCentralManagerDelegate>
- (instancetype)init;
@end
#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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With