IPhone sensor monitoring.

Source: Internet
Author: User

 

The proximity sensor on the iPhone detects when the device is close to your face (or otherwise covered ). there aren't used times when using the sensor is of value, however, the Google Voice Search application has put this to good use as a means to trigger
Voice Recording for a search request. If you have an interest in doing something similar, read on.

Proximity sensor monitoring

It all begins by enabling proximity monitoring, this is followed by setting up a notification request to call a method when the proximity state changes:

View plaincopy to clipboardprint?
  1. // Enabled monitoring of the Sensor
  2. [[Uidevice currentdevice] setproximitymonitoringenabled: Yes];
  3. // Set up an observer for proximity changes
  4. [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (sensorstatechange :)
  5. Name: @ "uideviceproximitystatedidchangenotification" Object: Nil];

// Enabled monitoring of the sensor [[uidevice currentdevice] setproximitymonitoringenabled: Yes]; // set up an observer for proximity changes [[nsicationicationcenter defacenter center] addobserver: Self selector: @ selector (sensorstatechange :) name: @ "uideviceproximitystatedidchangenotification" Object: Nil]; 

The method below will be called when the sensor state is updated, a message is printed to the debug Console Based on the sensor proximity.

View plaincopy to clipboardprint?
  1. -(Void) sensorstatechange :( nsicationicationcenter *) Notification
  2. {
  3. If ([[uidevice currentdevice] proximitystate] = Yes)
  4. Nslog (@ "device is close to user .");
  5. Else
  6. Nslog (@ "device is ~ Not ~ Closer to user .");
  7. }

-(Void) sensorstatechange :( nsnotificationcenter *) Notification {If ([uidevice currentdevice] proximitystate] = Yes) nslog (@ "device is close to user. "); else nslog (@" device is ~ Not ~ Closer to user .");} 

Detecting proximity sensor

Not all IOS device have proximity sensors. The apple API documentation states that you shoshould enable proximity monitoring and check the proximitystate, if the return value is no, then the device does not have a sensor.

I was unable to successfully use this approach to determine if a device has a sensor. Any additional ideas or suggestions are welcome.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.