gpt4 book ai didi

ios - Swift:为什么我的 iOS 无法扫描其他蓝牙设备

转载 作者:行者123 更新时间:2023-11-30 13:52:02 24 4
gpt4 key购买 nike

现在我正在做一个应用程序项目,需要我的 iPhone 扫描附近的其他蓝牙设备并将它们列出来。我想知道我的代码有什么问题吗?

代码:

import UIKit
import CoreBluetooth

class ViewController: UIViewController, CBCentralManagerDelegate {

var manager: CBCentralManager!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager = CBCentralManager (delegate: self, queue: nil)
}


func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

print("Peripheral: \(peripheral)")
}

func centralManagerDidUpdateState(central: CBCentralManager) {
print("Checking")
switch(central.state)
{
case.Unsupported:
print("BLE is not supported")
case.Unauthorized:
print("BLE is unauthorized")
case.Unknown:
print("BLE is Unknown")
case.Resetting:
print("BLE is Resetting")
case.PoweredOff:
print("BLE service is powered off")
case.PoweredOn:
print("BLE service is powered on")
print("Start Scanning")
manager.scanForPeripheralsWithServices(nil, options: nil)
default:
print("default state")
}
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

我使用的是 iPhone 5 (iOS 9),并且我确定我的蓝牙已打开。

当我在 iPhone 中运行该应用程序时,控制台仅记录以下输出:

Checking
BLE service is powered on
Start Scanning

但是输出中没有显示蓝牙设备的名称。即使我打开我的 iPad (iPad Mini 4 iOS 8),列表仍然不会更新。

有时它会扫描我的 MacBook Pro 蓝牙,输出结果如下:

Peripheral: <CBPeripheral: 0x14d70e00, identifier = 54738076-6C97-FD04-18CF-5E1AF6705865, name = vivien’s MacBook Pro, state = disconnected>

那么,为什么会发生这种情况呢?有人可以给我解释一下吗?

最佳答案

案例1:
您必须使用 GKSession 来扫描并连接其他 iOS 设备,而不是 CoreBluetooth。
案例2:
您的蓝牙设备是蓝牙 3.0 配件。您的 iPhone 可以在“设置”->“蓝牙”中发现并显示它。
但此消息不会传递到您的应用,因此您的应用不会发现它。
使用蓝牙 4.0 配件重试。

关于ios - Swift:为什么我的 iOS 无法扫描其他蓝牙设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com