Bluetooth Logo

Writing an SDK With Core Bluetooth – 17 – Asking Questions

This entry is part 21 of 24 in the series Writing an SDK With Core Bluetooth

We are now ready to have the Central Mode ask a question to one of its Magic 8-Balls.

After we are done with this step, we will be able to run an instance of the app as a Central (on a Mac, iPhone/iPad/iPod, Watch or TV), and an instance of the app as a Peripheral (on a Mac or iPhone/iPad/iPod).

The Central will discover the Peripheral, and display its name in the list. The user will then be able to select the Peripheral, and “ask it a question.”

The Peripheral device will almost immediately display the question.

WHAT HAPPENS WHEN WE ASK

The Timeline for Asking A Question

As you can see, this is a lot less involved than discovering the device.

  1. The Central app uses the ITCB_Device_Peripheral_Protocol protocol interface provided by the Central SDK to send the question, as a simple String, using the sendQuestion(_:) method.
  2. The ITCB_SDK_Device_Peripheral (the actual instance that was accessed by the protocol) finds the Question Characteristic, then uses its associated CBPeripheral instance to request a write of that Characteristic from Core Bluetooth.
  3. The Central app is also ITCB_Observer_Central_Protocol-conformant, so it will receive a questionAskedOfDevice(_:) callback, to indicate that the question was successfully asked, and displays the question in the log.
  4. Core Bluetooth communicates with Core Bluetooth on the Peripheral device to transmit the write request over to the Peripheral.
  5. The ITCB_SDK_Peripheral SDK instance on the Peripheral is a CBPeripheralManagerDelegate, so it receives a CBPeripheralManagerDelegate.peripheralManager(_:, didReceiveWrite:) call from its instance of CBPeripheralManager.
  6. The Peripheral app is ITCB_Observer_Peripheral_Protocol-conformant, so it receives a questionAskedByDevice(_:, question:) call, and displays the question to the user.

Now that we know the question, let’s find the answer…