简体   繁体   中英

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. I don't know if CoreBluetooth it's the right framework to use for it.

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. 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 don't have any handshake protocol of bluetooth. i mean you can see those device other then BLE in phone setting screen and able to connect.

Core Bluetooth only allows you to communicate with Bluetooth Low Energy devices using the GATT profile. 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). But for this framework must need MFi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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