OK, I'm using Core Bluetooth to connect my i-Pad to another BLE sensor.
I scan and detect it easily but when I want to establish a connection, the "did connect peripheral" method is only called during 30 sec as if I've called a disconnection...
And this result stay the same even I select or deselect "App Communicates using CoreBluetooth" in Required background mode.
- (id) init
{
self = [super init];
if (self)
{
pendingInit = YES;
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
foundPeripherals = [[NSMutableArray alloc] init];
connectedServices = [[NSMutableArray alloc] init];
}
return self;
}
- (void) centralManagerDidUpdateState:(CBCentralManager *)central
{
static CBCentralManagerState previousState = -1;
switch ([centralManager state])
{
case CBCentralManagerStateUnsupported:
{
NSLog(#"NSCentralManager State Unsupported...");
break;
}
case CBCentralManagerStatePoweredOff:
{
NSLog(#"NSCentralManager State Powered OFF");
[self clearDevices];
[discoverPeripheralDelegate discoveryDidRefresh];
if (previousState != -1)
{
[discoverPeripheralDelegate discoveryStatePoweredOff];
}
break;
}
case CBCentralManagerStateUnauthorized:
{
NSLog(#"NSCentralManager State Unauthorized: the application is not allowed");
break;
}
case CBCentralManagerStateUnknown:
{
NSLog(#"NSCentralManager state unknown... Bad News...");
break;
}
case CBCentralManagerStatePoweredOn:
{
NSLog(#"NSCentralManager State Powered ON!");
pendingInit = NO;
[self loadSavedDevices];
[centralManager retrieveConnectedPeripherals];
[discoverPeripheralDelegate discoveryDidRefresh];
break;
}
case CBCentralManagerStateResetting:
{
NSLog(#"CBCentralManager State Resetting");
[self clearDevices];
[discoverPeripheralDelegate discoveryDidRefresh];
[peripheralDelegate alarmServiceDidReset];
pendingInit = YES;
break;
}
}
previousState = [centralManager state];
}
- (void) loadSavedDevices
{
storedDevices = [[NSArray alloc] initWithObjects:kPressureServiceUUIDString, nil];
for (id deviceUUIDString in storedDevices) {
if (![deviceUUIDString isKindOfClass:[NSString class]])
continue;
CFUUIDRef uuid = CFUUIDCreateFromString(NULL, (CFStringRef)deviceUUIDString);
if (!uuid)
continue;
storedUUIDs = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"StoredUUIDS"]];
for (int i = 0; i < storedUUIDs.count; i++) {
NSArray * tempStoredUUIDs = storedUUIDs[i];
storedUUIDs[i] = [tempStoredUUIDs mutableCopy];
}
[centralManager scanForPeripheralsWithServices:nil options:nil];
storedUUIDs = [[NSMutableArray alloc] init];
[[NSUserDefaults standardUserDefaults] setObject:storedUUIDs forKey:#"StoredUUIDS"];
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString * finaluuids = [NSString stringWithFormat:#"%#",peripheral.UUID];
if (storedUUIDs.count == 0)
{
storedUUIDs = [[NSMutableArray alloc] initWithObjects:finaluuids, nil];
[centralManager stopScan];
[centralManager connectPeripheral:peripheral options:nil];
}
}
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(#"connected");
}
Here is my console. I don't have any warning, any error or deallocating just:
2013-05-14 09:21:14.925 PRESSURE[262:907] connected
2013-05-14 09:21:14.927 PRESSURE[262:907] Service (Sensor) connected
2013-05-14 09:21:44.994 PRESSURE[262:907] Service (Sensor) disconnected
How can I avoid my peripheral disconnected immediately after connection please?
It drives me crazy!
You have to do [peripheral retain] on didDiscoverPeripheral or put it into the retain property.
It is released right after the connect by BLE framework.
Related
This is what is happening....
I am making a game where by the end of the challenge a customized UIAlert shows saying if the player wins or loses. The problem is... that message is displaying twice and I have no idea why....
-(void)checkOnTests{
int p1sc;
int p1t;
int p2sc;
int p2t;
// IN BETWEEN, SCORES ARE CHECKED AND THEY ARE FINE
if (p1sc > p2sc) {
NSLog(#"P1 Wins");
winnerIndex = 1; // (INT)
if (playerIndex == winnerIndex) {
self.lbl_plyer_score.textColor = [UIColor greenColor]; //WINS
self.lbl_opponent_score.textColor = [UIColor redColor]; //LOSES
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else{
self.lbl_plyer_score.textColor = [UIColor redColor];
self.lbl_opponent_score.textColor = [UIColor greenColor];
[self finishChallengeWherePlayer:NO amtOfGold:0];
}
}else if (p1sc < p2sc){
winnerIndex = 2;
NSLog(#"P2 Wins");
if (playerIndex == winnerIndex) {
self.lbl_plyer_score.textColor = [UIColor greenColor];
self.lbl_opponent_score.textColor = [UIColor redColor];
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else{
self.lbl_plyer_score.textColor = [UIColor redColor];
self.lbl_opponent_score.textColor = [UIColor greenColor];
[self finishChallengeWherePlayer:NO amtOfGold:0];
}
}else if (p1sc == p2sc){
NSLog(#"We got a tie match");
winnerIndex = 3;
if (((p1t == 0) && (p2t == 0)) || (p1t == p2t)) {
[self finishChallengeWherePlayer:YES amtOfGold:1];
}else if (p1t < p2t){
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else if (p1t > p2t){
[self finishChallengeWherePlayer:YES amtOfGold:1];
}
}
}
The code above checks on results. So far everything is correct. No errors.
Below I implement the code where the message displays to the player telling if they win or lose the challenge.
-(void)finishChallengeWherePlayer:(BOOL)wins amtOfGold:(int)amt {
NSString *endMsg;
if (wins) {
endMsg = [NSString stringWithFormat:#"Congrats! You win the challenge, gaining: %i gold", amt];
}else{
endMsg = [NSString stringWithFormat:#"Oh No! You have lost the challenge. No gold for you"];
}
GameAlert *alert = [[GameAlert alloc] initWithTitle:#"Results" message:endMsg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show]; // this is my custom UIAlertView that is working fine throughout the game.
[self.delegate challengeFinished:amt testname:linker];
}
The only thing that This view controller is doing differently from the rest of the game is in the box below:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self dismissViewControllerAnimated:YES completion:nil];
}
My question is... Do I really have to tell the delegate that the challenge is over only after i dismiss this ViewController or am I doing something wrong here ? The alert is showing twice and I feel like a fool :(
In my code by default work UITapGesture how to remove this
NSString *dirString;
host is class object
-(Void) handleSwipeEvent : (UISwipeGestureRecognizer *) recognizer {
//NSString *swipeDirection = NULL;
//CGPoint location = [recognizer locationInView:self];
dirString = NULL;
UITapGestureRecognizer *tap=nil;
tap.enabled=NO;
if ([recognizer direction] == UISwipeGestureRecognizerDirectionLeft) {
dirString = kCATransitionFromRight;
}
if ([recognizer direction] == UISwipeGestureRecognizerDirectionRight)
{
dirString = kCATransitionFromLeft;
}
if (dirString) [host swipeTo:dirString];
}
-(void)createSwipeGestureRecognizer:(UIView *)view direction: (UISwipeGestureRecognizerDirection)direction
{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeEvent:)];
[view addGestureRecognizer:recognizer];
[recognizer setDirection:direction];
[self setSwipeRecognizer:recognizer];
[recognizer release];
}
- (void) setHost: (FlashCardViewController *) aHost {
host = aHost;
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionLeft];
[self createSwipeGestureRecognizer:self direction:UISwipeGestureRecognizerDirectionRight];
}
On the view i have to touch then load next contant.
How can i disable tap event
I have a UITable, and I like to add an image in the detail view for a cell. I can handle the selection for any image from the camera roll or from the camera:
cell.imageView.image = someImage;
But how do I define a specific image - in the case above: "someImage", so that the next time the app is run, the correct image is shown for each item.
UPDATE. This is the code I'm using to snap/select an image..
- (IBAction)btnTakePicture_Clicked:(id)sender
{
NSLog(#"%s", __FUNCTION__);
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..." delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Image Gallary", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%s", __FUNCTION__);
switch (actionSheet.tag)
{
case 1:
switch (buttonIndex)
{
case 0:
{
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Ooops" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
//picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
#endif
}
break;
case 1:
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
break;
}
break;
default:
break;
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(#"%s", __FUNCTION__);
dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
imgPicture.image = [[UIImage alloc] initWithData:dataImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
UPDATE 2. With the help from people below, I think this solution will work for me:
- (IBAction)photoLibraryAction:(id)sender
{
int c = self.capturedImages.count;
for (int i=0; i < c; i++ ){
if (self.imageView.tag == cellTag) {
NSLog(#"found it");
} else {
NSLog(#"can't find it");
}
}
}
if ([self.capturedImages count] == 1)
{
// we took a single shot
[self.imageView setImage:[self.capturedImages objectAtIndex:0]];
[self.imageView setTag:myTag];
}
I would do in this way.
#interface CustomClassCell : UITableViewCell
#property (unsafe_unretained, nonatomic) IBOutlet UIImageView *HomePicture;
#end
#interface CustomClass : NSObject
#property (nonatomic, copy) NSString *PicPath;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomClassCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomClassCellIdentifer"];
id object = [self.tableData objectAtIndex:indexPath.row];
CustomClass *myObject = (CustomClass*)object;
cell.HomePicture.image = [UIImage imageNamed:myObject.PicPath];
return cell;
}
Want to check whether EKEventStore permission exist on ios6 device.
EKEventStore *eventStore = [[UpdateManager sharedUpdateManager] eventStore];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
addEventBlock();
}
else
{
NSLog(#"Not granted");
}
}];
}
This code works only at the first time.
Use like this.
- (IBAction)btnAddFromDBTouched:(id)sender {
void (^addEventBlock)();
addEventBlock = ^
{
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
NSDate *startDate = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400*365];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
calendars:calendarArray];
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
nmfChooseEvent *myView = [[nmfChooseEvent alloc] init];
[myView setTableSource:events];
[self presentModalViewController:myView animated:YES];
myView.mainView=self;
return;
};
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
addEventBlock();
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *myView = [[UIAlertView alloc] initWithTitle:#"warning" message:#"To be Able to Use This Function, You Must Authorize xxx in Settings>>Privacy>>Calendars Section." delegate:nil cancelButtonTitle:"OK" otherButtonTitles:nil, nil];
[myView show];
});
}
}];
}
else
{
addEventBlock();
}
}
I have a requirement to adding a path in my MKMapview between two annotation pin. There is no issue with ios 5 or older but when i try to run app in ios 6 app is surprisingly quit. below is my code. If there is any correction in my code then please suggest me.
1.So my question is suggest the best way how to decrees memory in my following code so that i can solve crash in my Application.
- (void)viewDidLoad
{
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoints:pathArray mapView:_mapView];
[_mapView addAnnotation:annotation];
[annotation release];
[pathArray removeAllObjects];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%s",__FUNCTION__);
if ([annotation isKindOfClass:[NVPolylineAnnotation class]])
{
NSLog(#"=========ANOTATION=========NVPolylineAnnotationView START");
//ann=[ann initWithAnnotation:annotation mapView:_mapView];
NVPolylineAnnotationView *ann=[[NVPolylineAnnotationView alloc] init];
return [[ann initWithAnnotation:annotation mapView:_mapView] autorelease];//[[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:_mapView] autorelease];
}
else if([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pointers"] ;
// annView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annView.animatesDrop=NO;
annView.canShowCallout = TRUE;
return [annView autorelease];
}
else if([annotation isKindOfClass:[PlacePin class]])
{
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
}
return nil;
}
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(#"%s",__FUNCTION__);
// fixes that some marker are behind the polyline
for (int i=0; i<[views count]; i++)
{
MKAnnotationView *view = [views objectAtIndex:i];
if ([view isKindOfClass:[NVPolylineAnnotationView class]])
{
[[view superview] sendSubviewToBack:view];
/* In iOS version above 4.0 we need to update the polyline view after it
has been added to the mapview and it ready to be displayed. */
NSString *reqSysVer = #"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
[self updatePolylineAnnotationView];
}
}
}
NSLog(#"----------didAddAnnotationViews");
}
- (void)updatePolylineAnnotationView
{
NSLog(#"%s",__FUNCTION__);
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
[mapView selectAnnotation:mp animated:NO];
*/
for (NSObject *a in [_mapView annotations])
{
if ([a isKindOfClass:[NVPolylineAnnotation class]])
{
NVPolylineAnnotation *polyline = (NVPolylineAnnotation *)a;
NSObject *pv = (NSObject *)[_mapView viewForAnnotation:polyline];
if ([pv isKindOfClass:[NVPolylineAnnotationView class]])
{
NVPolylineAnnotationView *polylineView =
(NVPolylineAnnotationView *)[_mapView viewForAnnotation:polyline];
[polylineView regionChanged];
}
}
}
}