### Use Java API Binding

Hints on how to use the TinkerForge Java Binding with B4J Apps.
Information Java Binding:
http://www.tinkerforge.com/en/doc/Software/API_Bindings_Java.html
Introduction
TinkerForge offers a precompiled Java library Tinkerforge.jar.
The library can be accessed directly by B4J using JavaObjects.
This is a very powerfull B4J feature, as it allows to use B4J functions combined with external library functions.
Rapid Application Development brought to live.
Download the TinkerForge Java API Binding here:
http://www.tinkerforge.com/en/doc/Downloads.html#downloads-bindings-examples
TinkerForge Java API Binding Documentation:
http://www.tinkerforge.com/en/doc/Software/API_Bindings_Java.html#api-bindings-java-install
Notes:
Below example is based upon B4J, but the same can be applied to B4A.
Installation
• Unpack the Java Binding ZIP file to a location of choice.
• Copy the tinkerforge.jar to the B4J Additional Library folder.
B4J Project Attributes add Additional Jars
For the B4J project add the Java TinkerForge library, like
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
' Add the Tinkerforge jar - ensure to store in the B4J additional libraries folder
#AdditionalJar: Tinkerforge
#End Region
Example B4J App
Purpose
Read the Temperature using a TF Temperature Bricklet connected to a TF Master Brick.
Hardware Requirements
TinkerForge Master Brick ( https://www.tinkerforge.com/en/shop/bricks/master-brick.html )
TinkerForge Temperature Bricklet ( https://www.tinkerforge.com/en/shop/bricklets/temperature-bricklet.html )
Software Requirements:
Install the Brick Daemon (brickd) and the Brick Viewer (brickv) on the device.
Read here:
http://www.tinkerforge.com/en/doc/Tutorials/Tutorial_Extending/Tutorial.html#tutorial-first-steps
TF Temperature Java API Binding:
http://www.tinkerforge.com/en/doc/Software/Bricklets/Temperature_Bricklet_Java.html#temperature-bricklet-java-api
Access the TF Temperature Bricklet Principle
The principle to read the temperature, but basically for every TF Bricklet is:
• Define new instances to access the Master Brick and the Bricklet.
• Connect to the Master Brick.
• Run a Bricklet Method (described in the Java API Binding documentation).
Hint:
Important: The parameter must be exactly match the parameter types for the method. So short must be short and not int.
In B4J JavaObjects are defined and used to access the external Classes from the TinkerForge Java API Library tinkerforge.jar.
Hint:
Use jd-gui ( http://jd.benow.ca ) to explore the tinkerforge.jar code.
B4J Code
Example defines a class and uses a method to get the temperature.
B4J Class Temperature
'Class module
`Sub Class_Globals
Type TemperatureType (Host As String, Port As Int, UID As String)
End Sub`
`Public Sub Initialize
End Sub`
' Read the Temperature.
' Important: The parameter must be exactly match the parameter types for the method. So short must be short and not int.
' Parameter: bt as TemperatureType
' Returns the temperature as Short
`Public Sub getBrickletTemperature(bt As TemperatureType) As Short
Dim result As Short = 0
' Define the JavaObject to access the masterbrick
Dim joIPConnection As JavaObject
' Init the JavaObject = Packagename.Class
joIPConnection.InitializeNewInstance("com.tinkerforge.IPConnection", Null)
' Define the JavaObjects to connect to the Bricklet
Dim joBrickletTemperature As JavaObject
' Init the JavaObject = Packagename.Class
joBrickletTemperature.InitializeNewInstance("com.tinkerforge.BrickletTemperature", Array(bt.UID, joIPConnection))
Try
' Connect to the brick master via host and port
joIPConnection.RunMethod("connect", Array(bt.Host, bt.Port))
Dim ipconnectionstate As Byte = joIPConnection.RunMethod("getConnectionState", Null)
' Get the temperature using the JavaObject instance
' Java API Binding Method: public short getTemperature()
result = joBrickletTemperature.RunMethod("getTemperature", Null)
' Disconnect from tbe host
joIPConnection.RunMethod("disconnect", Null)
Catch
Log(" Error Connect: " & LastException.Message)
End Try
Return result
End Sub`
B4J Code Main
`Sub Process_Globals
' Access the class
Private BrickletTemperature As Temperature
' Button to obtain the temperature
Private btnGetTemperature As Button
...
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
...
BrickletTemperature.Initialize
tfTemperatureUID.Text = "nfu"
...
End Sub`
`Sub btnGetTemperature_Action
Dim bt As TemperatureType
bt.HOST = "localhost"
bt.PORT = 4223
bt.UID = "nfu" ' Get from the brick viewer
Log("T: " & (BrickletTemperature.getBrickletTemperature(bt) / 100))
End Sub`
NOTE:
If the TF Master Brick has a WIFI-Extension, then set the WIFI-Extension host address as IP-Address,
like 192.168.n.n.

Changed: 2018-06-07 09:24, Category: TinkerForge, File: T6C264M1965.htm, ID: 1965