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: 773
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Lastly just out of curiosity. What does this sentence do?
#AdditionalJar: androidthings, ReferenceOnly
It adds a reference to androidthings.jar however unlike regular jars, it will not be included in the APK. androidthings.jar is a "stub" library that is only required during compilation.
 

Yvon Steinthal

Active Member
Licensed User
Longtime User
Im having a problem with this : AddApplicationText(<uses-library android:name="com.google.android.things"/>)

It tells me the app is not compatible with my tablet. Anything im missing?

Y.
 
Top