swift - 我将如何扫描其他 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)")
}
}
}
解决方案
推荐阅读
- asp.net-mvc - 如何传递外键
- node.js - 猫鼬消耗大量内存
- sql - Return SUBSTRING after specific word and end at first quote
- winapi - 检索与使用属性中的 DigitalSignature 选项卡看到的指纹相同的指纹
- windows - 程序集窗口函数 - 函数名称后的数字?
- javascript - API 响应在 Node 中为 NULL,但在 Python 中工作正常
- makefile - 在 gnu Makefile 中组合多个 ifeq 和 ifneq
- objective-c - Objective C - 对象没有被添加到 NSMutableArray
- javascript - Add Form control class to a dynamically create Textbox in MCV
- mysql - 不同类型的mysql公用表