B4A Library Android Things

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jan 3, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The Things library provides access to hardware features of Android Things platforms.

    The library supports GPIO and Serial communication.
    I2C with JavaObject: https://www.b4x.com/android/forum/threads/things-i2c.75504/#post-479173

    Follow this tutorial to get started: https://www.b4x.com/android/forum/posts/474952/



    [​IMG]

    Using it is simple:

    1. Add a reference only dependency:
    Code:
    #AdditionalJar: androidthings, ReferenceOnly
    2. Initialize a PeripheralManager object.
    3. Open the pin with pm.OpenGpio.
    You can see the pins mapping here: https://developer.android.com/things/hardware/raspberrypi-io.html
    4. Set the pin direction (input or output).
    5. Add a listener if needed.

    Code:
    #AdditionalJar: androidthings, ReferenceOnly
    Sub Process_Globals
       
    Private pm As PeripheralManager
       
    Private pin4 As Gpio 'led
       Private pin17 As Gpio 'button
    End Sub

    Sub Globals
    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
         pin17 = pm.OpenGpio(
    "BCM17")
         pin17.Direction = pin17.DIRECTION_IN
         pin17.AddListener(
    "pin17")
       
    End If
       
    Activity.LoadLayout("1")
    End Sub

    Sub Pin17_StateChanged (Value As Boolean)
       
    Dim clr As Int
       
    If Value Then clr = 0xFF54EF72 Else clr = 0xFFC52D56
       
    Activity.Color = clr
    End Sub

    Sub ToggleButton1_CheckedChange(Checked As Boolean)
       pin4.Value = Checked 
    'turn on or off the led
    End Sub
    6. Add the following code to the manifest editor:
    Code:
    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.IOT_LAUNCHER"/>
      <category android:name=
    "android.intent.category.DEFAULT"/>
      </
    intent-filter>
    )
    AddPermission(com.google.android.things.permission.USE_PERIPHERAL_IO)
    If you are getting an error similar to:

    java.lang.SecurityException: Caller lacks required permission com.google.android.things.permission.USE_PERIPHERAL_IO

    Then reboot the device. Your app should then have this permission.

    Updates

    1.10 - Based on android things v0.8.
    Instructions were updated:
    - New permission to add.
    - New #AdditionalJar line.
     

    Attached Files:

    Last edited: Apr 29, 2018
  2. MarcoRome

    MarcoRome Expert Licensed User

    Hi Erel. Thank you for proposing the future in B4X. It's fantastic
     
  3. Beja

    Beja Expert Licensed User

    Hi Erel,
    This is my hometown : ) thanks a lot for the tireless crusade to make programming easier for us every day.
    Question:
    Is AddListener the same as a CPU's non-maskable Interrupt or IRQ?
    Thanks again.
     
    Johan Hormaza likes this.
  4. DonManfred

    DonManfred Expert Licensed User

    i don´t know things but usually addlistener will add a reference to the calling activity

    The listener will fire the events like
    Code:
    Pin17_StateChanged
    I guess the addlistener is used internaly in the lib already and no need to add another listener
     
    Beja likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    The listener functionality is implemented in Google library.
     
  6. Toley

    Toley Active Member Licensed User

    Do you plan to add more functions or it cannot be done ?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    It should be simple to add more features. Once developers start to use it I can extend the library.
     
    Hypnos and Toley like this.
  8. Michael1968

    Michael1968 Member Licensed User

    Hi Erel,
    is it possible to add pwm and i2c Support to the library?

    thx
    Michael
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this in the questions forum.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    V1.01 is released. It adds support for UART communication (serial port).

    Example of connecting the Raspberry Pi to an Arduino:

    [​IMG]

    Three wires are required:
    RPi TX - Arduino RX
    RPi RX - Arduino TX
    RPi Ground - Arduino Ground.

    Android Things binds the uart to the OS output. You need to disable this feature.
    Connect the SD card to the PC. Edit CMDLINE.TXT and remove this text: console=serial0,115200
    (http://stackoverflow.com/questions/41127018/uart-peripherals-on-android-things-for-raspberry-pi-3)
    Otherwise it will not receive any data.

    B4A code:
    Code:
    Sub Process_Globals
       
    Private pm As PeripheralManager
       
    Private uart As UartDevice
       
    Private timer1 As Timer
    End Sub

    Sub Globals
     
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       
    If FirstTime Then
         pm.Initialize
         
    Log(pm.UartDeviceList)
         
    uart = pm.OpenUart("UART0")
         
    uart.Baudrate = 9600
         
    uart.StopBits = 1
         
    uart.Parity = uart.PARITY_NONE
         
    uart.DataSize = 8
         
    uart.AddListener("uart")
         timer1.Initialize(
    "timer1"1000)
         timer1.Enabled = 
    True
       
    End If
    End Sub

    Sub Timer1_Tick
       
    uart.Write("abc".GetBytes("ascii"))
    End Sub

    Sub UART_DataAvailable (Buffer() As Byte)
       
    Log("Data! " & BytesToString(Buffer, 0, Buffer.Length, "ASCII"))
    End Sub
    B4R code, in this case I'm using the Mega with its additional hardware serial ports, you can instead use SoftwareSerial:
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private timer1 As Timer
       
    Private astream As AsyncStreams
       
    Private SerialNative1 As Stream
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       RunNative(
    "SerialNative1"Null)
       astream.Initialize(SerialNative1, 
    "astream_NewData""astream_Error")
       timer1.Initialize(
    "timer1_Tick"1000)
       timer1.Enabled = 
    True
    End Sub

    Sub Timer1_Tick
       astream.Write(
    "def")
    End Sub

    Sub AStream_Error
       
    Log("error")
    End Sub

    Sub AStream_NewData (Buffer() As Byte)
       
    Log("*************************")
       
    Log(Millis)
       
    Log(Buffer)
    End Sub

    #if C
    void SerialNative1(B4R::Object* unused) {
    ::Serial1.begin(9600); //<--You can change the baud rate
    b4r_main::_serialnative1->wrappedStream = &::Serial1;
    }
    #end if
    Raspberry Pi 3 UART pins: https://developer.android.com/things/hardware/raspberrypi-io.html
     
    Last edited: Jan 22, 2017
    Beja, Toley, rwblinn and 2 others like this.
  11. Hypnos

    Hypnos Active Member Licensed User

    After updated the new version of library, I got the following error:
    Cannot find: C:\Program Files (x86)\Anywhere Software\Basic4android\libraries\androidthings.jar

    Seems the androidhings.jar not included in the Things.zip file? Can anyone let me know where to find it?

    Thanks!
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    I've uploaded a new zip file with the missing jar.
     
    Hypnos likes this.
  13. Hypnos

    Hypnos Active Member Licensed User

    Just want to try the GPIO on Things, when I do a pm.Initialize, got the following error. anyone know what is the problem?
    Code:
    ** Service (starter) Create **
    ** 
    Service (starter) Start **
    ** 
    Activity (main) Create, isFirst = true **
    Error occurred on line: 
    12 (Main)
    java.lang.RuntimeException: Stub!
     at com.google.android.things.pio.PeripheralManagerService.<init>(PeripheralManagerService.java:
    10)
     at anywheresoftware.b4a.objects.PeripheralManager.Initialize(PeripheralManager.java:
    29)
     at com.popstudio.smb.main._activity_create(main.java:
    384)
     at java.lang.reflect.Method.invoke(Native Method)
     at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:708)
     at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:340)
     at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:247)
     at java.lang.reflect.Method.invoke(Native Method)
     at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    134)
     at com.aaa.aaa.main.afterFirstLayout(main.java:
    102)
     at com.aaa.aaa.main.access$
    000(main.java:17)
     at com.aaa.aaa.main$WaitForLayout.run(main.java:
    80)
     at android.os.Handler.handleCallback(Handler.java:
    751)
     at android.os.Handler.dispatchMessage(Handler.java:
    95)
     at android.os.Looper.loop(Looper.java:
    154)
     at android.app.ActivityThread.main(ActivityThread.java:
    6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    755)
    ** 
    Activity (main) Resume **
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Which device are you using?

    Have you added the required manifest code?
     
  15. Hypnos

    Hypnos Active Member Licensed User

    Thank you! I missed the first code:
    AddApplicationText(<uses-library android:name="com.google.android.things"/>)
     
  16. coslad

    coslad Well-Known Member Licensed User

    is this library support SPI ?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

  18. Adie

    Adie Member Licensed User

    @Hypnos Download latest lib from first post. It is fixed in V1.10
    @Erel, maybe change the file name for additional clarity.

    Adie
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Which file?
     
  20. Adie

    Adie Member Licensed User

    Things.zip first post. :)

    While on the topic, I totally missed the 'additional info' :(

    I only installed the new lib. Did not do any of the 'additional steps' and it worked. But now I am worried that it might not work properly as I could not test the radio comms.

    Lastly just out of curiosity. What does this sentence do?
    #AdditionalJar: androidthings, ReferenceOnly

    Thanks in any case for the speedy update.
    Adie
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice