簡體   English   中英

無法從外圍設備發現藍牙服務

[英]Cant discover bluetooth services from peripheral

構建一個應用程序以連接到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以發現其擁有的所有服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM