2

I'm trying to learn about CoreBluetooth and External Accessories on iOS.

First, I tried to see a list of devices connect to my phone via Bluetooth via print(EAAccessoryManager.sharedAccessoryManager().connectedAccessories) ... despite having 3 devices connected (according to the Settings app), I'm given an empty array.

Next, I tried registering for connect / disconnect notifications:

import UIKit
import ExternalAccessory

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "somethingConnected:",
            name: EAAccessoryDidConnectNotification,
            object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: "somethingDisconnected:",
            name: EAAccessoryDidDisconnectNotification,
            object: nil)

        EAAccessoryManager.sharedAccessoryManager().registerForLocalNotifications()
    }

    func somethingConnected(name: EAAccessory) {
        print("here")
    }

    func somethingDisconnected(name: EAAccessory) {
        print("there")
    }
}

... I receive nothing when I turn off/on (and thus disconnect/connect) a simple Bluetooth speaker I have.

I am seeing this issue (notifications not delivering until after the completion block of showBluetoothAccessoryPickerWithNameFilter() executes), but, generally, it seems like either:

A) Something with iOS isn't working correctly

B) I'm doing something wrong (the more likely of the two).

Do I need to have a special MFI certificate installed to see a list of connected Accessories? Why aren't notifications delivering?

Any recommendations / code examples are greatly appreciated.

Update

Most importantly: Still don't know why connectedAccessories doesn't work, so advice on this piece is greatly desired.

That said, re-reading the Apple Developer documentation, I don't believe that it's correct / possible to use NSNotificationCenter.defaultCenter().addObserver with these types of notifications.

Specifically, the documentation states that EA notifications will not be delivered until showBluetoothAccessoryPickerWithNameFilter() is called -- e.g. the EAAccessoryDidConnectNotification and EAAccessoryDidDisconnectNotification are meant to inform the app about what the User did with the picker dialogue. It doesn't seem that they are system-level notifications that can be picked up by NSNotificationCenter.

Please correct me if this is an incorrect reading.

Community
  • 1
  • 1
Dan
  • 4,197
  • 6
  • 34
  • 52

1 Answers1

2

you should change

selector: "somethingConnected:"

into

selector: @selector(somethingConnected:)

,than it will fire. For more details, please see the following website: Why does EAAccessoryDidConnectNotification occur twice?

Community
  • 1
  • 1
Nathan_hnl
  • 23
  • 7
  • it seems your code is Swift, oops. I made a mistake, sorry for that. But the provided link may solve your problem. – Nathan_hnl Jun 15 '16 at 09:51