Android Question android.os.Handler.Callback / JavaObject

MarcoRome

Expert
Licensed User
Longtime User
Hi All.
I have this code in Java:
B4X:
import com.zj.btsdk.BluetoothService;
BluetoothService mService = null;

mService = new BluetoothService(this, mHandler);
...
mService.connect(con_dev);
mService.sendMessage("Congratulations!\n", "GBK");
....

private final  Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case BluetoothService.MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                .......
    };

Now all work method, etc... but i will want intercept handleMessage ( looper calls dispatchMessage on message target which is a Handler ) it should be pretty simple.
So i write this code in JavaObject:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim btdisponibile As Boolean = stampante.RunMethod("isAvailable", Null)
    If btdisponibile Then
        stampante.RunMethod("connect", Array(MacBluetooth))
        stampante.RunMethod("sendMessage", Array("Test", "GBK"))
        ........   
    End If

End Sub

Sub stampante As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim handler As JavaObject
    handler.InitializeNewInstance("android.os.Handler", Null)
    handler.CreateEventFromUI("android.os.Handler.Callback", "Callback" , Null)
    Dim ir As JavaObject
    Return ir.InitializeNewInstance("com.zj.btsdk.BluetoothService", Array(ctxt, handler))
End Sub

Sub Callback_Event (MethodName As String, Args() As Object) As Object
    Log(MethodName)
    Log(Args(0))
    Return Null
End Sub

what is missing ?
Thank you very much
Marco
 

MarcoRome

Expert
Licensed User
Longtime User
There is no interface here. You need to subclass Handler. It must be done with inline Java or (simpler) a library.
Hi Erel.
Why do you tell me it's not an interface ?
upload_2017-5-8_9-20-35.png
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Thank you for your reply Erel.
Can you do a little example please.
Thank you very much
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Here a small example that i wrote
B4A source + btsdk.jar
 

Attachments

  • BluetoothCitizenMio.zip
    24.5 KB · Views: 277
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi All, well.
Often happens to find the following situation: "handleMessage(Message msg)". Where the same behaves as an interface. I'm trying to write code JO so you can intercept the handler interface. The code in java is as follows ( as in #1 Post ):

B4X:
import com.zj.btsdk.BluetoothService;
BluetoothService mService = null;

mService = new BluetoothService(this, mHandler);
...
mService.connect(con_dev);
mService.sendMessage("Congratulations!\n", "GBK");


private final Handler mHandler = new Handler() {
@Overridepublic
void handleMessage(Message msg)
{switch (msg.what)
           {case BluetoothService.MESSAGE_STATE_CHANGE:switch (msg.arg1) {
.......};

I translated the same in the following B4A code:

B4X:
...
stampante.RunMethod("connect", Array(BluetoothDevice))
stampante.RunMethod("start", Null)

Sub stampante As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    'Dim handler As JavaObject
    handler.InitializeNewInstance("android.os.Handler", Null)
    'For handleMessage(Message msg)
    msg = handler.RunMethod("obtainMessage", Null)
    handleMessage(handler.RunMethod("handleMessage", Array(msg)))
    Dim ir As JavaObject
    Return ir.InitializeNewInstance("com.zj.btsdk.BluetoothService", Array(ctxt, handler))
End Sub



Sub handleMessage( msg1 As JavaObject)
 
    Log(msg)
    Dim what As Int = msg.GetField("what")
    Select what
        Case stampante.GetField("BluetoothService.MESSAGE_STATE_CHANGE")
         
        Case Else
            Return 
    End Select
End Sub

The result in the log window is as follows:


.....
(Message) { when=-7h38m38s427ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s427ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s427ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s427ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
(Message) { when=-7h38m38s428ms what=0 target=android.os.Handler }
....
For n record.... continue ... and here CRASH
read: unexpected EOF!

So i think there is still something wrong with the code.
Erel say ( #4 )

Handler.Callback is indeed an interface. However you need to pass a Handler not Handler.Callback.
You can call the Handler constructor that accepts Handler.Callback.

The question is: This code is translate in correct mode ??

B4X:
Sub stampante As JavaObject
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    'Dim handler As JavaObject
    handler.InitializeNewInstance("android.os.Handler", Null)
    'For handleMessage(Message msg)
    msg = handler.RunMethod("obtainMessage", Null)
    handleMessage(handler.RunMethod("handleMessage", Array(msg)))
    Dim ir As JavaObject
    Return ir.InitializeNewInstance("com.zj.btsdk.BluetoothService", Array(ctxt, handler))
End Sub

Thank you all.
 
Last edited:
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
It doesn't look correct. As I previously wrote it will be simpler to create a library. You can use Eclipse and compile it with SLC.
Yes i know Erel. It was just for curiosity.
Thank you anyway for your time.
 
Upvote 0
Top