Detect low battery warning ios [duplicate] - iphone

This question already has answers here:
Detecting the type of iPhone interrupt
(2 answers)
Closed 10 years ago.
I'm making a turnbased game. To prevent users from cheat, the turn will automatically get passed to the opponent if the user close the app in the middle of a turn. This because so the user can't close the app, restart it, and beging the turn from the beginning.
There are two cases that should penalize the player however. If a phone call gets in, or the low battery warning appears. I can detect the phone call coming in and respond, but I don't know what to do with the battery?
Any suggestions would be awesome

Battery monitoring is enabled by setting to YES a property of the UIDevice singleton:
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
iPhone OS provides two type of battery monitoring events, one for when the state changes (e.g., charging, unplugged, full charged) and one that updates when the battery’s charge level changes. As was the case with proximity monitoring, you register callbacks to receive notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
ALso refer this link.

Related

Minimal iOS BluetoothManager Example

I've been constructing a minimal example for detecting nearby Bluetooth devices using the BluetoothManager private framework in iOS 5.0.
Using an answer found in this question: Finding generic Bluetooth devices within reach
Here's my viewDidLoad method to register for the BluetoothAvailabilityChangedNotification. I also register for the BluetoothDeviceDiscoveredNotification as well.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(bluetoothAvailabilityChanged:)
name:#"BluetoothAvailabilityChangedNotification"
object:nil];
btCont = [BluetoothManager sharedInstance];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(deviceDiscovered:)
name:#"BluetoothDeviceDiscoveredNotification"
object:nil];
When I get the Bluetooth availability changed notification, I set the device scanning enabled, as outlined in one of the answers in the aforementioned link.
- (void)bluetoothAvailabilityChanged:(NSNotification *)notification
{
NSLog(#"BT State: %d", [btCont enabled]);
[btCont setDeviceScanningEnabled:YES];
}
For completeness, here's the deviceDiscovered notification method.
- (void)deviceDiscovered:(NSNotification *) notification
{
NSLog(#"Discovered one!");
}
The logs produced by running the test application are as follows:
BTM: attaching to BTServer
BTM: posting notification BluetoothAvailabilityChangedNotification
BT State: 1
BTM: setting device scanning enabled
Unfortunately, the phone isn't picking up any Bluetooth devices at all, even though I know there are proximate discoverable devices (verified using an Android device).
Some things I have tried already:
Calling [btCont setPowered: YES]; and registering for the associated power state change notification, executing setDeviceScanningEnabled:YES in the callback
Calling [btCont resetDeviceScanning] prior to the setDeviceScanningEnabled call
Calling the scanForConnectableDevices:(unsigned int)arg1; method, guessing that arg1 may be some kind of timeout value. I've tried a variety of values with no success.
Any thoughts would be much appreciated.
Thanks!
As far as I know, the bluetooth manager gets the list after OS has filtered the results.
You will only get the nearby headset devices and not all generic devices.
you have to use scanForServices:
// start scan
[btManager setDeviceScanningEnabled:YES];
[btManager scanForServices:0xFFFFFFFF];

iOS: Monitoring Battery Level uses more battery? Should auto-save?

I have a GPS app that already uses a fair amount of battery. Because of the nature of the app, I don't want the user to loose all of their data if their battery dies without them knowing it. So, I figured I would monitor the battery and then save and stop the GPS data if the battery is very low. I would use:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(batteryStateDidChange:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(batteryLevelDidChange:)
name:UIDeviceBatteryLevelDidChangeNotification
object:nil];
So, a few questions:
Would monitoring the battery cause even MORE battery drain?
Is it a good idea to auto-save (core-data) for the user right before the battery dies?
The device already has to monitor battery notifications in order to display the battery level when the time / carrier / signal strength is visible... I wouldn't expect registering for the notifications would add any additional stress to anything (power consumption). Maybe a few extra cpu cycles from your app for handling the notification. :-)
Auto saving might be a smart idea.
But another idea might be to cease GPS / CoreLocation services for your app once battery gets underneath a certain level (or offer that as a user-settable option).
If the iDevice dies while your application is running or while the device is "sleeping" your UIApplicationDelegate will be sent a applicationWillTerminate: message.
If the application was put in the background then you can handle the saving there as well in your appdelegate's applicationWillEnterBackground
Be careful about Autosaving it can randomly cause CPU usage causing your user interface to be jerky at supposedly "random" intervals.

Prevent iPhone from locking when connected to charger and app is running

I'm writing an iPhone app. When the app is running and the iPhone is charging, there is no need to lock the iPhone. Is it possible to prevent the locking of the iPhone when the device is charging and my app is running?
You can subscribe to UIDeviceBatteryStateDidChangeNotification notification to get the moment when your iphone begins/stops to charge. Then in case iphone is charging you can set idleTimerDisabled property in UIApplication object to YES to prevent device to go to sleep:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateBatteryState:)
name:UIDeviceBatteryStateDidChangeNotification
object:nil];
- (void) updateBatteryState:(NSNotification*)notification{
[UIApplication sharedApplication].idleTimerDisabled =
([UIDevice currentDevice].batteryState == UIDeviceBatteryStateCharging);
}
P.S. If user decides to put device to sleep with sleep/wake button there's no way to prevent him of doing so
This is not possible with current SDK.
EDIT: hmm, haven't got the question correctly from the first read - look on other replies for correct answer; my guess was, you were asking about if it possible to prevent appearance of the sync/charge screen when connecting device via usb or to the wall outlet

UIDeviceOrientationDidChangeNotification doesn't work while device is on table?

Hi there i have a problem,
In my AppDelegate i have used method beginGeneratingDeviceOrientationNotifications to start notify me when device starts rotating.
It works fine if i hand-held my ipad but when it is kept on table it doesn't work as expected.
it fires UIDeviceOrientationUnknown notification.
Also this notification gets started after UI launches not on splash screen.
following is my code:
if([[[PulseUIFactory Factory] GetUICreator] IsIPad])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
current device stars giving proper values.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
then some where i launches my UI as
[self Launch_UI];
but notification starts responding after [self Launch_UI]; call even if notification is registered before its call...
Please any help is appreciable!!!
When you place your device on a table, [[UIDevice currentDevice] orientation] will return UIDeviceOrientationFaceUp. Then if your device remains sitting on the table face up, it doesn't matter how you rotate it on the table, the current device orientation will still be UIDeviceOrientationFaceUp.
If the device has issues determining the orientation, you may get UIDeviceOrientationUnknown. See this tutorial on how to handle device rotation using UIDeviceOrientationDidChangeNotification.
Regarding your notification only firing after the UI is loaded, the UIDeviceOrientationDidChangeNotification will only fire when the device is rotated. So if you are not rotating your device until after the UI loads, you wont get a notification. If this is not the cause of the issue, I'd have to see more of your code to have a better idea of what is going on.

How can I tell my app is about to become inactive/go to background state?

I am assuming I need to implement:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(resignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
but am not sure if this is the right notification to determine my app is about to leave active state.
Is this a good place to cancel network connections, along with app termination?
There are cases where UIApplicationWillResignActiveNotification is sent but the application does not enter the background, for example if a call is received but the user chooses not to answer it.
Use UIApplicationDidEnterBackgroundNotification to be notified when entering the background. Be aware that this will sometimes be sent after UIApplicationWillEnterForegroundNotification if the application is quickly opened again.