Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Can I check if a device has Accelerometer?
Started by santiagoluib3 Jul 01 2015 07:23 AM

2 replies to this topic

Best Answer bjsorrentino , 01 July 2015 - 01:52 PM

Hi @santiagoluib3,

You might consider running an "accelerometer" check for a few seconds and see if you get a response. For example, check the "event.deltaTime" and see if it changes. If you do get a response within this time, then you can assume there's an accelerometer. If not, then assume it "timed out" without a response.

 

Something like this (I tested this on a device with an accelerometer, but not one without, so you'll need to test it more yourself):

 

local hasAccelerometer = false
local previousDeltaTime = 0
local timeoutTimer
local timesToCheck = 10
 
local function onAccelerate( event )
    --print( event.deltaTime )
    if ( event.deltaTime ~= previousDeltaTime ) then
        hasAccelerometer = true
    end
end
 
local function checkForTimeout( event )
    if ( hasAccelerometer == true ) then
        print( "THERE IS AN ACCELEROMETER" )
        timer.cancel( timeoutTimer )
    elseif ( event.count == timesToCheck and hasAccelerometer == false ) then
        print( "NO ACCELEROMETER DETECTED" )
        Runtime:removeEventListener( "accelerometer", onAccelerate )
    end
end
 
timeoutTimer = timer.performWithDelay( 1000, checkForTimeout, timesToCheck )
 
Runtime:addEventListener( "accelerometer", onAccelerate )
 
Brent

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

santiagoluib3

[GLOBAL: userInfoPane.html]
santiagoluib3
  • Contributor

  • 181 posts
  • Corona SDK

Hi everyone, 

 

I'm not sure if this has been discussed earlier, but I can't seem to find anything related to my question (or I'm just too lazy to look for it :P ).  My apologies.

 

Is there a way in Corona SDK to check if a device is equipped with accelerometer?

 

Thanks.



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

  Best Answer

Hi @santiagoluib3,

You might consider running an "accelerometer" check for a few seconds and see if you get a response. For example, check the "event.deltaTime" and see if it changes. If you do get a response within this time, then you can assume there's an accelerometer. If not, then assume it "timed out" without a response.

 

Something like this (I tested this on a device with an accelerometer, but not one without, so you'll need to test it more yourself):

 

local hasAccelerometer = false
local previousDeltaTime = 0
local timeoutTimer
local timesToCheck = 10
 
local function onAccelerate( event )
    --print( event.deltaTime )
    if ( event.deltaTime ~= previousDeltaTime ) then
        hasAccelerometer = true
    end
end
 
local function checkForTimeout( event )
    if ( hasAccelerometer == true ) then
        print( "THERE IS AN ACCELEROMETER" )
        timer.cancel( timeoutTimer )
    elseif ( event.count == timesToCheck and hasAccelerometer == false ) then
        print( "NO ACCELEROMETER DETECTED" )
        Runtime:removeEventListener( "accelerometer", onAccelerate )
    end
end
 
timeoutTimer = timer.performWithDelay( 1000, checkForTimeout, timesToCheck )
 
Runtime:addEventListener( "accelerometer", onAccelerate )
 
Brent


[TOPIC: post.html]
#3

santiagoluib3

[GLOBAL: userInfoPane.html]
santiagoluib3
  • Contributor

  • 181 posts
  • Corona SDK

Thanks Brent.

 

The way I planned to implement it is this --  Provide the player with left/right streering buttons if the device doesn't have accelerometer, but the game defaults to accelerometer control if it has one.  I just felt the game is best played, more natural and more fun when played with accelerometer.   :)    




[topic_controls]
 
[/topic_controls]