简体   繁体   中英

can't search and display BLE device name with swift3

I'm coding BLE test apps for iOS with Swift 3 and I'm struggling with a problem that I can't scan devices.

I want to call the method "print("devicename: \\(peripheral)")" at the last block.

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

    private var isScanning = false
    var centralManager: CBCentralManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.centralManager = CBCentralManager(delegate: self,queue: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func centralManagerDidUpdateState(_ central: CBCentralManager) {

        if central.state == CBManagerState.poweredOn {

            centralManager.scanForPeripherals(withServices: nil,options: nil)
        } else {
            print("not ready")
        }
    }

    private func centralManager(central: CBCentralManager!,didDiscoverPeripheral peripheral: CBPeripheral!,advertisementData: [String : AnyObject]!,RSSI: NSNumber!)
    {
        print("devicename: \(peripheral)")
    }
}
func centralManagerDidUpdateState(_ central: CBCentralManager){

    switch central.state {

        case .poweredOn:    central.scanForPeripherals(withServices: nil, options: nil)

        default: print("Please turn on bluetooth")
        break
    }

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){

     print("----->",advertisementData)
     print("==>", peripheral)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM