Android Question [things] I2C

Erel

B4X founder
Staff member
Licensed User
Longtime User
@tigrot see this tutorial: https://www.b4x.com/android/forum/threads/74821/#content

You can access the I2C API with JavaObject:
B4X:
Dim jo As JavaObject = Manager
Log(jo.RunMethod("getI2cBusList", Null))
Dim Name As String = "..."
Dim Address As Int = ...
Dim device As JavaObject = jo.RunMethod("openI2cDevice", Array(Name, Address))

'Now call I2cDevice methods:
Dim buffer(100) As Byte
device.RunMethod("read", Array(Buffer, Buffer.Length))
Dim data As Byte = ...
Dim reg As Int = ...
device.RunMethod("writeRegByte", Array(reg, data))
 
Upvote 0

Michael1968

Active Member
Licensed User
Longtime User
Hi Erel,

I get this error:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 31 (Main)
java.lang.RuntimeException: Object should first be initialized (PeripheralManager).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at b4a.example.main._activity_create(main.java:382)
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:337)
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 b4a.example.main.afterFirstLayout(main.java:102)
at b4a.example.main.access$000(main.java:17)
at b4a.example.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 **
get the same error with the PWM example.

if i use b4j with Linux Image on the Pi, i2c works well.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Pls find an I2C example attached.
The BH1750 Digital Light Intensity Sensor is directly connected to the Raspberry Pi. Read the source for details. incl. pin mapping.

upload_2017-1-24_16-14-4.png
 

Attachments

  • 8-I2C BH1750.ZIP
    7.9 KB · Views: 381
Upvote 0

Michael1968

Active Member
Licensed User
Longtime User
the working I2C example attached.
The SHT Sensor is directly connected to the Raspberry Pi.
Thanks to Robert ,he wrote most of the Code, to help me to get it work :)

best regards

Michael
 

Attachments

  • sht30.zip
    13.1 KB · Views: 386
Upvote 0
Top