android phone - how to use vibrator?

Questions and information about Crank Software's Storyboard Engine

Moderator: jamiev

android phone - how to use vibrator?

Postby vbe » Tue Feb 20, 2018 9:50 am

Hello,
Is there possible to use android phone vibration using crank storyboard? If so can you please point me to some documentation or example?
Thank you
vbe
 
Posts: 5
Joined: Wed Sep 16, 2015 8:11 am

Re: android phone - how to use vibrator?

Postby MikeMarchand » Tue Feb 20, 2018 10:20 am

Hi vbe,

This is certainly possible, there are a couple ways to accomplish this. You could use the LuaJava interface, this allows you to invoke Java code from Lua. Another way is to write native Java and invoke the external library from Lua code.

Documentation for the Storyboard Lua Android Integration can be found here:
http://www.cranksoftware.com/cranksoftware/v5.1.0/docs/ch23s02.html#d0e5737

A forum post about launching native Android activities, a great example of using the LuaJava API:
http://forums.cranksoftware.com/viewtopic.php?f=9&t=318&p=605&hilit=LuaJava#p605

A forum post about using External JAR libraries:
http://forums.cranksoftware.com/viewtopic.php?f=9&t=362&p=846&hilit=LuaJava#p846

Android Vibrator class:
https://developer.android.com/reference/android/os/Vibrator.html

Hopefully this helps!

Cheers,
Mike
MikeMarchand
 
Posts: 27
Joined: Wed Jan 21, 2015 3:33 pm

Re: android phone - how to use vibrator?

Postby MikeMarchand » Tue Feb 20, 2018 10:45 am

Hi vbe,

In the past, we have used the following code. A quick disclaimer, I have not tested this in recent times so I cannot say for sure that it will work but hopefully it can at the very least point you in the correct direction.

Code: Select all
local function lm(msg)
    gre.set_data({ ["msg"] = msg })
    gre.send_event("errMsg")
end
function cb_vibrate(mapargs)
    if(luajava == nil) then
        lm("No luajava Lua object")
        return
    end
    if(luajava.bindClass == nil) then
        lm("No luajava.bindClass Lua object")
        return
    end
   
    --lm("A")
    local sys = luajava.bindClass("java.lang.System")
    if(sys == nil) then
        lm("No java.lang.System object")
        return
    end
   
    --lm("B")
    local stm = sys:currentTimeMillis()
    if(stm == nil) then
        lm("No current time")
        return
    end
   
    --lm("Time is " .. tostring(stm))
   
    local na = luajava.nativeActivity()
    if(na ~= nil) then
        --lm("Vibrate")
        local vs = na:getSystemService("vibrator")
        vs:vibrate(1000);
    else
        lm("No Native Activity")
    end
end


Cheers,
Mike
MikeMarchand
 
Posts: 27
Joined: Wed Jan 21, 2015 3:33 pm

Re: android phone - how to use vibrator?

Postby vbe » Wed Feb 21, 2018 8:33 am

Hello,
thank you your code is working and it vibrates for me (I tested on Lenovo TAB 2 A10-70F; Android K: 4.4.4)

BUT I need to add permission:
1. Run->StoryBoard Application Export Configuration
2.Select Packaging Method->Manifest->
type: android.permission.VIBRATE
3. click add
vbe
 
Posts: 5
Joined: Wed Sep 16, 2015 8:11 am


Return to Storyboard Engine

Who is online

Users browsing this forum: No registered users and 2 guests

cron