Bluetooth errors out on first attempt works on second

Graham

Member
Licensed User
Longtime User
Connecting a XOOM to a PIC based board I made. When I attempt to connect Xoom to PIC, first time after powering up PIC board, the connection errors out. The connection works great after I connect again. If I repower PIC board, its the same scenario...errors our first time and then second time BT stays connected. I am assuming I have something out of order on my XOOM code. Any ideas?

B4X:
Sub Activity_Create(FirstTime As Boolean)

If File.Exists(File.DirRootExternal,"config2.txt") Then
   ' Msgbox("File Exists","GC")
  Else
     Msgbox("Making file","GC")
   File.WriteString(File.DirRootExternal, "Config2.txt", _ 
   2 & CRLF & 2& CRLF & 3& CRLF & 4& CRLF & 5& CRLF & 6& CRLF & 7& CRLF & 8 _ 'GPSRowX
   & CRLF & 9& CRLF & 10& CRLF & 11& CRLF & 12& CRLF & 13& CRLF & 14& CRLF & 15& CRLF & 16 _ 'GPSRowX
   & CRLF & 17& CRLF & 18& CRLF & 19& CRLF & 20& CRLF & 21& CRLF & 22& CRLF & 23& CRLF & 24 _ 'GPSRowX
   & CRLF & 25& CRLF & 26& CRLF & 27& CRLF & 28& CRLF & 29& CRLF & 30& CRLF & 31& CRLF & 32 _ 'GPSRowX
   & CRLF & 33& CRLF & 34& CRLF & 35& CRLF & 36& CRLF & 37& CRLF & 38& CRLF & 39& CRLF & 40 _ 'GPSRowX
   & CRLF & 41& CRLF & 42& CRLF & 43& CRLF & 44& CRLF & 45& CRLF & 46& CRLF & 47& CRLF & 48 _ 'GPSRowX
   & CRLF & 1& CRLF & "Yes" & CRLF & 1& CRLF & 1.00& CRLF & 21& CRLF & "Yes" & CRLF & 30& CRLF & 222& CRLF & 30& CRLF & "Full" _
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _ 'Target
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _ 'Target
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _ 'Target
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _ 'Target
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _ 'Target
   & CRLF & 100 & CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100& CRLF & 100 _  'Target
   & CRLF & "Yes" & CRLF & 10000 & CRLF & 20000 & CRLF & 30000 & CRLF & 6.0 & CRLF & "Imp" _'Pop1-3,SimSpeed,Units
   & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" _
   & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" _
   & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" _
   & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" & CRLF & "On" _
   & CRLF & "Pull" & CRLF & "100" & CRLF & "210" & CRLF & "310" & CRLF & "5" & CRLF & "20000" & CRLF & "2")
 End If
   
ReadConfig

Activity.LoadLayout("CalScreen")
ScrID=1
PopID=1
HideKeyboard
LoadPlanter
imgBTOn.Left = 1100
imgBTOff.Left = 1100
AdjustGPS
AdjustGPSOffset
StandAloneCheck

MakeXML

If FirstTime Then
   admin.Initialize("admin")
   serial1.Initialize("serial1")
End If

End Sub

Sub Activity_Resume
Awake.KeepAlive(True) 
   If admin.IsEnabled = False Then
      If admin.Enable = False Then 
         ToastMessageShow("Error enabling Bluetooth adapter.", False)
      Else
         ToastMessageShow("Enabling Bluetooth adapter...", False)
         'the StateChanged event will be soon raised
      End If
   Else
      Admin_StateChanged(admin.STATE_ON, 0)
   End If
End Sub

Sub imgBlueTooth_Click
foundDevices.Initialize
   If admin.StartDiscovery   = False Then 
      ToastMessageShow("Error starting discovery process.", False)
   Else
      ProgressDialogShow("Searching for devices...")
   End If
End Sub

Sub Admin_DiscoveryFinished
   ProgressDialogHide
   If foundDevices.Size = 0 Then
      ToastMessageShow("No device found.", False)
   Else
      Dim l As List
      l.Initialize
      For i = 0 To foundDevices.Size - 1
         Dim nm As NameAndMac
         nm = foundDevices.Get(i)
         l.Add(nm.Name)
      Next
      Dim res As Int
      res = InputList(l, "Choose device to connect", -1)
      If res <> DialogResponse.CANCEL Then
         connectedDevice = foundDevices.Get(res)
         ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
         serial1.Connect(connectedDevice.Mac)
      End If
   End If

End Sub
Sub Admin_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ":" & MacAddress)
   Dim nm As NameAndMac
   nm.Name = Name
   nm.Mac = MacAddress
   foundDevices.Add(nm)
   ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub Serial1_Connected (Success As Boolean)
   ProgressDialogHide
   Log("connected: " & Success)
   If Success = False Then
      Log(LastException.Message)
      ToastMessageShow("Error connecting: " & LastException.Message, False)
      BT="Off"
      BTOff
   Else
      BT="On"
      BTOn
      Timer1.Initialize("Timer1", 100) ' 1000 = 1 second
         Timer1.Enabled = True
        
      If AStream.IsInitialized = False Then
      'AStream.Initialize(serial1.InputStream, serial1.OutputStream, "AStream")
        AStream.InitializePrefix(serial1.InputStream, False, serial1.OutputStream, "AStream")
      End If    

   End If
      
End Sub

Sub AStream_Error
    Msgbox("Connection is broken. GrahamCommand will restart","GC")
   serial1.Disconnect''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   'Activity.Finish
   ExitApplication
End Sub

Sub AStream_Terminated
  BTOff
  AStream_Error
End Sub
 
Top