osx - How to create an event loop in Objective C? -
i'm trying use corebluetooth
module list detected bluetooth devices in command-line osx application.
what have looks this, far:
@import corebluetooth; @interface mycentralmanager : nsobject<cbcentralmanagerdelegate> - (void) centralmanagerdidupdatestate: (cbcentralmanager *) central; - (void) centralmanager:(cbcentralmanager *) central diddiscoverperipheral:(cbperipheral *) peripheral advertisementdata:(nsdictionary *) advertisementdata rssi:(nsnumber *)rssi; @end @implementation mycentralmanager - (void) centralmanagerdidupdatestate: (cbcentralmanager *) central { nslog(@"state changed..."); } - (void) centralmanager:(cbcentralmanager *) central diddiscoverperipheral:(cbperipheral *) peripheral advertisementdata:(nsdictionary *) advertisementdata rssi:(nsnumber *)rssi { nslog(@"discovered %@", peripheral.name); } @end int main() { mycentralmanager* mycentralmanager = [[mycentralmanager alloc] init]; cbcentralmanager* cbcentralmanager = [[cbcentralmanager alloc] initwithdelegate:mycentralmanager queue:nil options:nil]; nslog(@"scanning devices !"); [cbcentralmanager scanforperipheralswithservices:nil options:nil]; sleep(5); // wait 5 seconds before stopping scan. [cbcentralmanager stopscan]; nslog(@"scanning devices ended."); return 0; }
now doesn't work @ never "state changed..."
nor "discovered ..."
log output.
i never written objective c application before i'm missing obvious. if had guess i'm doing wrong assume that:
- i have wait
centralmanager
in appropriate state before starting scan. - i never inside state changed delegate method assume first mistake is: instead of
sleep()
'ing, i have run event loop of sort underlying system has chance notify me of state change.
i'm stuck @ point: don't have gui, nor want 1 couldn't figure out way run event loop (assuming that's missing). how can ?
as said, first attempt objective c, don't afraid state obvious.
simply run thread's run loop
[[nsrunloop currentrunloop] rununtildate:[nsdate datewithtimeintervalsincenow:5]];
more information here.
btw: not have declare methods declared in protocol.
Comments
Post a Comment