Android Question Dji Lybrary ,How to define events for buttons c1, c2.

PABLO2013

Well-Known Member
Licensed User
Longtime User
HI Dear, How to define events for buttons c1, c2 and listen for click events for buttons c1, c2 in dji mobile sdk
tks in advanced
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use regular text for your questions.

Untested code:
B4X:
Sub CreateHardwareStateListener
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub

Sub HardwareState_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c1button As JavaObject = HardwareState.RunMethod("getC1Button", Null)
   Dim IsClicked As Boolean = c1button.RunMethod("isClicked", Null)
   Log($"C1 button: ${IsClicked}"$)
   Return Null
End Sub

Call CreateHardwareStateListener from AfterAircraftConnected.
 
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings thank you I'm going to try this. In these days I will finish my application dji-b4a, I hope you can try it. In any case you must trust it is your code, "half seriously half joking":), thank you for your help.
 
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings, I test it and it works very well.

When i pulse c1 button, "C1 = true" (this was what i need = +++ tks).

Also when the button is released an event occurs, "C1 = False" (it is ok).


Another thing when i press "C2" indicates, "C1 = false". i will create with your code the event for C2 button, to see what happens, thanks.
 
Last edited:
Upvote 0

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings, try to do the same code for c2 ... but if I press the button c2 or any other of the control it indicates "c1 = false"


B4X:
Sub AfterAircraftConnected
    If aircraft.CameraReady = False Or aircraft.BatteryReady = False Then
        Log("Camera / battery not ready")
        Sleep(500)
        If aircraft.Connected Then AfterAircraftConnected
        Return
    End If
    aircraft.RegisterBatteryStateEvent
    controller.Initialize("controller", aircraft)
    Log($"simulator: ${controller.SimulatorStarted)}"$
    camera.Initialize("camera", aircraft)
    pnlCamera.AddView(camera.CreateVideoView, 0, 0, pnlCamera.Width, pnlCamera.Height)
    timer1.Enabled = True
    WaypointOperator.Initialize("WaypointOperator")
    aircraftName = "N/A"
    Wait for (aircraft.GetName) Aircraft_ResultWithValue (Success As Boolean, ErrorMessage As String, Value As Object)
    If Success Then
        aircraftName = Value
    End If
   
    CreateHardwareStateListener
    CreateHardwareStateListener1
   
End Sub

Sub CreateHardwareStateListener
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub
Sub CreateHardwareStateListener1
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub

Sub HardwareState_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c1button As JavaObject = HardwareState.RunMethod("getC1Button", Null)
   Dim IsClicked As Boolean = c1button.RunMethod("isClicked", Null)
   Log($"C1 button: ${IsClicked}"$)
   ToastMessageShow($"C1 button: ${IsClicked}"$,False)
   Return Null
End Sub

Sub HardwareState_Event1 (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c2button As JavaObject = HardwareState.RunMethod("getC2Button", Null)
   Dim IsClicked As Boolean = c2button.RunMethod("isClicked", Null)
   Log($"C2 button: ${IsClicked}"$)
   ToastMessageShow($"C2 button: ${IsClicked}"$,False)
   Return Null
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Setup two different callbacks...

Why do you think the 2nd from you is using an Event sub Hardwarestate_Event1
The Event is always [callback]_Event

Try this (EDIT: Based on Erels answer you should NOT use this code)
B4X:
Sub CreateHardwareStateListener1
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState1", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub
Sub HardwareState1_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c1button As JavaObject = HardwareState.RunMethod("getC1Button", Null)
   Dim IsClicked As Boolean = c1button.RunMethod("isClicked", Null)
   Log($"C1 button: ${IsClicked}"$)
   ToastMessageShow($"C1 button: ${IsClicked}"$,False)
   Return Null
End Sub
Sub CreateHardwareStateListener2
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState2", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub
Sub HardwareState2_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c2button As JavaObject = HardwareState.RunMethod("getC2Button", Null)
   Dim IsClicked As Boolean = c1button.RunMethod("isClicked", Null)
   Log($"C2 button: ${IsClicked}"$)
   ToastMessageShow($"C2 button: ${IsClicked}"$,False)
   Return Null
End Sub

You also can use the code from @Erel and add code to check the C2 Button too. This will work too.

(EDIT: Based on Erels answer you should use THIS code)
B4X:
Sub CreateHardwareStateListener
   Dim RemoteController As JavaObject = aircraft
   RemoteController = RemoteController.RunMethod("getRemoteController", Null)
   Dim callback As Object = RemoteController.CreateEventFromUI("dji.common.remotecontroller.HardwareState$HardwareStateCallback", "HardwareState", Null)
   RemoteController.RunMethod("setHardwareStateCallback", Array(callback))
End Sub

Sub HardwareState_Event (MethodName As String, Args() As Object) As Object
   Log(MethodName)
   Dim HardwareState As JavaObject = Args(0)
   Dim c1button As JavaObject = HardwareState.RunMethod("getC1Button", Null)
   Dim c1Clicked As Boolean = c1button.RunMethod("isClicked", Null)
   Dim c2button As JavaObject = HardwareState.RunMethod("getC2Button", Null)
   Dim c2Clicked As Boolean = c2button.RunMethod("isClicked", Null)
   Log($"C1 button: ${c1Clicked}"$)
   Log($"C2 button: ${c2Clicked}"$)
   Return Null
End Sub
 
Last edited:
Upvote 0
Top