ios - Cant discover bluetooth services from peripheral -


building app connect arduino bluetooth module, finds devices fine when comes discovering services nil. im fine discover services function don't understand

here code:

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 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")! 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(){            service in peripheral.services! {             let thisservice = service as? cbservice                 if let thisservice = thisservice{                     peripheral.discovercharacteristics(nil, forservice: thisservice)             }         }     }         } 

you going straight characteristic discovery without discovering services first. before call discovercharacteristics need call discoverservices. once services have been discovered, peripheral:diddiscoverservices: delegate method called , can use trigger characteristic discovery.

for discoverservices call, can either pass array of cbuuid addresses want discover (recommended) or, if don't know services want, can pass nil discover services has.


Comments