Connecting iOS app to Arduino using Core Bluetooth

Hello,
I am writing an app that connects to an Arduino Nano with BLE. I would like the app to read data sent from the Arduino (the peripheral). My code connects to the Arduino using a specific UUID, but I think it is failing to detect a service from the Arduino. Here is my BluetoothViewController:
Code Block //
// BluetoothViewController.swift
import UIKit
import CoreBluetooth
//local central device object
class BluetoothViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {
//properties
private var centralManager: CBCentralManager!
private var peripheral: CBPeripheral!
var feedbackAmt = String() // from session setup view controller
//label to display stage of gait cycle
@IBOutlet weak var StateLabel: UILabel!
//characteristics
private var StateVar: CBCharacteristic?
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
//func tells the delegate the central manager's state updated (required)
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
//can start scanning peripherals
centralManager.scanForPeripherals(withServices: [ArduinoPeripheral.ArduinoServiceUUID], options: nil)
StateLabel.text = "Scanning for devices..."
}
}
//result of scan - call to this method reflects a detected advertisement packet of a BLE peripheral in range
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
//found peripheral so stop scanning
self.centralManager.stopScan()
//copy the peripheral instance
self.peripheral = peripheral
self.peripheral.delegate = self
//connect to peripheral
self.centralManager.connect(self.peripheral, options: nil)
}
//if successfully connected
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
//check if this is the desired peripheral
if peripheral == self.peripheral {
//start service discovery for desired service
peripheral.discoverServices([ArduinoPeripheral.ArduinoServiceUUID])
StateLabel.text = "Connected!"
}
}
//handle service discovery event
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
//cycle through available services
if let services = peripheral.services {
for service in services {
//find the service from arduino
if service.uuid == ArduinoPeripheral.ArduinoServiceUUID {
//start discovery of the service's characteristics
peripheral.discoverCharacteristics([ArduinoPeripheral.ArduinoCharacteristicUUID], for: service)
StateLabel.text = "Service found"
return
}
}
}
}
//handle discovery of characteristics
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.uuid == ArduinoPeripheral.ArduinoCharacteristicUUID {
StateVar = characteristic
StateLabel.text = "Characteristic found"
}
}
}
}
//subscribe to characteristic for value change / notification
func subscribeToNotifications(peripheral: CBPeripheral, characteristic: CBCharacteristic) {
peripheral.setNotifyValue(true, for: characteristic)
StateLabel.text = "Subscribed"
}
//notification update
func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
// Handle error
print(error)
return
}
// Successfully subscribed to or unsubscribed from notifications/indications on a characteristic
}
//read value
func readValue(characteristic: CBCharacteristic) {
self.peripheral.readValue(for: characteristic)
}
//value returned from readValue
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let error = error {
// Handle error
print(error)
return
}
StateLabel.text = "Value updated"
}
}


Where the Arduino characteristic UUUID and service UUID are defined in another swift file. I know it is connecting to the Arduino because the label says "Connected" when debugging, but it seems to get stuck on the line: "for service in services" and I am not sure why.
Thank you for the help!
I solved my issue but when I try to store the read value from the characteristic, I get the error: Referencing operator function '==' on 'BinaryInteger' requires that 'Data' conform to 'BinaryInteger'

I'm working on similar project, but I'm using the ESP BT mini-board, not the full Arduino. This is the tutorial I'm following https://www.cuvenx.com/post/ble-peripheral-ios-development-part2/ This board does connect 'is discovered'. Maybe when it's plugged into an Arduino, things will start working?

Where did you find the specific UUID for your bluetooth device?

Connecting iOS app to Arduino using Core Bluetooth
 
 
Q