-
Notifications
You must be signed in to change notification settings - Fork 8
Connection management
Once you discover the devices, is time to establish a connection with one of them. To do
this you need a WLXConnectionManager
. The WLXConnectionManager
is a protocol that
exposes an API to manage the connection with a device. As with the WLXDiscoverer
, you
ask for an object that implements the WLXConnectionManager
to the WLXBluetoothDeviceManager
through the method connectionManagerForPeripheral:usingReconnectionStrategy:
.
To obtain a WLXConnectionManager
you need a CBPeripheral
and WLXReconnectionStrategy
.
You can get the CBPeripheral
from the WLXDiscoveryData
. As for the reconnection strategy
you can create an instance of one the classes provided by the library or implement your own.
Once you get a WLXConnectionManager
you can start a connection using one of the
following methods: connectWithTimeout:
or connectWithTimeout:usingBlock:
. Both of this
methods require a timeout (in milliseconds). After the timeout expires, if the connection
cannot be established, an error will be raised. If you use the method that requires a block,
that block will get called with an error or nil if the connection was successfully established.
If you want to be notified about the connection status, you can implement the
WLXConnectionManagerDelegate
that provides methods for every possible scenario:
- Connection established
- Fail to connect
- Connection lost
- Connection terminated
- Did reconnect
- Will attempt to reconnect
The library provides a protocol to encapsulate the reconnection
logic, WLXReconnectionStrategy
. It also provides two concrete implementations:
WLXLinearReconnectionStrategy
and WLXNullReconnectionStrategy
.
The WLXNullReconnectionStrategy
, as its name suggests, does nothing. If the
connection is lost a connection lost error will immediately be raised.
The WLXLinearReconnectionStrategy
performs a linear back-off between each
reconnection attempt. Every time a connection is lost this strategy will check
if there are any remaining connection attempts. In such case, after waiting for
a certain amount of time, it will try to reconnect. If there are no remaining
reconnection attempts a connection lost error will be raised. To create an
instance of WLXLinearReconnectionStrategy
you must provide the following
parameters:
- a wait time, that is the amount of time (in milliseconds) to wait between each reconnection attempt.
- the maximum amount of reconnection attempts.
- a connection timeout, you can use the same one you use for the first connection
- a dispatch queue, we recommend to provide the same one the device
manager is using. You can getting by doing
deviceManager.queue
Here is an example of how to create a linear reconnection strategy:
WLXBluetoothDeviceManager * deviceManager = ...;
id<WLXReconnectionStrategy> reconnectionStrategy =
[[WLXLinearReconnectionStrategy alloc] initWithWaitTime:2000
maxReconnectionAttempts:3
connectionTimeout:3000
queue:deviceManager.queue];
The WLXConnectionManager
also publishes connection related notifications using
NSNotificationCenter
. The following is a list of the notifications:
-
WLXBluetoothDeviceConnectionEstablished
: Published every time a connection is established. You can get the peripheral from the user info using the keyWLXBluetoothDevicePeripheral
. -
WLXBluetoothDeviceReconnectionEstablished
: Published every time a reconnection is established. You can get the peripheral from the user info using the keyWLXBluetoothDevicePeripheral
. -
WLXBluetoothDeviceFailToConnect
: Published when a connection with a peripheral could not be established. You can get the error from theNSNotification
user info using the keyWLXBluetoothDeviceError
. -
WLXBluetoothDeviceConnectionLost
: Published when a established connection with a peripheral is lost. You can get the error and the peripheral from the user info dictionary using the keysWLXBluetoothDeviceError
andWLXBluetoothDevicePeripheral
. -
WLXBluetoothDeviceConnectionTerminated
: Published every time a theWLXConnecitonManager
disconnect
method is called and the connection is successfully terminated. -
WLXBluetoothDeviceReconnecting
: Published every time a reconnection attempt is about to be executed. You can get the remaining reconnection attempts from the user info dictionary using the keyWLXBluetoothDeviceRemainingReconnectionAttemps
.
A device registry encapsulates the logic of maintaining a registry of the
connected devices, so the next time you want to connect with a device you
do not have to discover a device a again. To create a new WLXBluetoothDeviceRegistry
you need an object that conforms to the WLXBluetoothDeviceRepository
protocol.
The repository is the one that actually stores the required information to later
be able to retrieve a peripheral. This information is encapsulated in a
WLXBluetoothDeviceConnectionRecord
.
Once you get an instance of WLXBluetoothDeviceRegistry
you have to enable it, then every time a new connection is established the
registry will save the connection record using the provided repository. To
enable the registry you should set the enabled
property to YES
.
You can have more than one device registry but you probably need one. Keep in mind that if you have more than one registry they should use different repositories.
The library provides an repository that stores the last connection record
into the user defaults, WLXBluetoothDeviceUserDefaultsRepository
but
you can have your own repository, say core data, by implementing the
protocol WLXBluetoothDeviceRepository
.
The following is an example of how to create a device registry using the user defaults repository:
WLXBluetoothDeviceManager * deviceManager = [WLXBluetoothDeviceManager deviceManager];
id<WLXBluetoothDeviceRepository> repository = [[WLXBluetoothDeviceUserDefaultsRepository alloc] initWithUserDefaults:[NSUserDefaults standardUserDefaults]];
WLXBluetoothDeviceRegistry * registry = [deviceManager deviceRegistryWithRepository:repository];
registry.enabled = YES;
Here is an example of how to establish a connection with a previously stored device:
id<WLXReconnectionStrategy> strategy = ...;
CBPeripheral * peripheral = registry.lastConnectedPeripheral;
id<WLXConnectionManager> connectionManager = [deviceManager connectionManagerForPeripheral:peripheral usingReconnectionStrategy:strategy];
[connectionManager connectWithTimeout:3000];