iOS Question BLE - disconnect the App without closing the App (all OK)

f0raster0

Well-Known Member
Licensed User
Longtime User
Guys,
running the BLE example.

When try to disconnect the peripheral I can't reconnect to it, the App keep scanning (when click scan), the only option to reconnect is closing the App.
it's like the peripheral is still "connected to the App" until closing the App?

Is there any way to really disconnect the App without closing the App?


B4X:
Sub btnDisconnect_Click
    Log("btnDisconnect")
    manager.Disconnect
    btnScan.Visible = True
    btnDisconnect.Visible = False
End Sub

Sub Manager_Disconnected
    Log("Disconnected: " & LastException)
    SetState(False)
    lblDeviceStatus.Text = "Not connected"
End Sub


Full Code:
B4X:
Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
  
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
    Page1.RootPanel.LoadLayout("1")
    NavControl.ShowPage(Page1)
    manager.Initialize("manager")
    Dim top As Int = btnReadData.top + btnReadData.Height + 4
  
    Page1.RootPanel.AddView(clv.AsView, 0, top,100%x, 100%y - top)
    Panel2.visible=False
  
    btnDisconnect.Visible = True
  
    App.IdleTimerDisabled = True
  
End Sub

Sub Page1_Resize (Width As Float, Height As Float)
  
End Sub

Sub Manager_StateChanged (State As Int)
    Dim msg As String
    Select State
        Case manager.STATE_UNKNOWN
            msg = "UNKNOWN"
        Case manager.STATE_POWERED_OFF
            msg = "OFF"         
        Case manager.STATE_POWERED_ON
            msg = "ON"         
        Case manager.STATE_RESETTING
            msg = "RESETTING"
        Case manager.STATE_UNAUTHORIZED
            msg = "UNAUTHORIZED"
        Case manager.STATE_UNSUPPORTED
            msg = "UNSUPPORTED"
    End Select
    btnScan.Enabled = (State = manager.STATE_POWERED_ON)
    Log("1.- Bluetooth Status" & msg)
  
End Sub

Sub btnScan_Click
    Log("2.- Btn Scan")
    manager.Scan(Null) 
    ActivityIndicator1.Visible = True
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    manager.StopScan
    If Name = "Nordic_Thingy" Or Name = "Nordic_Blinky" Then
            Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
            ConnectedName = Name
            manager.Connect(Id)
            manager.StopScan
            Log("3.- StopScan") 
    End If
End Sub

Sub Manager_Connected (services As List)
    Log("4.- Manager_Connected")
  
    lblDeviceStatus.Text = "Connected - " & ConnectedName
    ConnectedServices = services
    SetState(True)

End Sub

Sub SetState (connected As Boolean)
    Log("5.- *** SetState ***")
    btnDisconnect.Enabled = connected
    btnScan.Enabled = Not(connected)
    ActivityIndicator1.Visible = False
    ActivityIndicator2.Visible = False
    btnReadData.Enabled = connected
End Sub

Sub btnReadData_Click 
    ActivityIndicator2.Visible = True
    Log("6.- *** btnReadData ***")
    For Each s As String In ConnectedServices
        manager.ReadData(s)
    Next 
End Sub

Sub Manager_DataAvailable (Service As String, Characteristics As Map)
    ActivityIndicator2.Visible = False
    Log("7.- *** Service &  Characteristics ***")
    Log("Service: " & Service & " -- Characteristics: " & Characteristics)
  
End Sub

Sub btnDisconnect_Click
    Log("btnDisconnect")
    manager.Disconnect
    btnScan.Visible = True
    btnDisconnect.Visible = False
End Sub
Sub Manager_Disconnected
    Log("Disconnected: " & LastException)
    SetState(False)
    lblDeviceStatus.Text = "Not connected"
End Sub
 

f0raster0

Well-Known Member
Licensed User
Longtime User
ahh, probably I did something wrong in previous tests.
">>Clean project" and doing more tests it works OK o_O
 
Upvote 0
Top