Disabling notifications from Bluetooth 4.0 devices - iphone

My app connects to a Bluetooth sensor then starts updating the UI based on the notifications sent from said device. I am having a problem with IOS automatically generating a large amount of notification pop-ups when the app is minimized, I think this is due to the frequency at which the sensor is sending data. So I am trying to figure out how to keep the user from being bombarded when they minimize the app. I am trying to tell the device to stop sending data, but I suspect that delegate method never gets called.
I have tried adding
[application cancelAllLocalNotifications]
to both
- (void)applicationDidEnterBackground:(UIApplication *)application
and
- (void)applicationWillResignActive:(UIApplication *)application
but still seem to have an issue, any ideas.
Thanks

You can use CBPerpheral::setNotifyValue:forCharacteristic: to start or stop getting notification from the said peripheral.

Another option is to use session backgrounding. For that you need to add the bluetooth-central backgrounding mode to the app's plist file. After that the app is going to receive the bluetooth communication events both in foreground and background and no notifications will be generated by iOS. If your app decides it needs a notfification, it can simply generate a local notification (tutorial).

Related

Perform a function at a particular time

I am trying to do an app which make a song sing at a time set by the user. The user should set the time by opening the app. Even after the user force kills the app after setting the time, I must be able to make the song sing at the particular time. How do I do this?
You can do this but not without user interaction if your app is not running.
Set Local Notification for time which user set.
At that time your app will be notified by two way
1 If your app is running : didReceiveLocalNotification:(UILocalNotification *)notification
2 If your app in not running : didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
And you can play song from here.
To learn you can visit this :
iPhone Programming Tutorial – Local Notifications
iOS Multitasking: Local Notifications
To be able to play the song also when force quitting you need to do this by push notifications.
You can add the small sound to the Push Notification. So you need to store the sound snippet and the time on the server and trigger a push notification with the sound attached on the server. Or using LocalNotifications.
This will do the trick for you. Otherwise you can just play the sound if the app is running.

UIApplicationDelegate not called when using Backgrounder on a jailbroken device

I'm running into a bit of an issue.
My software needs to know when the application is in background so it will disconnect from the server, and start receiving push notifications.
For that I use UIApplicationDelegate's method :
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
- (void)applicationWillTerminate:(UIApplication *)application
The problem is, when a jailbroken device is using the "tweak" called backgrounder, that will force your application to stay active and not going into background, none of the UIApplicationDelegate's method get called when we click on the HOME button.
The thing is, each client connected in SSL cost me a lot of memory on the server. I do not really care that the device is jailbroken, or as a matter a fact, the application will run indefinitely on the device. But I would like them to disconnect from the server when the application is no longer on the screen, but I can't seems to find any method that will inform me of such operation when backgrounder is installed.
Solution find : check my own answer on the post
The best thing to do would to eat up A LOT of memory so that way the jail breakers would have to quit your application! Just kidding of course. The best option is to wait for inactivity on the user's side. For example: 1. Check if the device is jailbroken. 2. Check if backgrounder is installed (I am not sure if you can do this but I am pretty sure you can). 3. If the user is all those things wait for them to be inactive for, lets say, 30 seconds and then disconnect from the server.
Do none of those methods get called at all in your app when Backgrounder is installed, or only when your app transitions to the background? If they never get called at all, then the solution is extremely simple: Set a timer for, say, 10 seconds, when your app's application:didFinishLaunchingWithOptions method is called, and when that timer expires, if applicationDidBecomeActive: has not been called, then you know that the user is using Backgrounder.
If they do get called on app launch, though, then things get trickier. I'm not aware of any direct methods to detect if a tweak like Backgrounder is installed from within the sandbox. You could simply check if the device is jailbroken (there are a number of ways to do this, they should be easy to find), and add a timeout period for your server connection if so.
Milk Tea got me on the right track... So the bounty goes to him.
But actually I got a better solution...
I have checked every single notifications thrown when resigning the App...
and I got this, even with backgrounder configured to keep the App open, those notifications are thrown :
UIApplicationSuspendedEventsOnlyNotification -- When going into background
UIApplicationResumedEventsOnlyNotification -- When going into foreground
This makes sense, since even with backgrounder, it would have been a bad idea to let the App keep receiving events notifications, for example, when a change of rotation (or a shake) is done outside the App. Do you imagine every App doing a rotation on background everytime you move your iphone ?

iPhone background notifications

