ios - CoreBluetooth 中央在指定服务时未发现任何外围设备
<p><p>我正试图弄清楚如何使用 CoreBluetooth 扫描外围设备,以宣传预先知道的服务。所以我有以下 ViewController ,它显示了 UITableViewController 中发现的外围设备:</p>
<pre class="lang-swift prettyprint-override"><code>import UIKit
import CoreBluetooth
class MasterViewController: UITableViewController {
var discoveredPeripherals = ()
var connectedPeripherals = ()
var peripheralsSupportingDeviceInformation = ()
private let scannedServices: ? = //nil
private var scanning = false
private var centralManager: CBCentralManager?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.leftBarButtonItem = editButtonItem
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// MARK: - Table View
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return discoveredPeripherals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let peripheral = discoveredPeripherals
cell.textLabel!.text = peripheral.identifier.uuidString
cell.detailTextLabel!.text = peripheral.name ?? ""
if self.peripheralsSupportingDeviceInformation.contains(peripheral) {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
@IBAction func scanTapped(_ sender: Any) {
if !scanning {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Stop", style: .plain, target: self, action: #selector(scanTapped(_:)))
self.discoveredPeripherals.removeAll()
self.connectedPeripherals.removeAll()
self.peripheralsSupportingDeviceInformation.removeAll()
self.centralManager = CBCentralManager(delegate: self, queue: nil)
} else {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Scan", style: .plain, target: self, action: #selector(scanTapped(_:)))
self.centralManager?.stopScan()
self.centralManager = nil
}
}
}
extension MasterViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: scannedServices, options: )
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: , rssi RSSI: NSNumber) {
if discoveredPeripherals.contains(where: { periph -> Bool in
periph.identifier.uuidString == peripheral.identifier.uuidString
}) {
return
}
self.discoveredPeripherals.append(peripheral)
self.tableView.reloadData()
central.connect(peripheral, options: nil)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
self.connectedPeripherals.append(peripheral)
peripheral.delegate = self
peripheral.discoverServices(scannedServices)
}
}
extension MasterViewController: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
print("Service discovery error: \(String(describing: error))")
} else {
if let services = peripheral.services, services.contains(where: { service -> Bool in
service.uuid.uuidString == "180A"
}) {
self.peripheralsSupportingDeviceInformation.append(peripheral)
self.tableView.reloadData()
} else if let services = peripheral.services {
let serviceList = services.map { service -> String in
return service.uuid.uuidString
}
print("Services supported by \(peripheral.description): \(serviceList.joined(separator: ", "))")
}
}
}
}
</code></pre>
<p>它是最基本的,我的应用程序中目前没有任何其他内容。
现在,如果我将 <code>scannedServices</code> 设置为 <code>nil</code>,我会得到我周围的所有设备,其中大多数设备,比如我的 MacBook Pro、Apple Watch 等都会显示一个复选标记,指示广告设备信息服务 (180A)。
然而,当我将scannedServices 设置为 <code></code> 以仅扫描宣传该服务的设备时(至少我从文档中了解到),然后 <code>centralManager (_:didDiscover:,advertisementData:,RSSI:)</code> 委托(delegate)方法不再被调用,列表保持为空。</p>
<p>我是否忘记或误解了文档中的某些内容?
如果你想试一试,完整的项目是<a href="https://github.com/sarbogast/BluetoothScanner" rel="noreferrer noopener nofollow">there</a> .</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的代码很好。没有发现任何设备的原因不同...</p>
<p>尽管大多数蓝牙设备都实现了<em>设备信息</em>服务,但它们并不宣传它。当您的应用程序扫描设备时,过滤器会处理广告数据。所以没有找到设备。连接到设备后,所有实现的服务都将可用。</p>
<p>因此,您需要使用确实宣传的不同蓝牙服务进行测试。 MacBook Pro 不会宣传任何服务,除非您有一个正在运行的应用程序。我不知道苹果 watch 。您可能需要另一台设备进行测试。</p>
<p>在 Android 或 iOS 设备上安装 Nordic Semiconductor 的 <em>nRF Connect</em> 应用程序。它可以扫描外围设备并显示广告服务。它甚至可以通过选择和宣传服务将其配置为外围设备。 </p></p>
<p style="font-size: 20px;">关于ios - CoreBluetooth 中央在指定服务时未发现任何外围设备,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/57826377/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/57826377/
</a>
</p>
页:
[1]