Post

Replies

Boosts

Views

Activity

Bluetooth device name unknown, but it shows up in the iOS settings fine.
why is it that this code doesn't show the bluetooth device name but in the iOS settings it is displayed correctly. Thank you. import UIKit import CoreBluetooth import CoreLocation class BluetoothViewController: UIViewController, CBCentralManagerDelegate, CLLocationManagerDelegate { var centralManager: CBCentralManager! var locationManager: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() // Initialize central manager centralManager = CBCentralManager(delegate: self, queue: nil) // Initialize location manager to request location access locationManager = CLLocationManager() locationManager.delegate = self } // CBCentralManagerDelegate Methods func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .poweredOn: // Bluetooth is powered on, request location permission if needed if CLLocationManager.locationServicesEnabled() { locationManager.requestWhenInUseAuthorization() } startScanning() case .poweredOff: print("Bluetooth is powered off.") case .resetting: print("Bluetooth is resetting.") case .unauthorized: print("Bluetooth is unauthorized.") case .unknown: print("Bluetooth state is unknown.") case .unsupported: print("Bluetooth is unsupported on this device.") @unknown default: fatalError("Unknown Bluetooth state.") } } func startScanning() { // Start scanning for devices (you can add service UUIDs to filter specific devices) centralManager.scanForPeripherals(withServices: nil, options: [CBScanOptionAllowDuplicatesKey: true]) print("Scanning for Bluetooth devices...") } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) { // This method is called when a peripheral is discovered let deviceName = peripheral.name ?? "Unknown" let deviceAddress = peripheral.identifier.uuidString print("Found device: \(deviceName), \(deviceAddress)") // Optionally, you can stop scanning after discovering a device // centralManager.stopScan() } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { print("Connected to peripheral: \(peripheral.name ?? "Unknown")") } // CLLocationManagerDelegate Methods (for location services) func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if status == .authorizedWhenInUse { // Permission granted, now start scanning startScanning() } else { print("Location permission is required for Bluetooth scanning.") } } // Optionally handle when scanning stops or any errors occur func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { print("Failed to connect to peripheral: \(error?.localizedDescription ?? "Unknown error")") } func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { print("Disconnected from peripheral: \(peripheral.name ?? "Unknown")") } }
1
0
265
Jan ’25