Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从ios中的可扩展设备获取数据/服务_Ios_Swift_Bluetooth Lowenergy_Core Bluetooth - Fatal编程技术网

无法从ios中的可扩展设备获取数据/服务

无法从ios中的可扩展设备获取数据/服务,ios,swift,bluetooth-lowenergy,core-bluetooth,Ios,Swift,Bluetooth Lowenergy,Core Bluetooth,我需要连接一个BLE设备,然后根据通过其中不同按钮发送的数据处理数据。 为此,我编写了以下代码 import CoreBluetooth class HomeViewController: UIViewController,CBPeripheralDelegate,CBCentralManagerDelegate { var centralManager : CBCentralManager! var peri : CBPeripheral! override func

我需要连接一个BLE设备,然后根据通过其中不同按钮发送的数据处理数据。
为此,我编写了以下代码

import CoreBluetooth
class HomeViewController: UIViewController,CBPeripheralDelegate,CBCentralManagerDelegate 
{
    var centralManager : CBCentralManager!
    var peri : CBPeripheral!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }
    func centralManagerDidUpdateState(central: CBCentralManager) {
        if central.state == .Unknown
        {
            print("Unknown")
        }
        else if central.state == .Unsupported
        {
            print("Unsupported")
        }
        else if central.state == .Unauthorized
        {
            print("Unauthorized")
        }
        else if central.state == .Resetting
        {
            print("Resetting")
        }
        else if central.state == .PoweredOn
        {
            print("Powered On")
            startScan()
        }
        else if central.state == .PoweredOff
        {
            print("Powered Off")
        }
    }
    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        print("Discovered: \(peripheral.name) at \(RSSI)")
        print("AdvertisementData:\(advertisementData)")

        if peri != peripheral
        {
            peri = peripheral
            centralManager.connectPeripheral(peripheral, options: nil)
        }
    }
    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        print("Failed to connect \(peripheral) cause of \(error)")
    }

    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("connected to \(peripheral)")
//        centralManager.stopScan()
        print("Available services:\(peripheral.services)")
    }
    func peripheral(peripheral: CBPeripheral, didDiscoverIncludedServicesForService service: CBService, error: NSError?) {
        print("Services\(service) and error\(error)")
    }
    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
        print("Services and error\(error)")
    }
    func startScan(){
        print("Scanning...")
        centralManager.scanForPeripheralsWithServices(nil, options: nil)
    }
}
这是我的代码日志

Powered On
Scanning...
Discovered: Optional("**** BLE") at 127
AdvertisementData:["kCBAdvDataIsConnectable": 1, "kCBAdvDataServiceUUIDs": (
    1802
)]
connected to <CBPeripheral: 0x12756d910, identifier = 6197****-EB0A-F1E8-BEF4-1AFAC629C5BC, name = **** BLE, state = connected>
Available services:nil
已通电
扫描。。。
发现:127处的可选(*****BLE)
广告数据:[“KCBADVDATA可连接”:1,“kCBAdvDataServiceUUIDs”:(
1802
)]
连接到
可用服务:无
这是当从可编程设备单击一个按钮时生成的输出。但当单击另一个按钮时,我无法接收或读取数据。
同一应用程序的Android开发者已与这两个按钮集成。
因此,设备中没有任何问题。

有谁能帮我指出我在这段代码中的错误所在吗???

连接到外围设备后,你必须使用你想要发现的服务的UUID在外围设备上调用
discoverServices
,然后你必须发现服务的特征。如果您希望在单击按钮时更新,则必须打开与该按钮对应的特性的通知

如果你还需要帮助,我强烈推荐苹果公司的这个链接作为后续阅读。它以一种比我在这里描述的更好的方式一步一步地描述了你需要做的事情


连接到外围设备后,您必须使用要查找的服务的UUID在外围设备上调用
discoverServices
,然后您必须查找服务的特征。如果您希望在单击按钮时更新,则必须打开与该按钮对应的特性的通知

如果你还需要帮助,我强烈推荐苹果公司的这个链接作为后续阅读。它以一种比我在这里描述的更好的方式一步一步地描述了你需要做的事情


您还必须在连接到设备后发现服务和特性

例如,在“DidConnectPeripal”方法中,您必须执行以下操作:

 func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    print("connected to \(peripheral)")
    peripheral.delegate = self
    peripheral.discoverServices(nil)
    print("Discovering services!")
}
然后:

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    print("Discovered services: \(peripheral.services), Error\(error)")
    if let services = peripheral.services {
        for service in services {
            peripheral.discoverCharacteristics(nil, forService: service)
        }
    }
}
然后你必须处理每个特征:

 func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError)
您必须记住手动存储每个特征,因为如果不存储,它们将被释放

为了接收流数据(通知),您必须为每个特征启用notify

peripheral.setNotifyValue(true, forCharacteristic: characteristic)
您还必须实施:

 func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError)
以处理传入值


如您所见,开始使用时需要大量的锅炉板代码。

您还必须在连接到设备后发现服务和特性

例如,在“DidConnectPeripal”方法中,您必须执行以下操作:

 func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    print("connected to \(peripheral)")
    peripheral.delegate = self
    peripheral.discoverServices(nil)
    print("Discovering services!")
}
然后:

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    print("Discovered services: \(peripheral.services), Error\(error)")
    if let services = peripheral.services {
        for service in services {
            peripheral.discoverCharacteristics(nil, forService: service)
        }
    }
}
然后你必须处理每个特征:

 func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError)
您必须记住手动存储每个特征,因为如果不存储,它们将被释放

为了接收流数据(通知),您必须为每个特征启用notify

peripheral.setNotifyValue(true, forCharacteristic: characteristic)
您还必须实施:

 func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError)
以处理传入值


正如您所看到的,开始使用锅炉板需要相当多的代码。

Pandafox
的答案是完美的,只是缺少一点。
设置外围设备的委托

以下是发现外围设备、连接到外围设备以及发现其服务和特性的完整代码

1.连接外围设备

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    print("Discovered: \(peripheral.name) at \(RSSI)")
    print("AdvertisementData:\(advertisementData)")

    if peri != peripheral
    {
        peri = peripheral
        peri.delegate = self
        centralManager.connectPeripheral(peri, options: nil)
    }
}
  • 连接失败或成功

    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        print("Failed to connect \(peripheral) cause of \(error)")
    }
    
    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("connected to \(peripheral)")
    //        centralManager.stopScan()
        peripheral.discoverServices(nil)
    }
    
  • 3.发现服务

    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
            print("Services:\(peripheral.services) and error\(error)")
            if let services = peripheral.services {
                for service in services {
                    peripheral.discoverCharacteristics(nil, forService: service)
                }
            }
        }
    
  • 发现特征并设置通知

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
        {
            print("peripheral:\(peripheral) and service:\(service)")
            for characteristic in service.characteristics!
            {
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
            }
        }
    
  • 处理特性更新值的通知

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
        {
            print("characteristic changed:\(characteristic)")
        }
    

  • Pandafox的答案是完美的,只是缺少一点。
    设置外围设备的委托

    以下是发现外围设备、连接到外围设备以及发现其服务和特性的完整代码

    1.连接外围设备

    func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
        print("Discovered: \(peripheral.name) at \(RSSI)")
        print("AdvertisementData:\(advertisementData)")
    
        if peri != peripheral
        {
            peri = peripheral
            peri.delegate = self
            centralManager.connectPeripheral(peri, options: nil)
        }
    }
    
  • 连接失败或成功

    func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
        print("Failed to connect \(peripheral) cause of \(error)")
    }
    
    func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
        print("connected to \(peripheral)")
    //        centralManager.stopScan()
        peripheral.discoverServices(nil)
    }
    
  • 3.发现服务

    func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
            print("Services:\(peripheral.services) and error\(error)")
            if let services = peripheral.services {
                for service in services {
                    peripheral.discoverCharacteristics(nil, forService: service)
                }
            }
        }
    
  • 发现特征并设置通知

    func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
        {
            print("peripheral:\(peripheral) and service:\(service)")
            for characteristic in service.characteristics!
            {
                peripheral.setNotifyValue(true, forCharacteristic: characteristic)
            }
        }
    
  • 处理特性更新值的通知

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
        {
            print("characteristic changed:\(characteristic)")
        }
    

  • 谢谢您的回复,我错过了打外设上的
    discoverServices
    。但在更新后,仍然没有在LogScreen上获得任何更新感谢您的回复,我错过了在外围设备上致电
    discoverServices
    。但是在更新之后,仍然没有得到任何关于logScreen的更新。按照您的建议更新了代码,但是仍然没有任何关于logScreen的更新。仍然无法从外围设备获取任何服务。您还必须记住设置外围设备委托。我更新了“didConnectPeripal”方法并添加了“peripal.delegate=self”。查看这是否会使您获得更多的功能。func peripal(peripal:cbperipal,didiscovercharacteristicsforservice:CBService,error:NSError)此方法不调用。请帮助我。根据您的建议更新了代码,但日志屏幕上仍然没有任何更新。仍然无法从外围设备获取任何服务。您还必须记住设置外围设备委托。我更新了“didConnectPeripal”方法并添加了“peripal.delegate=self”。查看这是否会使您获得更多的功能。func peripal(peripal:cbperipal,didiscovercharacteristicsforservice:CBService,error:NSError)此方法不调用。请帮帮我,刚刚抓住了这个问题,修正了我的答案。哦,好吧,我刚刚抓住了这个问题,修正了我的答案。哦,好吧。