CBCentralManager Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/CoreBluetooth.framework |
| Availability | Available in iOS 5.0 and later. |
| Declared in | CBCentralManager.h |
Overview
CBCentralManager objects are used to manage discovered or connected remote peripheral devices (represented by CBPeripheral objects), including scanning for, discovering, and connecting to advertising peripherals.
Before you call CBCentralManager methods, the state of the central manager object must be powered on, as indicated by the CBCentralManagerStatePoweredOn constant. This state indicates that the central device (your iPhone or iPad, for instance) supports Bluetooth low energy and that Bluetooth is on and available to use.
Tasks
Initializing a Central Manager
Establishing or Canceling Connections with Peripherals
Retrieving Lists of Peripherals
Scanning or Stopping Scans of Peripherals
Monitoring Properties
Properties
delegate
The delegate object you want to receive central events.
Discussion
For information about how to implement your central manager delegate, see CBCentralManagerDelegate Protocol Reference.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hstate
Returns the current state of the central manager. (read-only)
Discussion
When a central manager object is initially created, the default value of this property is CBCentralManagerStateUnknown. As the central manager’s state changes, the central manager updates the value of this property and calls the centralManagerDidUpdateState: delegate method. For a list of the possible values representing the state of the central manager, see “Central Manager State.”
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hInstance Methods
cancelPeripheralConnection:
Cancels an active or pending local connection to a peripheral.
Parameters
- peripheral
The peripheral to which the central manager is either trying to connect or has already connected.
Discussion
This method is nonblocking, and any CBPeripheral class commands that are still pending to peripheral may or may not complete. Because other apps may still have a connection to the peripheral, canceling a local connection does not guarantee that the underlying physical link is immediately disconnected. From the app’s perspective, however, the peripheral is considered disconnected, and the central manager object calls the centralManager:didDisconnectPeripheral:error: method of its delegate object.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hconnectPeripheral:options:
Establishes a local connection to a peripheral.
Parameters
- peripheral
The peripheral to which the central is attempting to connect.
- options
A dictionary to customize the behavior of the connection. For available options, see “Peripheral Connection Options.”
Discussion
If a local connection to a peripheral is successfully established, the central manager object calls the centralManager:didConnectPeripheral: method of its delegate object. If the connection attempt fails, the central manager object calls the centralManager:didFailToConnectPeripheral:error: method of its delegate object instead. Attempts to connect to a peripheral do not time out. To explicitly cancel a pending connection to a peripheral, call the cancelPeripheralConnection: method. Pending connection attempts are also are canceled automatically when peripheral is deallocated.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hinitWithDelegate:queue:
Initializes the central manager with a specified delegate and dispatch queue.
Parameters
- delegate
The delegate to receive the central events.
- queue
The dispatch queue to use to dispatch the central role events. If its value is
nil, the central manager dispatches central role events using the main queue.
Return Value
Returns a newly initialized central manager.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hretrieveConnectedPeripherals
Retrieves a list of the peripherals currently connected to the system.
Discussion
Upon retrieving the list of connected peripherals, the central manager object calls the centralManager:didRetrieveConnectedPeripherals: method of its delegate object. If there are no peripherals currently connected to the system, the central manager object passes an empty array of peripherals to the centralManager:didRetrieveConnectedPeripherals: method. Note that the list of connected peripherals can include those that are connected by other apps and that will need to be connected locally via the connectPeripheral:options: method before they can be used.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hretrievePeripherals:
Retrieves a list of known peripherals by their UUIDs.
Parameters
- peripheralUUIDs
An array of
CFUUIDRefobjects from whichCBPeripheralobjects can be retrieved. SinceCBUUIDobjects represent Bluetooth-specific universally unique identifiers (UUIDs) (used for Bluetooth GATT attributes such as services and characteristics),CBPeripheralobjects use standardCFUUIDRefUUIDs as their unique identifiers.
Discussion
This method retrieves a list of peripherals that the central manager is able to match to the provided UUIDs. Upon retrieving the list of known peripherals, the central manager object calls the centralManager:didRetrievePeripherals: method of its delegate object. If no matches are made, the central manager object passes an empty array of peripherals to the centralManager:didRetrievePeripherals: method.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hscanForPeripheralsWithServices:options:
Scans for peripherals that are advertising service(s).
Parameters
- serviceUUIDs
An array of
CBUUIDobjects that the app is interested in. In this case, eachCBUUIDobject represents the UUID of a service that a peripheral is advertising.- options
An optional dictionary specifying options to customize the scan. For available options, see “Peripheral Scanning Option.”
Discussion
You can provide an array of CBUUID objects—representing service UUIDs—in the serviceUUIDs parameter. When you do, the central manager returns only peripherals that advertise services that the app is interested in (recommended). If the serviceUUIDs parameter is nil, all discovered peripherals are returned regardless of their supported services (not recommended). If the central manager is already scanning with different parameters, the provided parameters replace them. When the central manager object discovers a peripheral, it calls the centralManager:didDiscoverPeripheral:advertisementData:RSSI: method of its delegate object.
Availability
- Available in iOS 5.0 and later.
Declared In
CBCentralManager.hConstants
Peripheral Scanning Option
Keys used to pass options to the scanForPeripheralsWithServices:options: method.
NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;
Constants
CBCentralManagerScanOptionAllowDuplicatesKeyA Boolean value that specifies whether the scan should run without duplicate filtering. The value for this key is an
NSNumberobject. IfYES, multiple discoveries of the same peripheral are coalesced into a single discovery event. IfNO, filtering is disabled. Disabling this filtering can have an adverse effect on battery life and should be used only if necessary. If the key is not specified, the default value isYES.Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.
Peripheral Connection Options
Keys used to pass options to the connectPeripheral:options: method.
NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey; NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey; NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
Constants
CBConnectPeripheralOptionNotifyOnConnectionKeyA Boolean value that specifies whether the system should display an alert for a given peripheral if the app is suspended when a successful connection is made. The value for this key is a
NSNumberobject. This key is useful for apps that have not specified thebluetooth-centralbackground mode and cannot display their own alert. If more than one app has requested notification for a given peripheral, the one that was most recently in the foreground receives the alert. If the key is not specified, the default value isNO.Available in iOS 6.0 and later.
Declared in
CBCentralManager.h.CBConnectPeripheralOptionNotifyOnDisconnectionKeyA Boolean value that specifies whether the system should display a disconnection alert for a given peripheral if the app is suspended at the time of the disconnection. The value for this key is a
NSNumber. This key is useful for apps that have not specified thebluetooth-centralbackground mode and cannot display their own alert. If more than one app has requested notification for a given peripheral, the one that was most recently in the foreground receives the alert. If the key is not specified, the default value isNO.Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBConnectPeripheralOptionNotifyOnNotificationKeyA Boolean value that specifies whether the system should display an alert for all notifications received from a given peripheral if the app is suspended at the time. The value for this key is a
NSNumberobject. This key is useful for apps that have not specified thebluetooth-centralbackground mode and cannot display their own alert. If more than one app has requested notification for a given peripheral, the one that was most recently in the foreground receives the alert. If the key is not specified, the default value isNO.Available in iOS 6.0 and later.
Declared in
CBCentralManager.h.
Central Manager State
Values representing the current state of a central manager object.
typedef enum {
CBCentralManagerStateUnknown = 0,
CBCentralManagerStateResetting,
CBCentralManagerStateUnsupported,
CBCentralManagerStateUnauthorized,
CBCentralManagerStatePoweredOff,
CBCentralManagerStatePoweredOn,
} CBCentralManagerState;
Constants
CBCentralManagerStateUnknownThe current state of the central manager is unknown; an update is imminent.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBCentralManagerStateResettingThe connection with the system service was momentarily lost; an update is imminent.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBCentralManagerStateUnsupportedThe platform does not support Bluetooth low energy.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBCentralManagerStateUnauthorizedThe app is not authorized to use Bluetooth low energy.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBCentralManagerStatePoweredOffBluetooth is currently powered off.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.CBCentralManagerStatePoweredOnBluetooth is currently powered on and available to use.
Available in iOS 5.0 and later.
Declared in
CBCentralManager.h.
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)