B4A Library Android Things

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/


SS-2017-01-03_17.03.30.jpg


Using it is simple:

1. Add a reference only dependency:
B4X:
#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.

B4X:
#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:
B4X:
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.
 

Attachments

  • Things.zip
    46.2 KB · Views: 665
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel. Thank you for proposing the future in B4X. It's fantastic
 

Beja

Expert
Licensed User
Longtime 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.
 

DonManfred

Expert
Licensed User
Longtime User
Is AddListener the same as a CPU's non-maskable Interrupt or IRQ?
i don´t know things but usually addlistener will add a reference to the calling activity

The listener will fire the events like
B4X:
Pin17_StateChanged
I guess the addlistener is used internaly in the lib already and no need to add another listener
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
V1.01 is released. It adds support for UART communication (serial port).

Example of connecting the Raspberry Pi to an Arduino:

SS-2017-01-22_10.27.17.jpg


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

Hypnos

Active Member
Licensed User
Longtime 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!
 

Hypnos

Active Member
Licensed User
Longtime 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?
B4X:
** 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 **
 

Hypnos

Active Member
Licensed User
Longtime User
Which device are you using?

Have you added the required manifest code?

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

Adie

Member
Licensed User
Longtime 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?
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 12 (Main)
java.lang.RuntimeException: Stub!
.....
 ** Activity (main) Resume **

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

Adie
 

Adie

Member
Licensed User
Longtime 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
 
Top