Last active
February 15, 2023 16:40
-
-
Save alimovlex/cd4df88605aebc93a650f7171ab8c7d9 to your computer and use it in GitHub Desktop.
The BLE demo code for the data transmission.
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 { | |
| //The UUID of your BLE device. | |
| 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()); | |
| } | |
| //------------------------------------THE CENTRAL MANAGER SECTION-------------------------------------------- | |
| 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]); | |
| } | |
| } | |
| //------------------------------------THE PERIPHERAL SECTION-------------------------------------------- | |
| // 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) | |
| } | |
| } | |
| } | |
| } | |
| func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { | |
| if let peripheralServices = service.characteristics { | |
| print("The current peripheral's BLE characteristic is: \(services.characteristics.enumerated())"); | |
| for peripheralCharacteristic in peripheralServices { | |
| peripheral.setNotifyValue(true, for: peripheralCharacteristic); | |
| peripheral.readValue(for: peripheralCharacteristic); | |
| } | |
| } else { | |
| print(error?.localizedDescription); | |
| } | |
| } | |
| public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { | |
| print(characteristic.value.map { String(format: "%02x", $0) }.joined()); | |
| } | |
| func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) { | |
| peripheral.readRSSI(); | |
| } | |
| func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { | |
| print("The following message has been sent: \(characteristic.value.map { String(format: "%02x", $0) }.joined(separator: ""))"); | |
| } | |
| func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) { | |
| if characteristic.isNotifying { | |
| print("Subscribed to \(characteristic.uuid)"); | |
| } else { | |
| print(error?.localizedDescription); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment