没有从ios中的BLE设备获取数据/服务

Mru*_*ank 1 ios core-bluetooth bluetooth-lowenergy swift

我需要连接一个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)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码日志.

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
Run Code Online (Sandbox Code Playgroud)

从BLE设备单击一个按钮时会生成此输出.但是,当单击另一个按钮时,我无法接收或读取数据.
同一个应用程序的Android开发人员已与两个按钮集成.
所以设备没有任何问题.
任何人都可以帮我指导我在这个代码中出错的地方吗?

Vip*_* L. 7

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)
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 连接失败或成功

    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)
    }
    
    Run Code Online (Sandbox Code Playgroud)

3.DiscoverServices

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)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)
  1. 发现特征并设置通知

    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)
            }
        }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 处理特征更新值的通知

    func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
        {
            print("characteristic changed:\(characteristic)")
        }
    
    Run Code Online (Sandbox Code Playgroud)