Can I check if a device has Accelerometer?

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 :stuck_out_tongue: ).  My apologies.

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

Thanks.

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):

[lua]

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 )

[/lua]

Brent

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.   :slight_smile:    

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):

[lua]

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 )

[/lua]

Brent

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.   :slight_smile: