Android Question Bluetooth StartDiscovery always False

Kiniol

Member
Licensed User
Longtime User
Hi,
I'm trying to write program to communicate witch microcontroler by bluetooth.
Everything is ok but the function StartDiscovery always return "false". I don't now why?
May anyone can help me?

Main:
#Region  Project Attributes
    #ApplicationLabel: Terminal
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim bluetooth As BluetoothAdmin
    Dim serial1 As Serial
    Dim foundDevices As List
    Type nameAndMac(name As String, mac As String)
    Dim connectedDevice As nameAndMac
    Dim connected As Boolean
    Dim ast As AsyncStreamsText
    
    Dim zezwolenia As RuntimePermissions
    
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Baudrate As EditText
    Private ConnectButton As Button
    Private DataBits As EditText
    Private Parity As EditText
    Private ScrollView1 As ScrollView
    Private SendButton As Button
    Private StopBits As EditText
    Private TextToSend As EditText
    Private PerDranted As Boolean
    
    Private PermissionButton As Button
    Private ShowBtButton As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("Layout")
    Activity.Title = "Terminal BT"
    
    If FirstTime Then
        connected = False
        bluetooth.Initialize("bluetoothEvent")
        serial1.Initialize("serial1Event")
        ScrollView1.Initialize(25)
    End If
    
End Sub

Sub Activity_Resume
    
    

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    
    If UserClosed Then
        ast.Close
        serial1.Disconnect
        Activity.Finish
        ExitApplication
    End If

End Sub

#Region zdarzenie bluetooth

Sub bluetoothEvent_StateChanged (NewState As Int, OldState As Int)
    
    Parity.Text = NewState
    
End Sub

Sub bluetoothEvent_DiscoveryStarted
    
    ToastMessageShow("Wysukiwanie rozpoczęto...", True)
    
End Sub

Sub bluetoothEvent_DeviceFound (Name As String, MacAddress As String)
    
    Dim nm As nameAndMac
    nm.name = Name
    nm.mac = MacAddress
    foundDevices.Add(nm)
    ProgressDialogShow("Poszukiwanie urządzeń...@ ".Replace("@", foundDevices.Size))
    
End Sub

Sub bluetoothEvent_DiscoveryFinished
    
    ProgressDialogHide
    If foundDevices.Size = 0 Then
        ToastMessageShow("Nie znaleziono urządzenia blutooth." & CRLF & "Spróbuj ponownie", True)
    Else
        Dim s As List
        s.Initialize
        For i = 0 To foundDevices.Size - 1
            Dim macname As nameAndMac
            macname = foundDevices.Get(i)
            s.Add(macname.name)
        Next
        
        Dim res As Int
        res = InputList(s, "Wybierz urządzenie", -1)
        If res <> DialogResponse.CANCEL Then
            connectedDevice = foundDevices.Get(res)
            ProgressDialogShow("Próba połączenia z:" & CRLF & connectedDevice.name & CRLF & connectedDevice.mac)
            serial1.Connect(connectedDevice.mac)
        End If
    End If
    
End Sub

Sub serial1Event_Connected (Success As Boolean)
    
    ProgressDialogHide
    
    If Success Then
        ToastMessageShow("Połączono OK", True)
        connected = True
        Activity.Title = "Terminal BT - " & " (Połączenie OK)"
        If ast.IsInitialized Then
            ast.Close
        End If
        
        ast.Initialize(Me, "astEvent", serial1.InputStream, serial1.OutputStream)
        
    Else
        connected = False
        ToastMessageShow("Problem z połączeniem", True)
    End If
    
End Sub

Sub astEvent_Error
    
    ToastMessageShow(LastException.Message, True)
    
End Sub

Sub astEvent_Terminated
    
    ToastMessageShow("Połączenie zerwane", True)
    
End Sub

Sub astEvent_NewText(Text As String)
    
    
    
End Sub


#End Region

#Region design

Private Sub TextToSend_TextChanged (Old As String, New As String)
    
End Sub

Private Sub StopBits_TextChanged (Old As String, New As String)
    
End Sub

Private Sub Parity_TextChanged (Old As String, New As String)
    
End Sub

Private Sub DataBits_TextChanged (Old As String, New As String)
    
End Sub

Private Sub SendButton_Click
    
    searchPartner
    
End Sub

Private Sub ConnectButton_Click
    
    startAdapterBt
    
End Sub

Private Sub ShowBtButton_Click
    
    showAdapter
    
End Sub

Private Sub PermissionButton_Click
    
    addPermissions
    
End Sub

Private Sub Baudrate_TextChanged (Old As String, New As String)
    
End Sub

#End Region

#Region funkcje

Sub addPermissions
    
    Dim Permissions As List
    Dim phone As Phone
    If phone.SdkVersion >= 31 Then
        Permissions = Array("android.permission.BLUETOOTH_ADMIN","android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", "android.permission.BLUETOOTH_ADVERTISE", zezwolenia.PERMISSION_ACCESS_FINE_LOCATION, zezwolenia.PERMISSION_ACCESS_COARSE_LOCATION)
    Else
        Permissions = Array(zezwolenia.PERMISSION_ACCESS_FINE_LOCATION)
    End If
    For Each Per As String In Permissions
        zezwolenia.CheckAndRequest(Per)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            Return
        End If
    Next
    
    PerDranted = True
    
    PermissionButton.Color = Colors.Green
    
End Sub

Sub startAdapterBt
    
    If PerDranted = False Then
        Return
    End If
    
    If bluetooth.IsEnabled = False Then
        If bluetooth.Enable = False Then
            ToastMessageShow("Nie udało się włączyć blutooth", True)
        Else
            ToastMessageShow("Włączam adapter blutooth...", True)
        End If
    Else
        bluetoothEvent_StateChanged(bluetooth.STATE_ON, 0)
    End If

End Sub

Sub showAdapter
    
    If PerDranted Then
        Dim i As Intent
        i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
        i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
        StartActivity(i)
    End If
    
End Sub

Sub searchPartner
    
    foundDevices.Initialize
    bluetooth.StartDiscovery
    If bluetooth.StartDiscovery = False Then                                    'jeśli proces poszukiwania urządzeń przerwany to:
        ToastMessageShow("Błąd Poszukiwania", True)                                'wyświetl informacje o błędzie poszukiwaniau urządzeń
    Else                                                                        'wprzeciwnym razie:
        ToastMessageShow("Poszukiwanie urządzeń", True)                            'wyświetl informacje o poszukiwaniu urządzeń
    End If
    
End Sub

#End Region

Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_CONNECT)
AddPermission(android.permission.BLUETOOTH_ADVERTISE)
AddPermission(android.permission.BLUETOOTH_ADMIN)
 

Kiniol

Member
Licensed User
Longtime User
Only one thing i need to now is why this StartDiscovery has false. Rest of code I clean later.

Bluetooth Chat doesn't work. "Error starting discovery process"
 
Last edited:
Upvote 0
Top