首页 > 解决方案 > 我将如何扫描其他 iphone 设备

问题描述

嗨,我想创建一个简单的应用程序,当应用程序第一次打开时,它将显示我家中使用蓝牙的其他苹果设备,并打印设备名称。我不确定如何使用 CoreBluetooth 来解决这个问题。我已经尝试了一个我在下面写的教程,但它没有显示我家中的设备,我已经打开了蓝牙。它只显示我的邻居三星电视。我还想知道如何使用 UUID,这样我就不会在控制台中有重复的值。我什至尝试过 lightBlue 应用程序,它只显示这个三星电视。

安慰

代码

import UIKit
import CoreBluetooth


class ViewController: UIViewController,CBCentralManagerDelegate {
    private var centralManager : CBCentralManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

        view.backgroundColor = .white
    }
    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Bluetooth is On")
            centralManager.scanForPeripherals(withServices: nil, options: nil)
        } else {
            print("Bluetooth is not active")
        }
    }
    
 
    public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        print("\nName   : \(peripheral.name ?? "(No name)")")
        print("RSSI   : \(RSSI)")
        for ad in advertisementData {
            print("AD Data: \(ad)")
        }
    }
    



}

标签: swift

解决方案


推荐阅读