Android Question Get Bluetooth MAC

FrankBerra

Active Member
Licensed User
Longtime User
Hi all
I am trying to get my Bluetooth MAC Address of my device and i use this syntax:

Label2.Text = seriale.Address

but during compilation it gives me a lot of errors:

Error occurred on line: 39 (main)
java.lang.NullPointerException

at anywheresoftware.b4a.objects.Serial.getAddress(Serial.java:73)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)

at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

Seriale is declared as Serial
and Label2 is declared as Label


What can i do to get and show the MAC address of my device?

Thanks in advance
 

Beja

Expert
Licensed User
Longtime User
Erel has posted a solution for this problem.. do some search ;)

B4X:
Dim bta As JavaObject
     bta = bta.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethod("getDefaultAdapter", Null)
     Log(bta.RunMethod("getAddress", Null))

Thanks Don for the tip.. thumps up
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Sorry but I am a newbie here :-/

I used your code as follow:

B4X:
    Sub Globals
    Dim Label2 As Label
    Dim Label1 As Label
 
    Dim bta As JavaObject
    bta = bta.InitializeStatic("android.bluetooth.BluetoothAdapter").RunMethod("getDefaultAdapter", Null)
 
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Principale")
 
     Label2.Text = bta.RunMethod("getAddress", Null)

End Sub

I am trying to load my bluetooth MAC address into Label2 but i receive always these error codes:

Error occurred on line: 43 (main)
java.lang.RuntimeException: Object should first be initialized (JavaObject).

at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:236)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:96)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **

Any suggestion?

Thanks!
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Can you upload your project?
I gave you Erel's solution but if I test your project then will apply the code.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
There is no problem with your project.. I run it without touching it and it run perfect.. see the screenshot.
did you enable the Bluetooth device?
 

Attachments

  • blth.jpg
    blth.jpg
    17.1 KB · Views: 205
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
You helped me to spot the problem! ...and today I learned a lot of things

1) The project runs correctly only on real mobile phone. It gives back a lot of errors (instead of simple warnings) if executed on the emulator
2) It is working also using only the Serial library (without the JavaObject library and the codes you suggested)

Thank you very much for your help Beja!!! :) :)
 
Upvote 0
Top