Android Question New Bluetooth example

red30

Well-Known Member
Licensed User
Longtime User
I am using a new example for working with Bluetooth. I'm trying to understand how it works through debugging, but when the program comes to NotifyOfStateChanged it crashes. Why does Bluetooth turn on when the program just opens? Is it possible to make "turn on Bluetooth?” Dialog when I click on the btnSearchForDevices button? if the user pushes "yes", the search process starts, otherwise nothing is happening. I do not understand how to do this for this new example. Another question is whether it is possible to add a new device to the list as soon as it is found and then to keep on searching and adding to the list as new devices appear? Because it stops just after the first and connects to the selected one. Periodically, the program hangs in the Connection is broken state and until it is restarted, the message will continue to pour.
 

DonManfred

Expert
Licensed User
Longtime User
rogram comes to NotifyOfStateChanged it crashes
It is great help if you hide the Errormessage and not posting a project which shows the issue.
Hard to help you in this case.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
If I am running the application from the first post "Android Bluetooth / BluetoothAdmin Tutorial" and go step by step since
B4X:
Sub Service_Create
    Manager.Initialize
End Sub
Thats in
B4X:
Private Sub NotifyOfStateChanged
    For Each Target In Array(Main, ChatActivity)
        CallSub(Target, "UpdateState")
    Next
End Sub
Crashes: "Error occurred on line: 103 (BluetoothManager) java.lang.NullPointerException"
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
*** Service (starter) Create ***
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
state changed: 11
An error occurred:
(Line: 103) CallSub(Target, "UpdateState")
java.lang.NullPointerException
B4X:
*** Service (starter) Create ***
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
state changed: 12
An error occurred:
(Line: 103) CallSub(Target, "UpdateState")
java.lang.NullPointerException
But if i run the app in "Release" it works well.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Works if used Debug mode (legacy). But the question remains:
Why does Bluetooth turn on when the program just opens? Is it possible to make "turn on Bluetooth?” Dialog when I click on the btnSearchForDevices button? if the user pushes "yes", the search process starts, otherwise nothing is happening. I do not understand how to do this for this new example. Another question is whether it is possible to add a new device to the list as soon as it is found and then to keep on searching and adding to the list as new devices appear? Because it stops just after the first and connects to the selected one.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I want to turn on the bluetooth and immediately start the search when I click the "btnSearchForDevice" button.
B4X:
#Region  Project Attributes
    #ApplicationLabel: Test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

#AdditionalJar: com.android.support:support-v4

Sub Process_Globals
    'Public Manager As BluetoothManager
    Private rp As RuntimePermissions
    Dim loc As AHLocale
End Sub

Sub Globals
    Private btnSearchForDevices As Button
    Private btnexit As Button
    Private sonLang As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    'UpdateState
End Sub

Public Sub UpdateState
    If Starter.Manager.BluetoothState=True Then
        'UpdateState
        rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("Нет разрешения...", False)
            Return
        End If
        Dim success As Boolean = Starter.Manager.SearchForDevices
        If success = False Then
            ToastMessageShow("Ошибка запуска процесса поиска.", True)
        Else
            ProgressDialogShow2("Поиск утройств...", False)
        End If
    End If
    'btnSearchForDevices.Enabled = Starter.Manager.BluetoothState
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
End Sub

Sub btnSearchForDevices_Click
    StartService(Starter)
    UpdateState
End Sub

Public Sub DiscoverFinished
    ProgressDialogHide
    If Starter.Manager.foundDevices.Size = 0 Then
        ToastMessageShow("Устройства не найдены.", True)
    Else
        Dim l As List
        l.Initialize
        For Each nm As NameAndMac In Starter.Manager.foundDevices
            l.Add(nm.Name)
        Next
        InputListAsync(l, "Выберете устройство для подключения:", -1, True)
        Wait For InputList_Result (Index As Int)
        If Index <> DialogResponse.CANCEL Then
            Dim device As NameAndMac = Starter.Manager.foundDevices.Get(Index)
            Starter.Manager.ConnectTo(device)
            ProgressDialogShow2($"Попытка подключиться к: ${device.Name} (${device.Mac})"$, False)
        End If
    End If
End Sub

Public Sub AfterConnect (Success As Boolean)
    ProgressDialogHide
End Sub
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public Manager As BluetoothManager
    Private a=0 As Int
End Sub

Sub Service_Create
'    Manager.Initialize
End Sub

Private Sub Service_Start (StartingIntent As Intent)
    If a=0 Then
        a=1
    Else
        Manager.Initialize
    End If
End Sub



'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
Why is this code not working?
 
Upvote 0
Top