【问题标题】:Cant discover bluetooth services from peripheral无法从外围设备发现蓝牙服务
【发布时间】:2016-01-25 00:19:17
【问题描述】:

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




    }

【问题讨论】:

  • 在您通过didConnectPeripheral 委托方法确认您已连接到外围设备之前,请勿调用discoverServices

标签: ios swift bluetooth


【解决方案1】:

您无需先发现服务就直接进行特征发现。在致电discoverCharacteristics 之前,您需要致电discoverServices。发现服务后,将调用peripheral:didDiscoverServices: 委托方法,您可以使用它来触发特征发现。

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

【讨论】:

  • 我试过print(peripheralArray[0].services),结果还是nil?
猜你喜欢
  • 2014-08-27
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多