Detect whether Apple Pencil is connected to an iPad Pro

后端 未结 2 573
南旧
南旧 2021-02-13 15:52

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 per

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 16:41

    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 
    
    - (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
      }
    }
    

提交回复
热议问题