ios - 蓝牙连接至 iPhone 的设备

标签 ios iphone swift bluetooth

我正在开发一个应用程序,需要知道特定设备是否连接到我的手机才能执行后续任务。你能告诉我该怎么做吗? 我尝试过使用 CoreBluetooth,但找不到任何可以执行此操作的函数。我不知道 CoreBluetooth 是否是合适的框架。

我需要这些数据,因为我的代码找不到我的蓝牙汽车(我想是因为没有 BLE,我不知道为什么),并且我必须通过手机的设置/蓝牙进行连接。这是代码。

import UIKit
import CoreBluetooth

class ListController: UITableViewController, CBCentralManagerDelegate, CBPeripheralDelegate{

var centralManager : CBCentralManager!

var sensorTag : CBPeripheral?


override func viewDidLoad()
{
    super.viewDidLoad()
    centralManager = CBCentralManager(delegate: self, queue: nil)

}

// MARK: - Bluetooth

func centralManagerDidUpdateState(_ central: CBCentralManager)
{
    switch central.state
    {
    case .poweredOn:
        debugPrint("Bluetooth on this device is currently powered on.")
        centralManager.scanForPeripherals(withServices: nil, options: nil)
    case .poweredOff:
        debugPrint("Bluetooth on this device is currently powered off.")
    case .unsupported:
        debugPrint("This device does not support Bluetooth Low Energy.")
    case .unauthorized:
        debugPrint("This app is not authorized to use Bluetooth Low Energy.")
    case .resetting:
        debugPrint("The BLE Manager is resetting; a state update is pending.")
    case .unknown:
        debugPrint("The state of the BLE Manager is unknown.")

    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
{
    if peripheral.name != nil
    {
        debugPrint((peripheral.name)!)
    }
}

最佳答案

CoreBluetooth只能支持BLE(蓝牙4.0以上)。 CoreBluetooth没有任何蓝牙握手协议(protocol)。我的意思是您可以在手机设置屏幕中看到除 BLE 之外的那些设备并且能够连接。

核心蓝牙仅允许您使用 GATT 配置文件与蓝牙低功耗设备进行通信。但您可以通过外部附件框架进行通信,允许使用串行端口协议(protocol) (SPP) 等配置文件与“传统”蓝牙设备进行通信。但这个框架必须需要MFi

关于ios - 蓝牙连接至 iPhone 的设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47803859/

相关文章:

ios - 如何以编程方式为ios中的 Collection View 单元格提供宽度和高度,swift 3

ios - 如何使 ViewController 中的 3 个文本字段仅接受整数输入 - XCode

iphone - iPhone 推送通知声音有限制吗?

ios - 如何快速将字符串转换为阿拉伯语言

ios - 如何控制 UIViewController 的 View 框架

iphone - 在 iPhone 中使用 MKPolygon 创建给定点的轮廓

iphone - 关于 CLLocation Manager 和多个 ViewController 的一些架构问题

ios - 有没有办法让 `genstrings` 与 `LocalizedStringKey` 一起工作?

ios - iOS 应用程序的 Podfile 使用

swift - identified(by :\. self) - 它有什么作用?