我需要将哪些服务UUID 传递给retrieveConnectedPeripheralsWithServices 以快速获取任何连接的设备?

Posted

技术标签:

【中文标题】我需要将哪些服务UUID 传递给retrieveConnectedPeripheralsWithServices 以快速获取任何连接的设备?【英文标题】:which serviceUUIDs do I need to pass to retrieveConnectedPeripheralsWithServices to get any connected device in swift? 【发布时间】:2016-08-30 18:48:08 【问题描述】:

我正在尝试使用 swift 列出当前通过蓝牙连接到我的 iPhone 的设备。我想要任何设备(BLE、Classic Bluetooh...)

我在 Xcode 7.3 和 swift 2.2 中使用 CoreBluetooth

//CoreBluetooth methods
func centralManagerDidUpdateState(central: CBCentralManager)

    if (central.state == CBCentralManagerState.PoweredOn)
    
        self.centralManager?.retrieveConnectedPeripheralsWithServices(**HERE**)
    
    else
    
        // do something like alert the user that ble is not on
    


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

    allPeripherals.append(peripheral)


我无法编译项目,因为我不知道将什么作为参数传递给函数 retrieveConnectedPeripheralsWithServices 以获取所有连接的设备。

感谢您的帮助。

【问题讨论】:

查看这个答案:***.com/questions/25865937/… 【参考方案1】:

根据文档,您应该输入一个带有 CBUUID 对象的 NSArray。这些服务 UUID 必须属于先前已在此设备上为该外围设备发现的 UUID。通常,您会键入与扫描外围设备时使用的相同的 uuid。

在 Objective-C 上,至少这是一个你可以使用的例子:

@[[CBUUID UUIDWithString:@"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]]

我显然不知道你需要哪个 uuid。

【讨论】:

你知道我是否可以从系统中获取这些 UUID 吗?【参考方案2】:

经过调查,使用CoreBluetooh找不到经典设备,需要使用ExternalAccessory框架。这里有一段代码可以连接当前的“经典”设备:

class ViewController: UIViewController,CLLocationManagerDelegate,CBCentralManagerDelegate, EAAccessoryDelegate

var locationManager:CLLocationManager=CLLocationManager()
let accuracy=kCLLocationAccuracyBest


var centralManager: CBCentralManager?
var allPeripherals: Array<CBPeripheral> = Array<CBPeripheral>()
var connectedPheripherals: Array<CBPeripheral> = Array<CBPeripheral>()

var eaccessory:EAAccessoryManager?

@IBAction func putAction(sender: AnyObject) 
    //        GPS info
    let latitude=locationManager.location?.coordinate.latitude
    let longitude=locationManager.location?.coordinate.longitude
    let altitude=locationManager.location?.altitude
    let verticalAccuracy=locationManager.location?.verticalAccuracy
    let horizontalAccuracy=locationManager.location?.horizontalAccuracy
    let speed=locationManager.location?.speed

    //          Carrier info
    let networkInfo = CTTelephonyNetworkInfo()
    let carrier = networkInfo.subscriberCellularProvider
    let carrierName = carrier!.carrierName

    //          Phone's info
    let phone=UIDevice.currentDevice()
    let model=phone.model
    let os=phone.systemName

    //          Bluetooth's info
    for d in (eaccessory?.connectedAccessories)!
    
        print("name: \(d.name)")
        print("modelNumber: \(d.modelNumber)")
        print("MAC: \(d.valueForKey("macAddress"))")
    



func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) 



private func sendRequest(latitude:CLLocationDegrees, longitude:CLLocationDegrees, accuracy: CLLocationAccuracy)




override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    centralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())

    locationManager.delegate=self

    eaccessory = EAAccessoryManager.sharedAccessoryManager()

    locationManager.desiredAccuracy=accuracy
    locationManager.requestWhenInUseAuthorization()
    locationManager.allowsBackgroundLocationUpdates=true
    locationManager.pausesLocationUpdatesAutomatically=false
    locationManager.startUpdatingLocation()


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



//CoreBluetooth methods
func centralManagerDidUpdateState(central: CBCentralManager)

    if (central.state == CBCentralManagerState.PoweredOn)
    
        //self.centralManager?.retrieveConnectedPeripheralsWithServices
    
    else
    
        // do something like alert the user that ble is not on
    


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

    allPeripherals.append(peripheral)



如果要列出 BLE 设备,则需要使用 CoreBluetooth 框架和方法retrieveConnectedPeripheralsWithServices,该方法将接收 CBUUID 数组。这些数组必须填充您要发现的服务 UUID。其中一些在此处列出:https://www.bluetooth.com/specifications/gatt/services

【讨论】:

哦,是的,抱歉,我错过了您原始问题中有关蓝牙经典的部分。我的回答只针对BTLE

以上是关于我需要将哪些服务UUID 传递给retrieveConnectedPeripheralsWithServices 以快速获取任何连接的设备?的主要内容,如果未能解决你的问题,请参考以下文章

通过子程序将一组变量值传递给没有公共块的函数都有哪些方法?

连接BLE设备后,我怎么知道都有哪些服务可用,我只知道打印uuid但我想知道服务的名称

将哪些 Java 属性传递给 Java 应用程序以使用 http 代理进行身份验证

将 threadpoolexecutor 传递给 CompletableFuture 的方法都有哪些?

哪些 GATT 服务使用具有相同 UUID 的多个特征?

我必须向NNTP提供哪些标题?