Ios 核心蓝牙发现未发现某些设备

Ios 核心蓝牙发现未发现某些设备,ios,swift,core-bluetooth,Ios,Swift,Core Bluetooth,我有3台设备,1台iPhone、1台iPad、1台macbook,并使用核心蓝牙来发现其他设备 iPad(外围设备) iPhone(中央) macbook(中央) 当iPad/外围设备在做广告,而其他设备处于中央模式时,我只能在macbook上找到它。iPhone从未看到广告数据。如果我转换角色,同样的事情会再次发生。iOS设备彼此看不见,只有macbook可以看到它们。我快发疯了,不知道还能做什么有什么想法吗? BluetoothCentral.swift import Foundation

我有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)")
    }
}
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")
        }
    }
}
蓝牙外围设备。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)")
    }
}
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")
        }
    }
}

您实际上还没有将外围设备添加到CBPeripal Manager中。我认为这不重要。macbook/central能够使用相同的centralManager代码发现广告数据我认为这很重要,这就是为什么我这么说的原因。同样的事情发生在苹果的示例代码中,该代码在manager中添加了一个外围设备。我将您的代码添加到外围设备管理器中,主要是为了扫描特定的服务;如果我没有这样做,我的中央处理器就会看到周围的众多外围设备,很难确定它为什么会发现给定的外围设备。你试过LightBlue应用程序而不是你的中央代码吗?