无法从外围设备发现蓝牙服务



构建一个应用程序以连接到Arduino蓝牙模块,发现设备很好,但是在发现服务时,它总是零。我对发现服务功能很好,我真的不明白

这是我的代码:

import UIKit
import CoreBluetooth
class Devices: UITableViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
    var devices = [String]()
    var centralManager : CBCentralManager!
    var peripheral: CBPeripheral!
    var central: CBCentralManager!
    var peripheralArray: [CBPeripheral] = []
    var service : CBService!
    var deviceConnected = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.refreshControl = UIRefreshControl()
        self.refreshControl!.addTarget(self, action: "scanForDevices:", forControlEvents: UIControlEvents.ValueChanged)
        tableView.tableFooterView = UIView()
        central = CBCentralManager(delegate: self, queue: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func centralManagerDidUpdateState(central: CBCentralManager) {
        if central.state == CBCentralManagerState.PoweredOn {
            print("Bluetooth On")
        }
        else {
            print("Bluetooth off or not supported")
        }
    }

    func scanForDevices(sender: AnyObject) {
        devices.removeAll()
        print("Scanning")
        central.scanForPeripheralsWithServices(nil, options: nil)
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(2 * NSEC_PER_SEC)), dispatch_get_main_queue()) {
            self.central.stopScan()
            self.refreshControl?.endRefreshing()
            print("Stopped Scanning")
        }

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

        (advertisementData as NSDictionary).objectForKey(CBAdvertisementDataLocalNameKey) as? NSString
        let list = String(peripheral.name!)
        if (devices.contains(list)){
        } else {devices.append(list)
            peripheralArray.append(peripheral)
        }
        tableView.reloadData()

    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return devices.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("device")! as UITableViewCell
        cell.textLabel?.text = devices[indexPath.row]
        return cell
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if(deviceConnected == false){
            self.central.connectPeripheral(peripheralArray[indexPath.row], options: nil)
            deviceConnected = true
        }else{
            self.central.cancelPeripheralConnection(peripheralArray[indexPath.row])
            deviceConnected = false
        }
        discoverServices()
        self.peripheral = peripheralArray[indexPath.row]
        print(self.peripheral.services)

    }

    func discoverServices(){

        for service in peripheral.services! {
            let thisService = service as? CBService
                if let thisService = thisService{
                    peripheral.discoverCharacteristics(nil, forService: thisService)
            }
        }
    }


    }

您将直接进行特征发现,而无需先发现服务。在打电话给discoverCharacteristics之前,你需要打电话给discoverServices。发现服务后,将调用 peripheral:didDiscoverServices: 委托方法,您可以使用此方法触发特征发现。

对于discoverServices调用,您可以向它传递要发现的CBUUID地址数组(推荐),或者,如果您不知道需要哪些服务,则可以将其传递nil以发现它拥有的所有服务。

最新更新