Android Question Paired Devices

Terradrones

Active Member
Hi All

I need a kick in the right direction again please.

I used to be able to list all Paired Devices and select one of them on the list and it worked fine. Now it does not even show the list, although I have 4 paired Instruments.

Here I call the Function in the "Bluetooth" Class from the "Parameter" Activity:

B4X:
[/
Sub Paired_Click
    Dim success As Boolean = CGlobals.BT.Connect
    If success = False Then
        ToastMessageShow("Cannot Connect.", True)
    Else
        ToastMessageShow("Connected.", True)
    End If
    ProgressDialogHide
End Sub
]

In the "Bluetooth" Class:

[CODE=b4x][/
Public Sub Connect As Boolean
    Dim PairedDevices As Map
    
    Try
        PairedDevices = Serial.GetPairedDevices
        Dim list As List
        list.Initialize
        For i = 0 To PairedDevices.Size - 1
            list.Add(PairedDevices.GetKeyAt(i))
        Next
        Dim Res As Int
        Res = InputListAsync(list, "Select Instrument", -1,False) 'show list with paired devices
        If Res <> DialogResponse.CANCEL Then
            ProgressDialogShow2("Trying To Connect...", True)
            Serial.Connect(PairedDevices.Get(list.Get(Res)))         'convert the name to mac address
            CGlobals.Mac = PairedDevices.Get(list.Get(Res))        'Mac Address
            CGlobals.Name = PairedDevices.GetKeyAt(Res)        'Name
            Return True
        End If
    Catch
        Log(LastException)
    End Try
    Return False
End Sub

Under "PairedDevices" there are 4 Survey Instruments, but the program crashes at "Res=". Even the "Log(LastException" does not show anything.

It used to work, but not anymore.

Any suggestions?

Thanks
Michael

]
 

Terradrones

Active Member
This is the error message that I am getting when I remove "Try/Catch":

Error occurred on line: 69 (BlueTooth)
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1101)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:409)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:109)
at android.app.Dialog.show(Dialog.java:340)
at anywheresoftware.b4a.Msgbox.msgbox(Msgbox.java:186)
at anywheresoftware.b4a.keywords.Common.InputList(Common.java:532)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at CEASER.APK.bluetooth._connect(bluetooth.java:648)
at CEASER.APK.parameter._paired_click(parameter.java:5537)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7455)

I changed this:
Res = InputListAsync(list, "Select Instrument", -1,False) 'show list with paired devices
to this:
Res = InputList(list, "Select Instrument", -1) 'show list with paired devices
 
Upvote 0

Terradrones

Active Member
Here I did it slightly different.

The following Sub calls the "Connect" Sub in the Bluetooth Class:

B4X:
[/
Sub Paired_Click
    Dim success As Boolean = CGlobals.BT.Connect
    If success = False Then
        ToastMessageShow("Cannot Connect.", True)
    Else
        ToastMessageShow("Connected.", True)
    End If
    ProgressDialogHide
End Sub
]

Then in the Bluetooth Class, there is this Function:

[CODE=b4x][/
Public Sub Connect As Boolean
    Dim PairedDevices As Map
    
    Try
        PairedDevices = Serial.GetPairedDevices
        Dim list As List
        list.Initialize
        For i = 0 To PairedDevices.Size - 1
            list.Add(PairedDevices.GetKeyAt(i))
        Next
        Dim Res As Int
        Res = InputListAsync(list, "Select Instrument", 0,True) 'show list with paired devices
        If Res <> DialogResponse.CANCEL Then
            ProgressDialogShow2("Trying To Connect...", True)
            Serial.Connect(PairedDevices.Get(list.Get(Res)))         'convert the name to mac address
            CGlobals.Mac = PairedDevices.Get(list.Get(Res))        'Mac Address
            CGlobals.Name = PairedDevices.GetKeyAt(Res)        'Name
            Return True
        End If
    Catch
        Log(LastException)
    End Try
    Return False
End Sub

It crashes where it says "Res=...." with the following comments:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
    at anywheresoftware.b4a.keywords.Common.InputListAsync(Common.java:553)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at CEASER.APK.bluetooth._connect(bluetooth.java:648)
    at CEASER.APK.parameter._paired_click(parameter.java:5545)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:205)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7455)
    at android.view.View.performClickInternal(View.java:7428)
    at android.view.View.access$3600(View.java:813)
    at android.view.View$PerformClick.run(View.java:28495)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7753)
    at java.lang.reflect.Method.invoke(Native Method)

]

What am I doing wrong?
 
Upvote 0
Top