ios - Swift BLE "didDiscoverServices" not executing. Am I missing something? -
i've been reading tutorial use nrf8001 adafruit , connecting ios device via arduino. far i've set correctly , works fine in app.
i'm trying write own app (for now) exact same thing, read tutorial , understand copied code close could, far can connection , things seem working (ui-wise), can't seem past connecting device:
here's code after connecting:
func centralmanager(_ central: cbcentralmanager, diddiscover peripheral: cbperipheral, advertisementdata: [string : any], rssi rssi: nsnumber) { //what when discovers peripheral, add array list print("peripheral found: " + (peripheral.name ?? "unknown name")) peripheralsfoundnames.append((peripheral.name ?? "unknown name")) peripheralsfounddata.append((advertisementdata.description )) peripheralsfoundcb.append(peripheral) peripheralsfoundrssis.append(rssi) } func centralmanager(_ central: cbcentralmanager, didconnect peripheral: cbperipheral) { print("connected device!") displaystatusalert(localmsg: "connection succesful!") notificationcenter.default.post(name: notification.name(rawvalue: device_ready_key), object: self) data?.length = 0 //clear data might stored peripheral.discoverservices([bletemperatureservice]) print("here @ didconnect, connected to:" + peripheral.name!) // here needs add code check if it's single or multi-channel device via advertisement data or other constant, maybe name? }
as can see, calling explicitly peripheral.discoverservices, , have print statement executes. have following (note none of below lines seem execute @ time (at least not print statements):
func peripheral(_ peripheral: cbperipheral, diddiscoverservices error: error?) { print("here @ diddisoverservices") if ((error) != nil){ displaystatusalert(localmsg: "error: \n" + (error?.localizeddescription ?? "error unknown" )) } guard let services = peripheral.services else{ return } service in services { peripheral.discovercharacteristics(nil, for: service) } print ("discovered!") } func peripheral(_ peripheral: cbperipheral, diddiscovercharacteristicsfor service: cbservice, error: error?) { if ((error) != nil){ displaystatusalert(localmsg: "error: \n" + (error?.localizeddescription ?? "error unknown" )) } guard let characteristics = service.characteristics else{ return } characteristic in characteristics { //looks right characteristic print("looking characteristic") if characteristic.uuid.isequal(blerxcharacteristic) { deviceconnectedrxchar = characteristic //once found, subscribe particular characteristic peripheral.setnotifyvalue(true, for: deviceconnectedrxchar!) peripheral.readvalue(for: characteristic) print("rx characteristic: \(characteristic.uuid)") } if characteristic.uuid.isequal(bletxcharacteristic){ deviceconnectedtxchar = characteristic print("tx characteristic: \(characteristic.uuid)") } peripheral.discoverdescriptors(for: characteristic) } print ("characteristic discovered") } func peripheral(_ peripheral: cbperipheral, didupdatevaluefor characteristic: cbcharacteristic, error: error?) { if characteristic == deviceconnectedrxchar { if let asciistring = nsstring(data: characteristic.value!, encoding: string.encoding.utf8.rawvalue) { receiveddatastring = asciistring print("value recieved: \((receiveddatastring string))") notificationcenter.default.post(name: notification.name(rawvalue: device_sent_data), object: nil) } }
func peripheral(_ peripheral: cbperipheral, diddiscoverservices error: error?)
cbperipheraldelegate
method.
so missing setting cbperipheral
object delegate
.
so before doing peripheral.discoverservices([bletemperatureservice])
, need peripheral.delegate = self
.
Comments
Post a Comment