问题描述
我有3台设备,1台iPhone,1台iPad,1台Macbook,并使用核心蓝牙来发现其他设备。
iPad(外围设备)
iPhone(中央)
macbook(中央)
当iPad /外围设备在做广告而其他设备处于中央模式时,我只能在Macbook上发现它。 iPhone永远看不到广告数据。如果我换了角色,同样的事情会再次发生。 iOS设备永远不会看到彼此,只有Macbook可以看到它们。我快疯了,不知道该怎么办。 有什么想法吗?
BluetoothCentral.swift
import Foundation
import CoreBluetooth
class BluetoothCentral: NSObject,ObservableObject {
var centralManager: CBCentralManager!
override init(){
super.init()
centralManager = CBCentralManager(delegate: self,queue: .main)
}
}
extension BluetoothCentral: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown:
print("central.state is .unknown")
case .resetting:
print("central.state is .resetting")
case .unsupported:
print("central.state is .unsupported")
case .unauthorized:
print("central.state is .unauthorized")
case .poweredOff:
print("central.state is .poweredOff")
case .poweredOn:
print("central.state is .poweredOn")
centralManager.scanForPeripherals(withServices: nil)
@unknown default:
print("Unknown")
}
}
func centralManager(_ central: CBCentralManager,didDiscover peripheral: CBPeripheral,advertisementData: [String : Any],rssi RSSI: NSNumber) {
print(peripheral)
print("---------> \(peripheral.name)")
}
}
BluetoothPeripheral.swift
import Foundation
import CoreBluetooth
let SERVICE_NAME = "card-iphone"
class BluetoothPeripheral: NSObject,ObservableObject {
var uuid:String = ""
var peripheralManager: CBPeripheralManager?
required override init() {
super.init()
}
func start(uuid: String){
self.uuid = uuid
peripheralManager = CBPeripheralManager(delegate: self,queue: nil)
}
}
extension BluetoothPeripheral: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheral.state == .poweredOn {
if peripheral.isAdvertising {
peripheral.stopAdvertising()
}
let advertisingData: [String : Any] = [
CBAdvertisementDataLocalNameKey: SERVICE_NAME
]
self.peripheralManager?.startAdvertising(advertisingData)
} else {
print("handle other states")
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)