繁体   English   中英

将设备蓝牙连接到iPhone

[英]Devices Bluetooth connected to iPhone

我正在开发一个应用程序,该应用程序需要知道是否有特定设备连接到我的手机才能执行下一个任务。 你能告诉我怎么做吗? 我试过使用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(以上的Bluetooth 4.0)。 CoreBluetooth没有任何蓝牙握手协议。 我的意思是,您可以在电话设置屏幕中看到BLE以外的那些设备,并且可以连接。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM