简体   繁体   English

将设备蓝牙连接到iPhone

[英]Devices Bluetooth connected to iPhone

i'm developing an app that need to know if a specific device is connected to my phone to do next tasks. 我正在开发一个应用程序,该应用程序需要知道是否有特定设备连接到我的手机才能执行下一个任务。 Can you show me how can i do it? 你能告诉我怎么做吗? I've tried to use CoreBluetooth but i can't find any function that do it. 我试过使用CoreBluetooth,但找不到任何执行此功能的函数。 I don't know if CoreBluetooth it's the right framework to use for it. 我不知道CoreBluetooth是否是用于它的正确框架。

I need to have this datas because my code didn't find my bluetooth car (i think because doesn't have a BLE, i don't know why) and i must do connection from setting/bluetooth of my phone. 我需要这些数据,因为我的代码找不到我的蓝牙汽车(我认为因为没有BLE,我不知道为什么),并且我必须通过手机的设置/蓝牙进行连接。 This is the code. 这是代码。

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 can only support BLE (bluetooth 4.0 above). CoreBluetooth仅支持BLE(以上的Bluetooth 4.0)。 CoreBluetooth don't have any handshake protocol of bluetooth. CoreBluetooth没有任何蓝牙握手协议。 i mean you can see those device other then BLE in phone setting screen and able to connect. 我的意思是,您可以在电话设置屏幕中看到BLE以外的那些设备,并且可以连接。

Core Bluetooth only allows you to communicate with Bluetooth Low Energy devices using the GATT profile. 核心蓝牙仅允许您使用GATT配置文件与低功耗蓝牙设备进行通信。 But you can able to communicate via External Accessory framework allows communication with 'legacy' Bluetooth devices using profiles such as the Serial Port Protocol (SPP). 但是您可以通过外部附件框架进行通信,从而允许使用诸如串行端口协议(SPP)之类的配置文件与“传统”蓝牙设备进行通信。 But for this framework must need MFi 但是对于这个框架必须需要MFi

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

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