Android Question Android Things Infinite Loop ( DIRECTION_IN )

MarcoRome

Expert
Licensed User
Longtime User
Hi All as object thread i have:
Raspberry 3 Android 8.1.0 ( API 27) + Android Things Build OIM1.18 OEM Version 16
All work in DIRECTION_OUT_INITIALLY_LOW, but if i add DIRECTION_IN i have an infinite loop True - False. In summary Statechanged changes continuously to False / True.

I have an infinite loop on this code:
B4X:
pin11 = pm.OpenGpio("BCM11")
pin11.Direction = pin11.DIRECTION_IN
pin11.AddListener("pin11")
....
Sub pin11_StateChanged (Value As Boolean)
    Dim clr As Int
    Log(Value)
    'True
    If Value Then
        clr = Colors.Red
    Else
    'False
        clr = Colors.Green
    End If
    Activity.Color = clr
End Sub

This is complete code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: AndroidThings
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: landscape
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

#AdditionalJar: androidthings, ReferenceOnly
Sub Process_Globals
    Private pm As PeripheralManager
    Private pin4 As Gpio 'led
    Private pin13 As Gpio 'led
    Private pin23 As Gpio 'led
    Private pin21 As Gpio 'led
   
    Private pin11 As Gpio 'button
   
End Sub

Sub Globals
    Private ToggleButton1 As ToggleButton
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   

    If FirstTime Then
        pm.Initialize
        Log(pm.GpioList)
        pin4 = pm.OpenGpio("BCM4")
        pin4.Direction = pin4.DIRECTION_OUT_INITIALLY_LOW
       
        pin13 = pm.OpenGpio("BCM13")
        pin13.Direction = pin13.DIRECTION_OUT_INITIALLY_LOW
       
        pin23 = pm.OpenGpio("BCM23")
        pin23.Direction = pin23.DIRECTION_OUT_INITIALLY_LOW
       
        pin21 = pm.OpenGpio("BCM21")
        pin21.Direction = pin21.DIRECTION_OUT_INITIALLY_LOW
       
        pin11 = pm.OpenGpio("BCM11")
        pin11.Direction = pin11.DIRECTION_IN
        pin11.AddListener("pin11")
       
    End If
    Activity.LoadLayout("1")
   
End Sub

Sub pin11_StateChanged (Value As Boolean)
    Dim clr As Int
    Log(Value)
    'True
    If Value Then
        clr = Colors.Red
    Else
    'False
        clr = Colors.Green
    End If
    Activity.Color = clr
End Sub

Sub ToggleButton1_CheckedChange(Checked As Boolean)
    pin4.Value = Checked 'turn on or off the led
    pin13.Value = Checked 'turn on or off the led
    pin21.Value = Checked 'turn on or off the led
    pin23.Value = Checked 'turn on or off the led
End Sub

This is Manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

'PER ANDROID THINGS
AddApplicationText(<uses-library android:name="com.google.android.things"/>)

''Launch activity automatically on boot (can remove if not needed)
AddActivityText(Main,
<intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.HOME"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
)
AddPermission(com.google.android.things.permission.USE_PERIPHERAL_IO)
AddPermission(com.google.android.things.permission.MANAGE_INPUT_DRIVERS)

Note: In manifst also change in android:targetSdkVersion=27 and without AddPermission(com.google.android.things.permission.MANAGE_INPUT_DRIVERS) but dont change nothing

Android_Things.jpg


As in this movie:


Note:
1. If i press the button it executes the instruction (as in the movie), but if i release the button it starts the infinite loop true / false ( as if it didn't memorize the state )
2. If i replace the button with an LED (and of course I change the instruction in DIRECTION_OUT_INITIALLY_LOW) it works without any problem.

Any idea ??
Thanks
Marco
 
Last edited:

DC1

Member
Licensed User
Longtime User
I have sucessfully built, complied and tested the

So now moved on to this thread :-

using the above code, I get the following error :-

Logger connected to: Android.local:5555
--------- beginning of main
--------- beginning of system
--------- beginning of crash
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_activity_create (java line: 358)
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/things/pio/PeripheralManager;
at anywheresoftware.b4a.objects.PeripheralManagerWrapper.Initialize(PeripheralManagerWrapper.java:26)
at ph1lj.things4.main._activity_create(main.java:358)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at ph1lj.things4.main.afterFirstLayout(main.java:105)
at ph1lj.things4.main.access$000(main.java:17)
at ph1lj.things4.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.things.pio.PeripheralManager" on path: DexPathList[[zip file "/data/app/ph1lj.things4-iyx58n8MOC_UfA7KFN8VCg==/base.apk"],nativeLibraryDirectories=[/data/app/ph1lj.things4-iyx58n8MOC_UfA7KFN8VCg==/lib/arm, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 14 more




so after much frustration, I re-flashed another uSD - hoping it would solve the problem.
Unfortunately not - can anyone help ?
 
Upvote 0
Top