
#Region  Bluetooth Devices
	Public Serial1 As Serial
	Public Serial2 As Serial
	Public Serial3 As Serial
	
	'Public Bluetooth_Admin_Hardware_On_Off As BluetoothAdmin
	Public Bluetooth_Admin_1 As BluetoothAdmin
	Public Bluetooth_Admin_2 As BluetoothAdmin
	Public Bluetooth_Admin_3 As BluetoothAdmin
	
	Public Bluetooth_Device_1_Ast As AsyncStreamsText
	Public Bluetooth_Device_2_Ast As AsyncStreamsText
	Public Bluetooth_Device_3_Ast As AsyncStreamsText
	
	Public Clock_Reconnect_Bluetooth_Device_1 As Timer
	Public Clock_Reconnect_Bluetooth_Device_2 As Timer
	Public Clock_Reconnect_Bluetooth_Device_3 As Timer
	
	Public Starter_Clock_Main_Timer As Timer
	Public BluetoothState, ConnectionState As Boolean
	Public Bluetooth_Toast_Messages As DynamicToast
	
	
	
	Public Com1_Is_Connected As Byte
	Public Com2_Is_Connected As Byte
	Public Com3_Is_Connected As Byte
	
	Public Com1_In_Data As String
	Public Com2_In_Data As String
	Public Com3_In_Data As String
	
#End Region

End Sub
Sub Service_Create
	Is_Setup_Mode_Enabled = "0"
	Log("Service_Create*********")
#Region  General Timer
	Service_Timer.Initialize("Service_Timer", 2000)
	Service_Timer.Enabled=True
#End Region	

#Region  Bluetooth 
	Int_Bluetooth_Admin_Hardware
	Turn_Bluetooth_Hardware_On
	Int_Bluetooth_Admin_1_TO_3
	Int_Bluetooth_Ports
	Int_All_Bluetooth_Timers
	Clock_Reconnect_Bluetooth_Device_1.Enabled=False
	Clock_Reconnect_Bluetooth_Device_2.Enabled=False
	Clock_Reconnect_Bluetooth_Device_3.Enabled=False
	Starter_Clock_Main_Timer.Enabled=True
#End Region	


End Sub
Sub Service_Start (StartingIntent As Intent)
	
End Sub
Sub Service_TaskRemoved
	'This event will be raised when the user removes the app from the recent apps list.
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

'**********************************************************************************************************************

#Region  Bluetooth          >>
Sub Int_All_Bluetooth_Timers
	Clock_Reconnect_Bluetooth_Device_1.Initialize("Clock_Reconnect_Bluetooth_Device_1", 5000)
	Clock_Reconnect_Bluetooth_Device_2.Initialize("Clock_Reconnect_Bluetooth_Device_2", 5000)
	Clock_Reconnect_Bluetooth_Device_3.Initialize("Clock_Reconnect_Bluetooth_Device_3", 5000)
	
	Starter_Clock_Main_Timer.Initialize("Starter_Clock_Main_Timer", 2000)
End Sub
Sub Starter_Clock_Main_Timer_Tick
	If Com1_Is_Connected = 0 And Main.Can_Service_Timer_Run_For_Com1_Service_Module = 1 And Clock_Reconnect_Bluetooth_Device_1.Enabled=False Then
		If Main.Bluetooth_Address1_for_Service_Module ="00:00:00:00:00:00:00" Then
		Else
			Log("Connecting Bluetooth 1 Device Now..! >" & Main.Bluetooth_Address1_for_Service_Module)
			Clock_Reconnect_Bluetooth_Device_1.Enabled=True
		End If
	End If
	
	If Com2_Is_Connected = 0 And Main.Can_Service_Timer_Run_For_Com2_Service_Module = 1 And Clock_Reconnect_Bluetooth_Device_2.Enabled=False Then
		If Main.Bluetooth_Address2_for_Service_Module ="00:00:00:00:00:00:00" Then
		Else
			Log("Connecting Bluetooth 2 Device Now..! >" & Main.Bluetooth_Address2_for_Service_Module)
			Clock_Reconnect_Bluetooth_Device_2.Enabled=True
		End If
	End If
	
	If Com3_Is_Connected = 0 And Main.Can_Service_Timer_Run_For_Com3_Service_Module = 1 And Clock_Reconnect_Bluetooth_Device_3.Enabled=False Then
		If Main.Bluetooth_Address3_for_Service_Module ="00:00:00:00:00:00:00" Then
		Else
			Log("Connecting Bluetooth 3 Device Now..! >" & Main.Bluetooth_Address3_for_Service_Module)
			Clock_Reconnect_Bluetooth_Device_3.Enabled=True
		End If
	End If
	
	
	
	If  Main.Can_Service_Timer_Run_For_Com1_Service_Module = 0 And Com1_Is_Connected = 1 Then
		Log("Dis-Connecting Bluetooth 1 Device Now..! >" & Main.Bluetooth_Address1_for_Service_Module)
		Clock_Reconnect_Bluetooth_Device_1.Enabled=False
		Disconnect_Bluetooth_Device_1
	End If
	If  Main.Can_Service_Timer_Run_For_Com2_Service_Module = 0 And Com2_Is_Connected = 1 Then
		Log("Dis-Connecting Bluetooth 2 Device Now..! >" & Main.Bluetooth_Address2_for_Service_Module)
		Clock_Reconnect_Bluetooth_Device_2.Enabled=False
		Disconnect_Bluetooth_Device_2
	End If
	If  Main.Can_Service_Timer_Run_For_Com3_Service_Module = 0 And Com3_Is_Connected = 1 Then
		Log("Dis-Connecting Bluetooth 3 Device Now..! >" & Main.Bluetooth_Address3_for_Service_Module)
		Clock_Reconnect_Bluetooth_Device_3.Enabled=False
		Disconnect_Bluetooth_Device_3
	End If	
End Sub
Sub Int_Bluetooth_Admin_Hardware
	'Bluetooth_Admin_Hardware_On_Off.Initialize("Bluetooth_Admin_Hardware_On_Off")
End Sub
Sub Bluetooth_Admin_Hardware_On_Off_StateChanged(NewState As Int, OldState As Int)
	'Log("Bluetooth Hardware State StateChanged " & "New State" & NewState & " Old State" & OldState)
	'BluetoothState = NewState = Bluetooth_Admin_Hardware_On_Off.STATE_ON
	'Log("Bluetooth Hardware State " & BluetoothState)	
End Sub
Sub Turn_Bluetooth_Hardware_On
	Some_Toggle_Functions.TurnBluetoothOn
	'Bluetooth_Admin_Hardware_On_Off.Enable
	Bluetooth_Toast_Messages.SuccessMessageToast = "Bluetooh Hardware is On"
End Sub
Sub Turn_Bluetooth_Hardware_Off
	Some_Toggle_Functions.TurnBluetoothOff
	''''Bluetooth_Admin_Hardware_On_Off.Disable
	Bluetooth_Toast_Messages.ErrorMessageToast = "Bluetooh Hardware is Off"
End Sub
Sub Int_Bluetooth_Admin_1_TO_3
	Bluetooth_Admin_1.Initialize("Bluetooth_Admin_1")
	Bluetooth_Admin_1.Enable
	Sleep(100)
	Bluetooth_Admin_2.Initialize("Bluetooth_Admin_2")
	Bluetooth_Admin_2.Enable
	Sleep(100)
	Bluetooth_Admin_3.Initialize("Bluetooth_Admin_3")
	Bluetooth_Admin_3.Enable
	Sleep(100)
	
End Sub
Sub Int_Bluetooth_Ports
	Serial1.Initialize("Serial1")
	Sleep(100)
	Serial2.Initialize("Serial2")
	Sleep(100)
	Serial3.Initialize("Serial3")
	Sleep(100)	
End Sub
Sub Clock_Reconnect_Bluetooth_Device_1_Tick
	If Some_Toggle_Functions.Bluetooth = False Then
		Turn_Bluetooth_Hardware_On
		Return
	End If
	If Main.Bluetooth_Address1_for_Service_Module = "00:00:00:00:00:00:00" Or Main.Bluetooth_Address1_for_Service_Module = "null"  Then
		Return
	End If
	Log("Clock Trying To Connect / Reconnect Bluetooth Device 1 Running >" & Main.Bluetooth_Address1_for_Service_Module)
	Serial1.ConnectInsecure(Bluetooth_Admin_1,Main.Bluetooth_Address1_for_Service_Module,1)
	Clock_Reconnect_Bluetooth_Device_1.Enabled=False	
End Sub
Sub Clock_Reconnect_Bluetooth_Device_2_Tick
	If Some_Toggle_Functions.Bluetooth = False Then
		Turn_Bluetooth_Hardware_On
		Return
	End If
	If Main.Bluetooth_Address2_for_Service_Module = "00:00:00:00:00:00:00" Or Main.Bluetooth_Address2_for_Service_Module = "null" Then
		Return
	End If
	Log("Clock Trying To Connect / Reconnect Bluetooth Device 2 Running >" & Main.Bluetooth_Address2_for_Service_Module)
	Serial2.ConnectInsecure(Bluetooth_Admin_2,Main.Bluetooth_Address2_for_Service_Module,1)
	Clock_Reconnect_Bluetooth_Device_2.Enabled=False
End Sub
Sub Clock_Reconnect_Bluetooth_Device_3_Tick
	If Some_Toggle_Functions.Bluetooth = False Then
		Turn_Bluetooth_Hardware_On
		Return
	End If
	If Main.Bluetooth_Address3_for_Service_Module = "00:00:00:00:00:00:00" Or Main.Bluetooth_Address3_for_Service_Module = "null" Then
		Return
	End If
	Log("Clock Trying To Connect / Reconnect Bluetooth Device 3 Running >" & Main.Bluetooth_Address3_for_Service_Module)
	Serial3.ConnectInsecure(Bluetooth_Admin_3,Main.Bluetooth_Address3_for_Service_Module,1)
	Clock_Reconnect_Bluetooth_Device_3.Enabled = False
End Sub

Sub Serial1_Connected (Success1 As Boolean)
	If Success1 = True Then
		Bluetooth_Device_1_Ast.Initialize(Me, "Bluetooth_Device_1_Ast", Serial1.InputStream, Serial1.OutputStream)
		Serial1.Listen
		Log("{Com1} Bluetooth Device Connected")
		Clock_Reconnect_Bluetooth_Device_1.Enabled = False
		Com1_Is_Connected = 1
	End If
End Sub
Sub Serial2_Connected (Success1 As Boolean)
	If Success1 = True Then
		Bluetooth_Device_2_Ast.Initialize(Me, "Bluetooth_Device_2_Ast", Serial2.InputStream, Serial2.OutputStream)
		Serial2.Listen
		Log("{Com2} Bluetooth Device Connected")
		Clock_Reconnect_Bluetooth_Device_2.Enabled = False
		Com2_Is_Connected = 1
	End If
End Sub
Sub Serial3_Connected (Success1 As Boolean)
	If Success1 = True Then
		Bluetooth_Device_3_Ast.Initialize(Me, "Bluetooth_Device_3_Ast", Serial3.InputStream, Serial3.OutputStream)
		Serial3.Listen
		Log("{Com3} Bluetooth Device Connected")
		Clock_Reconnect_Bluetooth_Device_3.Enabled = False
		Com3_Is_Connected = 1
	End If
End Sub
Sub Bluetooth_Device_1_Ast_NewText(Text1 As String)
	'Log(Text1)
	Com1_In_Data = Text1
	'Log(Text1)
End Sub
Sub Bluetooth_Device_2_Ast_NewText(Text2 As String)
	'Log(Text2)
	Com2_In_Data = Text2
End Sub
Sub Bluetooth_Device_3_Ast_NewText(Text3 As String)
	'Log(Text3)
	Com3_In_Data = Text3
End Sub
Sub Bluetooth_Device_1_Ast_Terminated
	Log("Bluetooth_Device_1_Ast_Terminated")
	Disconnect_Bluetooth_Device_1
	Com1_Is_Connected = 0
	Com1_In_Data = ""
End Sub
Sub Bluetooth_Device_1_Ast_Error
	Log("Bluetooth_Device_1_Ast_Error")
	Disconnect_Bluetooth_Device_1
	Com1_Is_Connected = 0
	Com1_In_Data = ""
End Sub
Sub Bluetooth_Device_2_Ast_Terminated
	Log("Bluetooth_Device_2_Ast_Terminated")
	Disconnect_Bluetooth_Device_2
	Com2_Is_Connected = 0
	Com2_In_Data = ""
End Sub
Sub Bluetooth_Device_2_Ast_Error
	Log("Bluetooth_Device_2_Ast_Error")
	Disconnect_Bluetooth_Device_2
	Com2_Is_Connected = 0
	Com2_In_Data = ""
End Sub
Sub Bluetooth_Device_3_Ast_Terminated
	Log("Bluetooth_Device_3_Ast_Terminated")
	Disconnect_Bluetooth_Device_3
	Com3_Is_Connected = 0
	Com3_In_Data = ""
End Sub
Sub Bluetooth_Device_3_Ast_Error
	Log("Bluetooth_Device_3_Ast_Error")
	Disconnect_Bluetooth_Device_3
	Com3_Is_Connected = 0
	Com3_In_Data = ""
End Sub
Sub Disconnect_Bluetooth_Device_1
	If Bluetooth_Device_1_Ast.IsInitialized = True Then
		Bluetooth_Device_1_Ast.Close
		Serial1.Disconnect
		Com1_Is_Connected = 0
	Log("Com1 Bluetooth Disconnected")	
	End If
End Sub
Sub Disconnect_Bluetooth_Device_2
	If Bluetooth_Device_2_Ast.IsInitialized = True Then
		Bluetooth_Device_2_Ast.Close
		Serial2.Disconnect
		Log("Com2 Bluetooth Disconnected")
		Com2_Is_Connected = 0
	End If
End Sub
Sub Disconnect_Bluetooth_Device_3
	If Bluetooth_Device_3_Ast.IsInitialized = True Then
		Bluetooth_Device_3_Ast.Close
		Serial3.Disconnect
		Log("Com3 Bluetooth Disconnected")	
		Com3_Is_Connected = 0
	End If
End Sub

#End Region
