fix(BLE): correción a código BLE

main
parent a2b7c60346
commit a10ed29791

BIN
.DS_Store vendored

Binary file not shown.

Binary file not shown.

@ -7,7 +7,7 @@
<key>App-iOS-Wearable.xcscheme_^#shared#^_</key> <key>App-iOS-Wearable.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>0</integer> <integer>3</integer>
</dict> </dict>
</dict> </dict>
</dict> </dict>

Binary file not shown.

@ -6,6 +6,7 @@
// //
import UIKit import UIKit
import CoreBluetooth
class ActivitiesViewController: UIViewController, BluetoothManagerDelegate { class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
@ -25,6 +26,7 @@ class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
title = "Actividades" title = "Actividades"
BluetoothManager.shared.delegate = self BluetoothManager.shared.delegate = self
BluetoothManager.shared.searchForDevices()
} }
@IBAction func connectButtonTapped(_ sender: UIButton) { @IBAction func connectButtonTapped(_ sender: UIButton) {
@ -82,4 +84,11 @@ class ActivitiesViewController: UIViewController, BluetoothManagerDelegate {
print("Datos del MPU6050: \(data)") 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)")
}
}
} }

@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Necesitamos su Bluetooth para la conexión con el SmartWatch.</string>
<key>UIMainStoryboardFile</key> <key>UIMainStoryboardFile</key>
<string>Main</string> <string>Main</string>
<key>GIDClientID</key> <key>GIDClientID</key>

@ -10,6 +10,7 @@ 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 {
@ -24,6 +25,7 @@ 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
weak var delegate: BluetoothManagerDelegate? // delegado para notificaciones weak var delegate: BluetoothManagerDelegate? // delegado para notificaciones
@ -59,7 +61,11 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
// CBCentralManagerDelegate - Detectar y conectar al ESP32 // CBCentralManagerDelegate - Detectar y conectar al ESP32
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) { func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
if peripheral.name == "ESP32_BLE_Serial" { let deviceName = peripheral.name ?? "Sin nombre"
let uuid = peripheral.identifier.uuidString
print("Dispositivo detectado: \(deviceName) - UUID: \(uuid)")
if peripheral.name == "ESP_32_BLE_ITM" {
self.esp32Peripheral = peripheral self.esp32Peripheral = peripheral
self.esp32Peripheral?.delegate = self self.esp32Peripheral?.delegate = self
centralManager.stopScan() centralManager.stopScan()
@ -118,4 +124,19 @@ class BluetoothManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
esp32Peripheral?.writeValue(data, for: cmdCharacteristic, type: .withResponse) esp32Peripheral?.writeValue(data, for: cmdCharacteristic, type: .withResponse)
print("Comando enviado: \(command)") print("Comando enviado: \(command)")
} }
func searchForDevices() {
discoveredPeripherals.removeAll()
if centralManager.state == .poweredOn {
centralManager.scanForPeripherals(withServices: nil, options: nil)
print("Buscando dispositivos BLE...")
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.centralManager.stopScan()
print("Deteniendo escaneo")
self.delegate?.didDiscoverPeripherals(peripherals: self.discoveredPeripherals)
}
} else {
print("Bluetooth no esta disponible")
}
}
} }

Loading…
Cancel
Save