无法从外设

时间:2016-01-25 00:19:17

标签: ios swift bluetooth

构建一个连接到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)
            }
        }
    }




    }

1 个答案:

答案 0 :(得分:0)

在没有首先发现服务的情况下,您将直接进行特征发现。在致电discoverCharacteristics之前,您需要致电discoverServices。一旦发现服务,将调用peripheral:didDiscoverServices:委托方法,您可以使用它来触发特征发现。

对于discoverServices调用,您可以传递一个您想要发现的CBUUID地址数组(推荐),或者,如果您不知道自己想要哪些服务,则可以通过它nil发现它拥有的所有服务。