B4A Library USSD

Hello all,

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim myussd As USSDCallBack
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
  
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    myussd.Initialize("ussd")
  

    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_CALL_PHONE )
    Wait For Activity_PermissionResult(perm As String,res As Boolean)
    If perm=Starter.rp.PERMISSION_CALL_PHONE And res=True Then
        Dim id As InputDialog
        id.InputType=id.INPUT_TYPE_PHONE
        Dim sf As Object = id.ShowAsync("", "Enter USSD", "Ok", "", "Cancel", Null, False)
        Wait For (sf) Dialog_Result(Result As Int)
        If Result = DialogResponse.POSITIVE Then
            Log(id.Input)
        End If
        myussd.sendUssdRequest(id.Input)
    Else
        ToastMessageShow("Permission not granted",False)
    End If
  
End Sub

Sub ussd_onreceiveussdresponse(request As String,response As String)
    Log(response)
    Msgbox(response,request)
End Sub
Sub ussd_onreceiveussdresponsefailed(request As String,failcode As Int)
    Log(failcode)
End Sub
0.10 has separate events like _onreceiveussdresponsesub(request As String,response As String,subId as int) and _onreceiveussdresponsefailedsub(request As String,failcode As Int,subId as int) for sendUssdRequestForSubscription method .
 

Attachments

  • USSDCallBack.zip
    2.6 KB · Views: 797
  • USSDCallBack0.09.zip
    4.9 KB · Views: 14
  • USSDCallBack0.10.zip
    5.2 KB · Views: 5
Last edited:

vinpolins

Member
Hello all,
Hello, thank you for your useful library, and I hope you still work on this project.

Is it possible to add the ability to select SIM to make a USSD request on dual SIM smartphone?

Could you edit the lib for that, or could you share the source code and I will make an edit?
Thank you
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Can you test the new version ? 0.09
It is now attached to the original post .
something like this for testing :
B4X:
For Each si As SubscriptionInfo In myussd.ActiveSubscriptionInfoList
        Log(si.CarrierName&" "&si.SubscriptionId )
        myussd.sendUssdRequestForSubscription("*142#", si.SubscriptionId)
    Next

B4X:
AddPermission(android.permission.READ_PHONE_STATE)
 
Last edited:

vinpolins

Member
Can you test the new version ? 0.09
Thank you so much for the help, very useful.
So far the version 0.09 works well as expected.

I have 2 suggestions for improving if possible:
01- Add the SubscriptionId information in the OnReceiveUssdResponse and OnReceiveUssdResponseFailed events, so that user can know the event comes from which subscription ID (currently only Request and Response information available in these events).
02- Add the ability to check if the SubscriptionId is available (i.e not turned off) before sending a request (otherwise the OnReceiveUssdResponseFailed event will fire).

Thank you!
 

vinpolins

Member
Or, instead of suggestion #02, could you add the relationship between SubscriptionId and SIM's slotIndex?

I can obtain the availability of a subscription (i.e. SIM) by the method: getSimState(int slotIndex).
But I don't have any idea what is the relationship between SubscriptionId and SIM's slotIndex?

In my dual-SIM smartphone, the slotIndex's value is 0 and 1, while the SubscriptionId's value is 1 and 3 respectively.
I can't find any relationship between them.
 
Last edited:

vinpolins

Member
In summary, my two suggestions are:

01- Add SimSlotIndex property to SubscriptionInfo object;
02- Add SubscriptionId (or SimSlotIndex) to OnReceiveUssdResponse and OnReceiveUssdResponseFailed events.
 

vinpolins

Member
Check v0.10

The lib works fine almost as expected, except there was one issue:

For some reason, in the lib v0.10, the sendUssdRequestForSubscription method for SimSlot 1 always fires OnReceiveUssdResponseFailedSub event. (Failcode = -1)

The remaining work fine, including:
- The sendUssdRequestForSubscription method for SimSlot 0 works well, and does NOT fire OnReceiveUssdResponseFailedSub event;
- All other properties work fine as expected (CarrierName, DisplayName, SimSlotIndex, SubscriptionId, ...);
- When I fallback to v0.09, the sendUssdRequestForSubscription method works fine (for both SimSlot 0 and 1). I also test the same USSD command on the phone's dialpad for SIM Slot 1 and it works well. That means my SIM at Slot 1 was OK.

Thank you so much!
 

vinpolins

Member
I tried to perform the method sendUssdRequestForSubscription for seperately sim slot (i.e. user manually select sim slot from Spinner, not perform this method in the For...Next loop), and the event OnReceiveUssdResponseFailedSub does not fire anymore. That sounds weird!

But another weird issue happens: The USSDCallBack object is now "memorable", that means:
- When I first sendUssdRequestForSubscription from slot 0, the response was OK. Then I sendUssdRequestForSubscription from slot 1, the response was received exactly as from slot 0 (but they actually must be different, not the same).
- Now I completely close the app, and first sendUssdRequestForSubscription from slot 1, the response was also OK. Then I sendUssdRequestForSubscription from slot 0, the response was received exactly as from slot 1 (in fact they must be different).

Very strange (!)

My workaround is doing so that the USSDCallBack object cannot be "memorable".
So I removed the USSDCallBack variable in the Sub Class_Globals, and declare it as a local variable in the Sub where I call method sendUssdRequestForSubscription.
That works for me for now.

Please kindly fix the issue, if possible. Thanks a lot!
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…