我正在学习核心蓝牙框架,我做了一个关于学习的测试项目:
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。 一个我用来执行这个应用程序'对于扫描,我想看到其他iPhone的名称(在蓝牙设置中名称是' iPhone'),但是当我开始扫描方法时,确实发现了'被调用,但设备常量为零。 为什么?我错了什么?
答案 0 :(得分:0)
会更好
peripheral.name
代替
let device =(advertiseData as NSDictionary)
.object(forKey:CBAdvertisementDataLocalNameKey)
如?的NSString