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