繁体   English   中英

swift3无法搜索和显示BLE设备名称

[英]can't search and display BLE device name with swift3

我正在使用Swift 3为iOS编写BLE测试应用程序,并且遇到无法扫描设备的问题。

我想在最后一块调用方法"print("devicename: \\(peripheral)")"

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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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