Pequeña optimización de código

main
parent a10ed29791
commit 9d15b015df

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "A2E1875D-08F8-40F4-BF09-703444BECDCF"
type = "1"
version = "2.0">
</Bucket>

@ -6,7 +6,6 @@
//
import UIKit
import CoreBluetooth
class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
@ -26,7 +25,6 @@ class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
title = "Actividades"
BluetoothManager.shared.delegate = self
BluetoothManager.shared.searchForDevices()
}
@IBAction func connectButtonTapped(_ sender: UIButton) {
@ -84,11 +82,4 @@ class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
print("Datos del MPU6050: \(data)")
}
}
func didDiscoverPeripherals(peripherals: [CBPeripheral]) {
print("Dispositivos encontrados: ")
for peripheral in peripherals {
print("Nombre: \(peripheral.name ?? "Sin nombre"), UUID: \(peripheral.identifier.uuidString)")
}
}
}

@ -10,7 +10,6 @@ import CoreBluetooth
protocol BluetoothManagerDelegate: AnyObject {
func didUpdateConnectionStatus(isConnected: Bool)
func didReceiveData(fromCharacteristic uuid: String, data: String)
func didDiscoverPeripherals(peripherals: [CBPeripheral]) // delegado a notificar de dispositivos encontrados
}
class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
@ -25,7 +24,8 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
private var centralManager: CBCentralManager!
private var esp32Peripheral: CBPeripheral?
private var characteristics: [CBUUID: CBCharacteristic] = [:] // variable para almacenar características encontradas
private var discoveredPeripherals: [CBPeripheral] = [] // almacenar dispositivos encontrados
private let deviceName = "ESP_32_BLE_ITM" // Nombre del wearable para realizar conexión
weak var delegate: BluetoothManagerDelegate? // delegado para notificaciones
@ -37,7 +37,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
// inicia el escaneo de dispositivos
func startScanning() {
if centralManager.state == .poweredOn {
centralManager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil)
// centralManager.scanForPeripherals(withServices: [SERVICE_UUID], options: nil)
centralManager.scanForPeripherals(withServices: nil, options: nil)
print("Buscando ESP32...")
}
@ -65,7 +65,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
let uuid = peripheral.identifier.uuidString
print("Dispositivo detectado: \(deviceName) - UUID: \(uuid)")
if peripheral.name == "ESP_32_BLE_ITM" {
if peripheral.name == self.deviceName {
self.esp32Peripheral = peripheral
self.esp32Peripheral?.delegate = self
centralManager.stopScan()
@ -76,7 +76,7 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
// CBCentralManagerDelegate - Conexión exitosa
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Conectado al ESP32")
print("Dispositivo iOS conectado a: \(self.deviceName)")
delegate?.didUpdateConnectionStatus(isConnected: true)
esp32Peripheral?.discoverServices(nil) // descubrir todos los servicios
}
@ -125,15 +125,14 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
print("Comando enviado: \(command)")
}
// Función para buscar dispositivos BLE / depuraciones
func searchForDevices() {
discoveredPeripherals.removeAll()
if centralManager.state == .poweredOn {
centralManager.scanForPeripherals(withServices: nil, options: nil)
print("Buscando dispositivos BLE...")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
self.centralManager.stopScan()
print("Deteniendo escaneo")
self.delegate?.didDiscoverPeripherals(peripherals: self.discoveredPeripherals)
}
} else {
print("Bluetooth no esta disponible")

Loading…
Cancel
Save