Swift 核心蓝牙在扫描特定的CBUUID IOS时找不到外围设备(前台和后台)
我在扫描特定的CBUUID时遇到了核心蓝牙找不到外围设备的问题,我也想在前台和后台运行我的应用程序。但这是在没有CBUUID的情况下发现所有近外围设备扫描的问题“manager.scanForPeripherals(服务:nil,选项:nil)”。我使用的是“MactsAsBeacon”对于brodcasting beacon simulator,为什么它不能在具有特定CBUUID的外围设备上工作?它在后台如何工作?“我启用了蓝牙中心”我想在后台做些额外的工作吗Swift 核心蓝牙在扫描特定的CBUUID IOS时找不到外围设备(前台和后台),swift,ibeacon,beacon,Swift,Ibeacon,Beacon,我在扫描特定的CBUUID时遇到了核心蓝牙找不到外围设备的问题,我也想在前台和后台运行我的应用程序。但这是在没有CBUUID的情况下发现所有近外围设备扫描的问题“manager.scanForPeripherals(服务:nil,选项:nil)”。我使用的是“MactsAsBeacon”对于brodcasting beacon simulator,为什么它不能在具有特定CBUUID的外围设备上工作?它在后台如何工作?“我启用了蓝牙中心”我想在后台做些额外的工作吗 var manager:CBCe
var manager:CBCentralManager!
var peripheralCB:CBPeripheral!
var peripherals = [CBPeripheral]()
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {
if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] {
// 2
let peripherals = peripheralsObject as! Array<CBPeripheral>
// 3
if peripherals.count > 0 {
// 4
peripheralCB = peripherals[0]
// 5
peripheralCB?.delegate = self
}
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var consolMessages = ""
switch central.state
{
case .poweredOff:
consolMessages = "BLE is powered off"
case.poweredOn:
consolMessages = "BLE is powered on"
let serviceUUIDs = [CBUUID(string: "B0702880-A295-A8AB-F734-031A98A512DE") as AnyObject]
let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : false]
manager.scanForPeripherals(withServices: serviceUUIDs as? [CBUUID], options:dictionaryOfOptions)
// manager.scanForPeripherals(withServices: nil, options:nil)
case.resetting:
consolMessages = "BLE is resetting"
case.unauthorized:
consolMessages = "BLE is unauthorized"
case.unknown:
consolMessages = "BLE is unknown"
case.unsupported:
consolMessages = "unsupported"
}
print("\(consolMessages)")
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("peripheral \(peripheral)")
print("peripheral Name \(peripheral.name)")
if #available(iOS 9.0, *) {
peripheral.accessibilityAssistiveTechnologyFocusedIdentifiers()
} else {
// Fallback on earlier versions
}
print("peripheral Name \(peripheral.name)")
peripherals.append(peripheral)
manager.connect(peripheral, options: nil)
let AdvertsatingData = advertisementData[CBAdvertisementDataManufacturerDataKey]
print("AdvertsatingData\(AdvertsatingData)")
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("peripheral Connected")
print("peripheral didConnect \(peripheral)")
print("Connected peripheral Name \(peripheral.name)")
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
let alert = UIAlertController(title: "Alert", message: "didFailToConnect", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
print("peripheral Disconnectd")
print("Disconnect peripheral Name \(peripheral.name)")
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if error != nil{
}
else {
print("didDiscoverCharacteristicsFor")
}
}[![This is the Beacon Simulator https://i.stack.imgur.com/diyWA.png ][1]][1]
var管理器:CBCentralManager!
var peripheracb:CBPeripheral!
var外围设备=[CBPeripheral]()
重写func viewDidLoad(){
super.viewDidLoad()
manager=CBCentralManager(代理:self,队列:nil)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func centralManager(central:CBCentralManager,willRestoreState dict:[字符串:任意]){
如果let peripheralsObject=dict[CBCentralManagerRestoredStatePeripheralsKey]{
// 2
设peripherals=peripheralsObject as!数组
// 3
如果peripherals.count>0{
// 4
外围设备B=外围设备[0]
// 5
外围设备B?.delegate=自身
}
}
}
func CentralManager数据状态(uCentral:CBCentralManager){
var consolMessages=“”
切换中央状态
{
案例.断电:
ConsoleMessages=“BLE已关机”
case.poweredOn:
ConsoleMessages=“BLE已通电”
让ServiceUUID=[CBUUID(字符串:“B0702880-A295-A8AB-F734-031A98A512DE”)作为任意对象]
let DictionaryFoptions=[CBCentralManagerScanOptionAllowDuplicatesKey:false]
manager.ScanFor外围设备(服务:serviceUUIDs为?[CBUUID],选项:DictionaryFoOptions)
//manager.ScanFor外围设备(服务:无,选项:无)
案例1:重置:
ConsoleMessages=“BLE正在重置”
未经授权的情况:
consolemessages=“未经授权”
病例。未知:
consolemessages=“BLE未知”
case.unsupported:
ConsoleMessages=“不受支持”
}
打印(“\(控制台消息)”)
}
func centralManager(ucentral:CBCentralManager,didDiscover peripal:cbperipal,advertisementData:[String:Any],rssi-rssi:NSNumber){
打印(“外围设备\(外围设备)”)
打印(“外围设备名称\(peripheral.Name)”)
如果可用(iOS 9.0,*){
peripheral.accessibilityAssistiveTechnologyFocusedIdentifiers()
}否则{
//对早期版本的回退
}
打印(“外围设备名称\(peripheral.Name)”)
外围设备。附加(外围设备)
manager.connect(外围设备,选项:无)
让AdvertsatingData=advertisementData[CBAdvertisementDataManufacturerDataKey]
打印(“广告数据\(广告数据)”)
}
func centralManager(uCentral:CBCentralManager,didConnect外围设备:CBPeripheral){
打印(“外围连接”)
打印(“外围设备连接\(外围设备)”)
打印(“连接的外围设备名称\(peripheral.Name)”)
}
func centralManager(uCentral:CBCentralManager,DidFailt连接外围设备:CBPeripheral,错误:error?){
let alert=UIAlertController(标题:“alert”,消息:“didFailToConnect”,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.default,处理程序:nil))
self.present(警报、动画:true、完成:nil)
}
func centralManager(uCentral:CBCentralManager,DidDisconnect外围设备:CBPeripheral,错误:错误?){
打印(“外围设备断开连接”)
打印(“断开外围设备名称\(peripheral.Name)”)
}
func外围设备(外围设备:CBPeripal,DIDdiscoverCharacteristics服务:CBService,错误:error?){
如果错误!=nil{
}
否则{
打印(“DidDiscoveryCharacteristics for”)
}
}[![这是信标模拟器https://i.stack.imgur.com/diyWA.png ][1]][1]
这不起作用的几个原因:
- MactsAsBeacon发送iBeacon广告,这些广告是可复制的制造商广告,没有蓝牙服务UUID不要将iBeacon ProximityUID与用于使用CoreBooth进行扫描的服务UUID混淆。它们是根本不同的东西。
- 除非提供服务UUID,否则CoreBluetooth不会获得后台回调。这是苹果强加的限制
- 即使在前台,CoreBluetooth也无法从iBeacon数据包的数据中读取信标标识符。苹果为iOS上接收的iBeacon数据包清除这些数据
小心确保您可以使用现成的iOS信标检测器检测来自MactsAsBeacon的传输。Mac程序无法在某些风格的MacOS上传输数据。这不起作用的几个原因:
- MactsAsBeacon发送iBeacon广告,这些广告是可复制的制造商广告,没有蓝牙服务UUID不要将iBeacon ProximityUID与用于使用CoreBooth进行扫描的服务UUID混淆。它们是根本不同的东西。
- 除非提供服务UUID,否则CoreBluetooth不会获得后台回调。这是苹果强加的限制
- 即使在前台,CoreBluetooth也无法从iBeacon数据包的数据中读取信标标识符。苹果为iOS上接收的iBeacon数据包清除这些数据