无法从外设发现蓝牙服务

问题描述:

构建一个应用程序以连接到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

在你确认你通过'didConnectPeripheral'委托方法连接到外设之前,不要调用'discoverServices' – Paulw11

你直接去发现特征不首先发现服务。在致电discoverCharacteristics之前,您需要致电discoverServices。一旦发现服务,将调用peripheral:didDiscoverServices:委托方法,您可以使用它来触发特征发现。

对于discoverServices呼叫,您可以通过它,你要发现的CBUUID地址数组(推荐),或者,如果你不知道你想要的服务,您可以通过它nil发现所有服务IT具有。

+0

我已经尝试过'print(peripheralArray [0] .services)'并且结果仍然为零? – user3533049