Bluetooth | Bluetooth low energy | Discovering & Connecting Peripherals | Swift

I already discussed about Bluetooth & Bluetooth fundamentals and discovering peripherals.

We will now discuss about connecting from central to discovered peripheral. We will also learn discovering services and characteristics offered by peripheral. Let’s start discover:

delegate method as below:

As per apple doc for didConnect:

Parameters

central

The central manager providing this information.

peripheral

The peripheral that has been connected to the system.

Discussion

This method is invoked when a call to connect(_:options:) is successful. You typically implement this method to set the peripheral’s delegate and to discover its services.

After a successful connection from central to peripheral we should also look for discovering services provided by peripheral. It should be noted that a peripheral can contain more than one service and a service can contain more than one characteristic. Let’s discover service:

 

After discovering service, let’s discover characteristic.

Let’s implement didUpdateValueFor delegate method:

You can also write on a characteristic this way:

Let’s implement didReceiveWrite delegate method of peripheral manager:

will trigger call back to below delegate method: