Playground Bluetooth Connection View provides an interface for displaying connection status of Bluetooth devices. Before we tackle the PlaygroundBluetooth API, we need to setup this Connection View in the view controller so that users can choose discovered peripherals to connect to.
PlaygroundBluetoothConnectionViewDelegate
The PlaygroundBluetoothConnectionView protocol defines the methods that a delegate of PlaygroundBluetoothCentralManager objects must adopt. The optional methods of the protocol allow the delegate to monitor the discovery, connectivity, and retrieval of peripheral devices.
Apple does not go into much detail about PlaygroundBluetooth Connection View, but these are the methods used to display a table view that shows you the connectable devices discovered during a peripheral scan:
connectionView(_: titleFor:) connectionView(_:itemForPeripheral:withAdvertisementData:rssi:) connectionView(_: shouldDisplayDiscovered:withAdvertisementData:rssi) connectionView(_: firmwareUpdateInstructionFor:)
Connection View: TitleFor method
In the Swift Playgrounds for Bluefruit playground books live view, you'll notice a button with the label "Connect RC". This was added using the connectionView(_: titleFor: )
method. This method provides a localized title for the given state of the connection view.
For example, if the connection view state is "noConnection", the UI label will display "Connect RC" which will give the user the option to search for peripherals in the area. If the connection view state is "selectingPeripherals" the UI will expand into a table view that will populate the rows with peripheral devices found with the UUID we are looking for. On top of the table view, we see the "Select your RC" label that was localized in the titleFor method:
Connection View: itemForPeripheral method
This method displays UI elements when a peripheral appears in the table view. Here, you'll need to add a peripheral name placeholder for any unidentified peripheral.
Then, you'll need to give each peripheral device an icon image for the peripheral to be displayed in the table view. I created an image of the Adafruit logo in photoshop with the dimensions 83x83. The image was placed in the Private Resources folder then referenced in the itemForPeripheral method:
let icon = UIImage(imageLiteralResourceName:"Images/adafruit_logo_small copy.png") let issueIcon = icon
Now when a peripheral is found, it will have a name displayed and image attached to it:
Connection View: shouldDisplayDiscovered method
This method filters out peripheral items. There's not much information available for this method, but it is optional.
Page last edited March 08, 2024
Text editor powered by tinymce.