LoginSignup
0

More than 3 years have passed since last update.

CoreBluetoothでCharacteristicを複数検索したい時

Posted at

状況

  • 一連のCentral Managerの立ち上げ後、discoverServices(_:)で指定したService UUIDを持つPeripheralが見つかった

  • Characteristicを複数持つPeripheralが存在するためCharacteristic検索を複数回行いたい

  • 一度にdiscoverCharacteristics(_ characteristicUUIDs: [CBUUID]?,
    for service: CBService)
    の配列にCharacteristicsを入れて検索するのではなく個別で複数回発火したい

解決法

基本的に複数回検索するのはベストプラクティスではない

検索するときは一度の検索で全てのcharacteristicsをまとめて検索する

// 正しい方法 
peripheral.discoverServices([fooCBUUID], [barCBUUID])

// これはダメ
peripheral.discoverServices([fooCBUUID])
peripheral.discoverServices([barCBUUID])

また、複数回discoverServices(_:)を呼んだ場合、それまでに検索したcharacteristicを保持したままそれを含めて検索するので気をつけてください
(これで困っている人たちもいらっしゃる)

参考サイト

https://forums.developer.apple.com/thread/14871
https://www.raywenderlich.com/231-core-bluetooth-tutorial-for-ios-heart-rate-monitor

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0