I'm making an iPhone app. I can receive UDP messages using AsyncUdpSocket. I want the app running in background, and when receive a message, an UIAlertView is displaying to the user, and he can enter in the app, or ignore the alert.
Is it possible to detect a message when the app is running in background.
Do I need something to execute my code in this method?
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
I saw lot of tutorials, with timer, but I don't need timer to wake up my app. Also I read that's it's possible while playing music, tracking position or using VOIP.
Do I need to play a fake song to keep my app running? or to do something like that?
This app is for security, for example if someone is touching or moving your motorbike/computer/whatever else, your iPhone can alert you and prevents from stealing.
I read other threads similar but didn't find an answer.
Thank you guys for giving me tips, or any help /sample.
You can't run in the background on a non-jailbroken phone without being in one of those three categories of app, and Apple’s really unlikely to approve your app if you use that facility for another purpose. UDP probably isn’t the best solution for this anyway—if your phone leaves the network that the other device (whatever it is) is on, it won’t receive the notification at all, whether or not it’s in the foreground. You’re probably a lot better off using the push notification API.
This seems like a perfect case to use Apple Push Notifications (APN). You app can register to receive the notifications and the phone will alert the user with any combination of badging, messages, or sounds. Sounds like you already have a server that is sending the UDP messages, so incorporating APN should be minimal. Especially if you use a third-party to send the push notification, such as Urban Airship. (I am not affiliated with them, but have used them on a large commercial project.)
By definition, local events are not triggered by receiving a message.

iphone XMPP App run background

I created a chat application using XMPP framework..when I quit the app(enter background mode) I want to receive the chat message..and also need to display the icon badge...How can I do this?
You can indeed run a XMPP Framework-based app in the background in iOS4 by calling it a VoIP app. (However, Apple will reject it from the App Store unless it also truly does VoIP).
You need to set the VoIP flag in your app's (appname)-info.plist file, and then in
(void)xmppStream:(XMPPStream *)sender socketWillConnect:(AsyncSocket *)socket
You'll need to set the socket stream flags to include kCFStreamNetworkServiceTypeVoIP:
CFReadStreamSetProperty([socket getCFReadStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
CFWriteStreamSetProperty([socket getCFWriteStream], kCFStreamNetworkServiceType, kCFStreamNetworkServiceTypeVoIP);
Then, your app will be woken up briefly when a new XMPP message arrives. In your normal
(void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
handler, you would want to create a local notification for the message if you are backgrounded (you can keep track of background state via UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification). The local notification handler can set the application badge number, etc (just like you would for a push notification).
EDIT
Newer versions of the XMPP Framework (specifically, GCDAsyncSocket) now support a call to make this easier, so you can just have:
- (void)xmppStream:(XMPPStream *)sender socketWillConnect:(GCDAsyncSocket *)socket
{
// Tell the socket to stay around if the app goes to the background (only works on apps with the VoIP background flag set)
[socket performBlock:^{
[socket enableBackgroundingOnSocket];
}];
}
There are a limited number of programs that can run in the background without limit, these being VOIP programs, those that play music, and those that track the user's location. If you're not doing any of these legitimately then you're limited to ten minutes of background operation. Note that Apple will reject apps that try silly tricks like playing 'empty' sounds to keep the app live.
You can find info on running tasks in the background here:
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Your other option I would guess is to have the chat program operate by contacting a server, and to have that server queue responses when a user is offline then deliver them when they next log on. Not being a net programmer myself I don't know how feasible this is, but I think it's the only way to do this indefinitely if you're only offering text chat. The better option would be to make your application VOIP enabled using the guide above.
EDIT As of the release of iOS 5.0, it is also possible to get apps registered as Newsstand applications to download information while backgrounded, plus a bunch of other funky features that are also Newsstand only.
-Ash
In the latest XMPP Framework you don't need to modify framework files.
Just do this:
1. Add this to your connect method
#if !TARGET_IPHONE_SIMULATOR
{
self.xmppStream.enableBackgroundingOnSocket = YES;
}
#endif
2. Add voip key to your info plist file:

App working in single tasking

How can make my iphone application is working both single tasking and multitasking.
Like if suppose in my application is working finely in latest multitasking supporting devices.
But it was not working properly in single-task supporting devices..
In single-task supporting devices during run time if suppose i pressed the exit button it will not support multitasking ..
How can solve this problem..
It depends on what your application is doing. If your application does not post local notifications to itself, or handle external notifications, then all you may need to do differently between single and multitasking is to save your user data and important state in the
- (void)applicationDidEnterBackground:(UIApplication *)application
method to handle multi-tasking iOS's, in addition to the
- (void)applicationWillTerminate:(UIApplication *)application
method which will handle the single-tasking versions. If your application is not killed while in the background in a multi-tasking OS then it will resume exactly where it was suspended, unlike the single-tasking OS where you must save all state and restore that state manually.