我正在研究核心蓝牙框架,我做了一个关于它的学习测试项目:
class ViewController: UIViewController,CBCentralManagerDelegate,CBPeripheralDelegate {
var centralManager: CBCentralManager = CBCentralManager()
var peripheral: CBPeripheral? = nil
override func viewDidLoad()
{
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func centralManagerDidUpdateState(_ central: CBCentralManager)
{
central.scanForPeripherals(withServices: nil, options: nil)
}
@available(iOS 5.0, *)
public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
{
let device = (advertisementData as NSDictionary)
.object(forKey: CBAdvertisementDataLocalNameKey)
as? NSString
let isMyIphone = device?.contains("iPhone")
}
}
为了开始我想看到我周围的蓝牙名称,因此我有2个iPhone。一个我用来执行这个'app'进行扫描,我想看到另一个iPhone的名称(蓝牙设置名称是'iPhone'),但是当我开始扫描方法'didDiscover'被调用但设备常数是零。为什么?我错了什么?
如果你使用peripheral.name更好 让device =(advertiseData as NSDictionary).object(forKey:CBAdvertisementDataLocalNameKey)as?的NSString