Last active
February 15, 2023 16:45
-
-
Save alimovlex/d68ea6c332b7dd240941e7537dc52301 to your computer and use it in GitHub Desktop.
The BLE connection implementation for iOS in Swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (C) 2023 Recompile.me. | |
| * All rights reserved. | |
| */ | |
| import CoreBluetooth | |
| class BLE_Communicator: CBCentralManagerDelegate, CBPeripheralDelegate { | |
| let DEVICE_SERVICE_UUID = CBUUID.init(string: "b4250400-fb4b-4746-b2b0-93f0e61122c6"); | |
| var centralManager: CBCentralManager!; | |
| var peripheral: CBPeripheral!; | |
| func connect() { | |
| centralManager = CBCentralManager(delegate: self, queue: .global()); | |
| } | |
| func centralManagerDidUpdateState(_ central: CBCentralManager) { | |
| print("Central state update") | |
| if central.state != .poweredOn { | |
| print("Central is not powered on") | |
| } else { | |
| print("Central scanning for", DEVICE_SERVICE_UUID); | |
| centralManager.scanForPeripherals(withServices: [DEVICE_SERVICE_UUID], | |
| options: [CBCentralManagerScanOptionAllowDuplicatesKey : true]) | |
| } | |
| } | |
| // Handles the result of the scan | |
| func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { | |
| // We've found it so stop scan | |
| self.centralManager.stopScan() | |
| // Copy the peripheral instance | |
| self.peripheral = peripheral | |
| self.peripheral.delegate = self | |
| // Connect! | |
| self.centralManager.connect(self.peripheral, options: nil) | |
| } | |
| // The handler if we do connect succesfully | |
| func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { | |
| if peripheral == self.peripheral { | |
| print("Connected to BLE device") | |
| peripheral.discoverServices([DEVICE_SERVICE_UUID]); | |
| } | |
| } | |
| func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { | |
| if peripheral == self.peripheral { | |
| print("Disconnected"); | |
| self.peripheral = nil; | |
| // Start scanning again | |
| print("Central scanning for", DEVICE_SERVICE_UUID); | |
| centralManager.scanForPeripherals(withServices: [DEVICE_SERVICE_UUID], | |
| options: [CBCentralManagerScanOptionAllowDuplicatesKey : true]); | |
| } | |
| } | |
| // Handles discovery event | |
| func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { | |
| if let services = peripheral.services { | |
| for service in services { | |
| if service.uuid == DEVICE_SERVICE_UUID { | |
| print("LED service found") | |
| //Now kick off discovery of characteristics | |
| peripheral.discoverCharacteristics([DEVICE_SERVICE_UUID], for: service) | |
| } | |
| } | |
| } | |
| } | |
| } |
Author
Good morning. First of all say that you are a beginner in Swift 5.5. I am trying to use your code to communicate an iPad and an ESP32. The ESP32 part already works, but I don't get any reaction on the iPad, it doesn't even print to the console (print). I would like to know if you have more elaborate code that I could understand and use, and that would also include reading and writing features. Thank you very much.
Here is the explanation for what you need. Thanks for asking!
BLE_Communicator_Transceiver.swift
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good morning. First of all say that you are a beginner in Swift 5.5. I am trying to use your code to communicate an iPad and an ESP32. The ESP32 part already works, but I don't get any reaction on the iPad, it doesn't even print to the console (print).
I would like to know if you have more elaborate code that I could understand and use, and that would also include reading and writing features.
Thank you very much.