ios - setting up iBeacon on two devices Xcode -
ios - setting up iBeacon on two devices Xcode -
i experimenting game app uses ibeacon notify user person wants connect them play 1 of 8 games. have created peripheral manager , various regions (one each game using unique uuid , region.identifier. since using same view controller both transmit ibeacon info , receive have user tap button either set device broadcast specific game beacon info or tap button receive specific game beacon info. here code;
//this sets transmitter
-(void) configuretransmitter { // received signal strength indicator (rssi) value (measured in decibels) device. value represents measured strength of beacon 1 meter away , used during ranging. specify nil utilize default value device. nsnumber * powerfulness = [nsnumber numberwithint:-63]; self.peripheraldata = [self.beaconregion peripheraldatawithmeasuredpower:power]; // global dispatch queue. dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); // create peripheral manager. self.peripheralmanager = [[cbperipheralmanager alloc] initwithdelegate:self queue:queue]; } -(void)peripheralmanagerdidupdatestate:(cbperipheralmanager *)peripheral { // peripheral active, means bluetooth adapter can start advertising. if (peripheral.state == cbperipheralmanagerstatepoweredon) { return; } }
//this creates beacon transmitter
nsuuid * uid = [[nsuuid alloc] initwithuuidstring:uuidstring]; self.beaconregion = [[clbeaconregion alloc] initwithproximityuuid:uid identifier:identifierstring];
//these button actions configure beacon , have peripheralmanager start advertising info. each have own uuid , identifier.
-(ibaction)checkersinvite:(id)sender;{ self.invitefield.text = @"com.checkers.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"3b257014-2c29-40e0-8e1e-1d7a9e5d0964"; [self configuretransmitter]; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitecheckers setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)chessinvite:(id)sender;{ self.invitefield.text = @"com.chess.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"0f45fe1a-c4b7-4772-936c-271adb388367"; [self configuretransmitter]; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitechess setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)backgammoninvite:(id)sender;{ self.invitefield.text = @"com.backgammon.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"23f10764-f8ff-4da2-8577-f639d2b11dfa"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitebackgammon setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)starbattleinvite:(id)sender;{ self.invitefield.text = @"com.starbattle.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"29ca193c-aa40-4984-a22d-02797fa84583"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitestarbattle setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)mankalainvite:(id)sender;{ self.invitefield.text = @"com.mankala.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"83fdd95f-dada-4593-bd0c-5275f71a2108"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitemankala setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)capturevinvite:(id)sender;{ self.invitefield.text = @"com.capturev.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"d1e35df5-1615-4919-826b-1a62302b38d7"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitecapturev setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)penteinvite:(id)sender;{ self.invitefield.text = @"com.pente.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"add6255f-c170-4ebd-b534-2c2d3bc39f7e"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitepente setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(ibaction)linkivinvite:(id)sender;{ self.invitefield.text = @"com.linkiv.bluetooth"; identifierstring = invitefield.text; self.uuidstring = @"880ba4cb-1e01-4138-904a-db40f86f0549"; [self.peripheralmanager startadvertising:self.peripheraldata]; timer = [nstimer scheduledtimerwithtimeinterval:10 target:self selector:@selector(changeuuid:) userinfo:nil repeats:no]; [self.invitelinkiv setbackgroundimage:[uiimage imagenamed:@"accept.png"] forstate:uicontrolstatenormal]; } -(void)changeuuid:(nstimer *)timer{ self.uuidstring = @"9f0ed9d4-c40a-4bfe-a8b3-c183365d8303"; [self.peripheralmanager stopadvertising]; }
//i included timer reset info user can send out phone call if no 1 responds.
//then set receiver beacons each correspond game info beingness sent.
nsuuid * uid2 = [[nsuuid alloc] initwithuuidstring:@"3b257014-2c29-40e0-8e1e-1d7a9e5d0964"]; self.beaconregion2 = [[clbeaconregion alloc] initwithproximityuuid:uid2 identifier:@"com.checkers.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion2 setnotifyentrystateondisplay:yes]; [self.beaconregion2 setnotifyonentry:yes]; [self.beaconregion2 setnotifyonexit:yes]; nsuuid * uid3 = [[nsuuid alloc] initwithuuidstring:@"0f45fe1a-c4b7-4772-936c-271adb388367"]; self.beaconregion3 = [[clbeaconregion alloc] initwithproximityuuid:uid3 identifier:@"com.chess.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion3 setnotifyentrystateondisplay:yes]; [self.beaconregion3 setnotifyonentry:yes]; [self.beaconregion3 setnotifyonexit:yes]; nsuuid * uid4 = [[nsuuid alloc] initwithuuidstring:@"23f10764-f8ff-4da2-8577-f639d2b11dfa"]; self.beaconregion4 = [[clbeaconregion alloc] initwithproximityuuid:uid4 identifier:@"com.backgammon.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion4 setnotifyentrystateondisplay:yes]; [self.beaconregion4 setnotifyonentry:yes]; [self.beaconregion4 setnotifyonexit:yes]; nsuuid * uid5 = [[nsuuid alloc] initwithuuidstring:@"29ca193c-aa40-4984-a22d-02797fa84583"]; self.beaconregion5 = [[clbeaconregion alloc] initwithproximityuuid:uid5 identifier:@"com.starbattle.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion5 setnotifyentrystateondisplay:yes]; [self.beaconregion5 setnotifyonentry:yes]; [self.beaconregion5 setnotifyonexit:yes]; nsuuid * uid6 = [[nsuuid alloc] initwithuuidstring:@"83fdd95f-dada-4593-bd0c-5275f71a2108"]; self.beaconregion6 = [[clbeaconregion alloc] initwithproximityuuid:uid6 identifier:@"com.mankala.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion6 setnotifyentrystateondisplay:yes]; [self.beaconregion6 setnotifyonentry:yes]; [self.beaconregion6 setnotifyonexit:yes]; nsuuid * uid7 = [[nsuuid alloc] initwithuuidstring:@"d1e35df5-1615-4919-826b-1a62302b38d7"]; self.beaconregion7 = [[clbeaconregion alloc] initwithproximityuuid:uid7 identifier:@"com.capturev.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion7 setnotifyentrystateondisplay:yes]; [self.beaconregion7 setnotifyonentry:yes]; [self.beaconregion7 setnotifyonexit:yes]; nsuuid * uid8 = [[nsuuid alloc] initwithuuidstring:@"add6255f-c170-4ebd-b534-2c2d3bc39f7e"]; self.beaconregion8 = [[clbeaconregion alloc] initwithproximityuuid:uid8 identifier:@"com.pente.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion8 setnotifyentrystateondisplay:yes]; [self.beaconregion8 setnotifyonentry:yes]; [self.beaconregion8 setnotifyonexit:yes]; nsuuid * uid9 = [[nsuuid alloc] initwithuuidstring:@"880ba4cb-1e01-4138-904a-db40f86f0549"]; self.beaconregion9 = [[clbeaconregion alloc] initwithproximityuuid:uid9 identifier:@"com.linkiv.bluetooth"]; // when set yes, location manager sends beacon notifications when user turns on display , device within region. [self.beaconregion9 setnotifyentrystateondisplay:yes]; [self.beaconregion9 setnotifyonentry:yes]; [self.beaconregion9 setnotifyonexit:yes]; }
//then configure receivers each beacon
-(void) configurereceiver { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion2]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion2]; } -(void) configurereceiver2 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion3]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion3]; } -(void) configurereceiver3 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion4]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion4]; } -(void) configurereceiver4 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion5]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion5]; } -(void) configurereceiver5 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion6]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion6]; } -(void) configurereceiver6 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion7]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion7]; } -(void) configurereceiver7 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion8]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion8]; } -(void) configurereceiver8 { // location manager. self.locationmanager = [[cllocationmanager alloc] init]; self.locationmanager.delegate = self; [self.locationmanager startmonitoringforregion:self.beaconregion9]; [self.locationmanager startrangingbeaconsinregion:self.beaconregion9]; }
//and these button actions loading of receivers
-(ibaction)reciever:(id)sender;{ [self configurereceiver]; } -(ibaction)reciever2:(id)sender;{ [self configurereceiver2]; } -(ibaction)reciever3:(id)sender;{ [self configurereceiver3]; } -(ibaction)reciever4:(id)sender;{ [self configurereceiver4]; } -(ibaction)reciever5:(id)sender;{ [self configurereceiver5]; } -(ibaction)reciever6:(id)sender;{ [self configurereceiver6]; } -(ibaction)reciever7:(id)sender;{ [self configurereceiver7]; } -(ibaction)reciever8:(id)sender;{ [self configurereceiver8]; }
//and here hand off info launch notification app delegate
-(void)locationmanager:(cllocationmanager *)manager didenterregion:(clregion *)region { // see if we've entered region. if ([region.identifier isequaltostring:@"com.checkers.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play checkers?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.chess.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play chess?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.backgammon.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play backgammon?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.starbattle.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play star battle?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.mankala.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play mankala?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.capturev.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play capture v?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.pente.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play pente?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } if ([region.identifier isequaltostring:@"com.linkiv.bluetooth"]) { uilocalnotification * notification = [[uilocalnotification alloc] init]; notification.alertbody = @"want play link iv?"; [[uiapplication sharedapplication] presentlocalnotificationnow:notification]; } }
so summerize
one user opens app , taps on receive checkers button configuring beacon receiver allow them know if user asking if wants play checkers.
another user opens app , taps on transmitter button invite play checkers.
when happens user 1 should notification on screen asking if want play checkers.
however not happen. happens when user 2 taps twice on checkers button , 1 time on chess button once.
this true games.
i i'll receive requests backgammon. press backgammon button twice , chess button . receive notification asking if want play backgammon
can @ code , allow me know going on here.
ios xcode5 bluetooth-lowenergy ibeacon nsnotifications
Comments
Post a Comment