Hi there,
I'll briefly explain the problem and then add related coding. I start by auto-connecting to bluetooth in activity MAIN and once connected, I start activity CENTRAL. My goal is to monitor the bluetooth connection and if it breaks, send the program back to the beginning of MAIN. Initially I declare BT As BluetoothAdmin and I am currently using that to return the status.
Now the above bluetooth connects fine and runs fine; once connected, I direct the program to a different activity (Central). In central I have the following code...
Now if I alter the if statement to only implement when ( BT.IsEnabled == true ) it directs back to main as it should - so the timer seems to be functioning correctly.
However when I do disconnect the bluetooth device it seems that Main.Bt.IsEnabled remains True. I'm not sure why this would be; if the connection is broken, will it not change the status automatically?
Any help would be very appreciated!
Thanks
Taylor
I'll briefly explain the problem and then add related coding. I start by auto-connecting to bluetooth in activity MAIN and once connected, I start activity CENTRAL. My goal is to monitor the bluetooth connection and if it breaks, send the program back to the beginning of MAIN. Initially I declare BT As BluetoothAdmin and I am currently using that to return the status.
B4X:
Sub Activity_Create(FirstTime As Boolean)
'StartActivity(Central) '*** TO ACCESS CENTRAL
Activity.LoadLayout("setup")
End Sub 'Loads Layout 1 and initializes serial and BT COM
Sub Activity_Resume
'MyDeviceName = "Put the name of your coupled device here!"
MyDeviceName = "HC-06"
ToastMessageShow("Connecting to " & MyDeviceName, True)
timBT.Initialize("timBT",5000)
timBT.Enabled = False
Try
BT.Initialize("BT")
Serial1.Initialize("Serial1")
Catch
ToastMessageShow("No BlueTooth Device visible...", True)
End Try
'Start Bluetooth
Try
If BT.IsEnabled = False Then
lblBT.Color = Colors.Black
BT.Enable
Else
'connect to SK
BTConnectToDevice
End If
Catch
End Try
End Sub ' Calls "BTConnectToDevice
Sub BTConnectToDevice
Dim PairedDevices As Map
PairedDevices = Serial1.GetPairedDevices
Try
Serial1.Connect(PairedDevices.Get(MyDeviceName))
Catch
ToastMessageShow("Device not available",True)
End Try
End Sub ' Pairs with HC-06
Sub BT_StateChanged(NewState As Int,OldState As Int)
If NewState = BT.STATE_ON Then
BTConnectToDevice
Log("BT Connect")
Else
Serial1.Disconnect
timBT.Enabled = False
Log("BT Disconnect")
End If
End Sub ' Originally found IN MAIN.
Sub Serial1_Connected (Success As Boolean)
If Success = True Then
ToastMessageShow("Bluetooth connected to " & Serial1.Address, False)
timBT.Enabled = True
Else 'disconnected
ToastMessageShow("Connection to " & Serial1.Address & _
" broken!", True)
timBT.Enabled = False
errorSelection
End If
End Sub ' Once connected to HC-06 will enable timBT timer.
Now the above bluetooth connects fine and runs fine; once connected, I direct the program to a different activity (Central). In central I have the following code...
B4X:
Dim BtCheck As Timer
B4X:
BtCheck.Initialize("BtCheck", 1000)
BtCheck.Enabled = True
B4X:
Sub BtCheck_tick
If Main.BT.IsEnabled == False Then
StartActivity(Main)
End If
End Sub ' Will check if BT active
Now if I alter the if statement to only implement when ( BT.IsEnabled == true ) it directs back to main as it should - so the timer seems to be functioning correctly.
However when I do disconnect the bluetooth device it seems that Main.Bt.IsEnabled remains True. I'm not sure why this would be; if the connection is broken, will it not change the status automatically?
Any help would be very appreciated!
Thanks
Taylor