This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get battery status?
I am implementing one iPhone application in which i want to read battery status of the iPhone by programming. I dont know is it possible or not. Please give me advice for that.
To read battery level and status you just have to:
float battLvl = [[UIDevice currentDevice] batteryLevel]; // range is from 0.0 to 1.0
UIDeviceBatteryState battState = [UIDevice currentDevice] batteryStatus];
To monitor battery level and status you can do the following:
// enable monitoring and add observers with selector
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryChanged:) name:#"UIDeviceBatteryStateDidChangeNotification" object:device];
// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];
NSLog(#"state: %i | charge: %f", device.batteryState, device.batteryLevel);
}
Following lines of code will be helpful:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
[[UIDevice currentDevice] batteryState];
For detail, refer THIS link. It will elaborate you.
Related
is there a logic to detect if the user has flipped their phone like from battery side to screen side and vice versa in horizontal plane? I have tried getting raw values to determine if the device is in the horizontal position on both faces but how to detect the whole motion , could someone point me in right direction.
If you take a look at the UIDevice class reference, you'll see the orientation enum. Two of it's values are UIDeviceOrientationFaceDown and UIDeviceOrientationFaceUp. That being said, all you have to do is register an observer to the UIDeviceOrientationDidChangeNotification notification, and upon call you can check the devices current orientation and handle this accordingly.
[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceOrientationDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationFaceDown) {
// device facing down
}else if (orientation == UIDeviceOrientationFaceUp) {
// device facing up
}else{
// facing some other direction
}
}];
Be sure to use the following to start generating the device notifications you'll need to observe.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
If you want to get more specific information about the orientation of the device, you'll need to use the Core Motion framework to get gyro data directly. With this, you can track the exact current direction the device is facing in 3D space.
_motionManager = [CMMotionManager new];
NSOperationQueue *queue = [NSOperationQueue new];
[_motionManager setGyroUpdateInterval:1.0/20.0];
[_motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData *gyroData, NSError *error) {
NSLog(#"%#",gyroData);
}];
This question already has answers here:
How to get battery status?
(5 answers)
Closed 9 years ago.
I am trying to display battery percentage with UILabel , but the result is nonsense ! here is my code :
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
int i=[myDevice batteryState];
_battery.text = [NSString stringWithFormat:#"%i",i];
the labels shows number 2 !!!!
Use below code to get battery level
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batteryLevel = [myDevice batteryLevel];
_battery.text = [NSString stringWithFormat:#"%f",batteryLevel*100];
[myDevice batteryLevel]; will give you the battery between 0.0 (empty) and 1.0 (100% charged)
Hope it helps..
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.
[myDevice batteryState];//return is a variable of UIDeviceBatteryState
the labels shows number 2 means"UIDeviceBatteryStateCharging, // plugged in, less than 100%",if you want to display battery percentage. The code in the first answer will help you.
In iOS, how to move UI elements (buttons and labels) when a device rotate to landscape mode from portrait mode?
There are so many ways.
In my case, I'm using notification
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(rotated:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
In rotated method
-(void) rotated: (NSNotification*) notification
{
orientation = [[UIDevice currentDevice] orientation];
if(UIDeviceOrientationPortraitUpsideDown==orientation)
// UI relocation
else if(UIDeviceOrientationPortrait==orientation)
// UI relocation
else if(UIDeviceOrientationLandscapeRight==orientation)
// UI relocation
else if(UIDeviceOrientationLandscapeLeft==orientation)
// UI relocation
}
I want to detect UIDeviceOrientationFaceDown. How can i do this???
Actually i want to perform some action when my iphone face is down on flat surface.
how is it possible??? plz help me to do this.
i have this code, but don't know where and how apply this code
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(#"will receive orientation notifications: %#", getOrientationUpdates?#"YES":#"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
if there is any tutorial then please suggest me that
Thanks in Advance and most welcome to your precious help and suggestions.
You can accomplish this by detecting the ProximityState of iPhone. Using the [UIDevice currentDevice] singleton, setting the proximityMonitoringEnabled to YES. you can access the proximity information through the proximityState property.
[[UIDevice currentDevice]proximityState];
iPhone has a sensor turns off the screen when you put it on your ear during a call, AFAIK that is an infrared sensor. and you can access it.
EDIT:
You can also accomplish it using the below code. UIDeviceOrientationFaceDown
The device is held parallel to the ground with the screen facing downwards. (regardless if it touched any object) if you want to know if the iPhone touched an object, detect the proximity state of device.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)detectOrientation;{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationPortrait:
{
NSLog(#"portrait");
}
break;
case UIDeviceOrientationPortraitUpsideDown:
{
NSLog(#"portraitUpSideDown");
}
break;
case UIDeviceOrientationLandscapeLeft:
{
NSLog(#"landscapeLeft");
}
break;
case UIDeviceOrientationLandscapeRight:
{
NSLog(#"landscapeRight");
}
break;
case UIDeviceOrientationFaceDown:
{
NSLog(#"facedown!!");
}
break;
default:
break;
}
}
}
EDIT: to answer the question in comment. add this line in your viewDidLoad
[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];
then write a method
-(void)handleProximityChangeNotification{
if([[UIDevice currentDevice]proximityState]){
NSLog(#"...");
}
}
You can use the z value from accelerometer sensor (-1 <= z <= 1). If your device is facing down, the z value will be in 0 < z <=1 (0 when it's facing parallel to the ground, 1 when it's facing perpendicular to the ground). To get this data you can use CMMotionManager
#import <CoreMotion/CoreMotion.h>
CMMotionManager *cm=[[CMMotionManager alloc] init];
cm.deviceMotionUpdateInterval=0.2f;
[cm startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMDeviceMotion *data, NSError *error) {
if(data.gravity.z>=0.3f)//
{
//DO something
}
}];
Don't forget to add CoreMotion framework to your project.
//I have created below snippet to let the sensor to be detected.
-(void)addProximitySensorControl {
UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;
BOOL state = device.proximityState;
if(state)
NSLog(#"YES");
else
NSLog(#"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(proximityChanged:)
name:#"UIDeviceProximityStateDidChangeNotification"
object:nil];
}
In the iPhone 3GS or earlier proximityChanged: method is called successfully but in iPhone 4 while I am hovering object from upwards the sensor(screen) its not being detected. Any idea Guys?
I can see a few problems with this code. The first is that you use
name:#"UIDeviceProximityStateDidChangeNotification"
instead of
name:UIDeviceProximityStateDidChangeNotification
Both work, but using the bare version will give you a compiler error if you make a typo. (You want to get a compiler error with typos, it prevents silent errors).
The next thing is that you aren't actually checking for the proximity sensor being available before adding the notification. Your code:
BOOL state = device.proximityState
But this just checks whether or not the device is close to the users face. What you really want is to set proximityEnabled to YES, then check that it actually got set. It's a little counterintuitive.
UIDevice *device = [UIDevice currentDevice];
[device setProximityMonitoringEnabled:YES];
if ([device isProximityMonitoringEnabled]) {
// Do your stuff
}
Here's a full code sample:
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
UIDevice *device = [UIDevice currentDevice];
// Register for proximity notifications
[device setProximityMonitoringEnabled:YES];
if ([device isProximityMonitoringEnabled]) {
[notificationCenter addObserver:self
selector:#selector(proximityChanged:)
name:UIDeviceProximityStateDidChangeNotification
object:device];
} else {
NSLog(#"No Proximity Sensor");
}
Apple Docs: "Not all iOS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityMonitoringEnabled property remains NO, proximity monitoring is not available."
There is nothing wrong with your code (assuming that you did implement proximityChanged: of course). I tested your code on an iPhone 4 and it responds to my hand moving in front of the proximity sensor.
Maybe the hardware is slightly different on the 3GS, meaning it is more sensitive to what you are doing? Can you try on a different iPhone 4 device (or at least verify that the proximity sensor works at all e.g. by using the phone app)?
Check out this one :
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html
You should always check whether the particular device have proximity sensor or not.
Not all iOS devices have proximity sensors.
BOOL state = device.proximityState;
if(state)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(proximityChanged:)
name:#"UIDeviceProximityStateDidChangeNotification" object:nil];
else
NSLog(#"NO");