检测Apple Pencil是否已连接到iPad Pro

com*_*ial 13 ios

是否有API可以让您确定Apple Pencil是否已连接到iPad Pro?查看9.1 SDK我没有看到任何直接这样做的东西.或者这可以使用蓝牙API完成.

Ric*_*ich 7

我找不到Apple Pencil的蓝牙实现的任何实际文档(我不相信任何存在),但以下代码适用于我.

它检查自己宣传为支持"设备信息"服务的连接设备,然后如果其中任何一个具有"Apple Pencil"的名称.

PencilDetector.h

@import CoreBluetooth

@interface PencilDetector : NSObject <CBCentralManagerDelegate>

- (instancetype)init;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

实际上,以下更简单的同步代码在检查连接设备之前不等待中央管理器上电似乎在我的测试中也能正常工作.但是,文档声明在状态更新之前不应该调用管理器上的任何方法 CBCentralManagerStatePoweredOn,因此较长的代码可能更安全.

随时随地

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
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @ChandanShettySP @"180A"是对应于十六进制值"0x180A"的NSString,它是"设备信息"服务的蓝牙"分配号码"或"短UUID".这是当前铅笔设备提供的服务,并且它将在未来继续工作,因为大多数(或可能所有)蓝牙设备应提供此服务. (2认同)

HAS*_*HAS 5

我花了相当长时间才弄清楚CBCentralManager centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)仅在通过其connect(_ peripheral: CBPeripheral, options: [String : Any]? = nil)功能启动连接时才调用(是的,阅读文档会有所帮助:])。

由于当设备通过用户连接到设备时我们没有回调(就像Apple Pencil一样-我很乐意被证明是错误的),因此我不得不在这里使用计时器。

它是这样工作的:

初始化ApplePencilReachability计时器时,会设置一个计时器,它每秒检查一次铅笔的可用性。如果找到铅笔,则计时器无效;如果关闭了蓝牙,计时器也将无效。再次打开时,将创建一个新计时器。

我并不为此感到特别自豪,但它确实有效:-)

import CoreBluetooth

class ApplePencilReachability: NSObject, CBCentralManagerDelegate {

  private let centralManager = CBCentralManager()
  var pencilAvailabilityDidChangeClosure: ((_ isAvailable: Bool) -> Void)?

  var timer: Timer? {
    didSet {
      if oldValue !== timer { oldValue?.invalidate() }
    }
  }

  var isPencilAvailable = false {
    didSet { 
      guard oldValue != isPencilAvailable else { return }
      pencilAvailabilityDidChangeClosure?(isPencilAvailable)
    }
  }

  override init() {
    super.init()
    centralManager.delegate = self
    centralManagerDidUpdateState(centralManager) // can be powered-on already?
  }
  deinit { timer?.invalidate() }

  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
      timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { 
        [weak self] timer in // break retain-cycle
        self?.checkAvailability()
        if self == nil { timer.invalidate() }
      }
    } else {
      timer = nil
      isPencilAvailable = false
    }
  }

  private func checkAvailability() {
    let peripherals = centralManager.retrieveConnectedPeripherals(withServices: [CBUUID(string: "180A")])
    let oldPencilAvailability = isPencilAvailable
    isPencilAvailable = peripherals.contains(where: { $0.name == "Apple Pencil" })
    if isPencilAvailable {
      timer = nil // only if you want to stop once detected
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

  • 你是对的。同样,FWIW,所有BT内容都应该在后台队列上运行(它可以使DispatchQueue.main.async回调)。同样,名称比较可能不是一个好主意。 (2认同)