AsyncStreamsText issue

wantlin

Member
Licensed User
Longtime User
Hi Erel

I have a problem for AsyncStreamsText function .
My problem : First time connect the Bluetooth,use AsyncStreamsText .
It is can run the blueService_NewText command
When Bluetooth disconnect , I will re connect the device , I use
the blueService_NewText of AsyncStreamsText , it can not read
the value .
Can you inform me how do the re-call the AsyncStreamsText ?

Thank you !:sign0085:
 

wantlin

Member
Licensed User
Longtime User
Hi Erel

The code is for connect Bluetooth . The Sub code name is check_bluetooth . The sub code is re connect the Bluetooth device . If it finished the connect , I will call StartService(blueService) .Please help me . Thank you !


Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim BtAdmin As BluetoothAdmin
Dim BtSerial As Serial
Dim Broadcast As BroadCastReceiver

Dim DeviceName As String = ""
Dim DeviceMac As String = ""
End Sub
Sub Service_Create
BtAdmin.Initialize("BtAdmin")
BtAdmin.Enable
BtSerial.Initialize("BtSerial")
'Broadcast_Init
End Sub

Sub Service_Start (StartingIntent As Intent)
Log("Start discovery BtAdmin")
BtAdmin.StartDiscovery

End Sub

Sub Service_Destroy

End Sub
Sub bluedissconnect
BtAdmin.Disable
BtSerial.Disconnect
End Sub
Sub check_bluetooth
Log("Check Bluetooth")
'
BtAdmin.Disable
BtSerial.Disconnect
Wait(2)
StopService(blueService)
BtAdmin.Enable
Wait(2)
BtAdmin.StartDiscovery
End Sub

Sub BtAdmin_DiscoveryFinished 'Event handler
Log("Discovery stopped")
End Sub

Sub BtAdmin_StateChanged (NewState As Int, OldState As Int) 'Event handler
If (NewState =BtAdmin.STATE_OFF) Then
BtAdmin.Enable
End If
End Sub

Sub BtAdmin_DeviceFound (Name As String, Mac As String) 'Event handler
'---- Cancel discovery
'BtAdmin.CancelDiscovery
'---- Capture info. of the device that is found
DeviceName = Name
DeviceMac = Mac
Log("Device found: " & DeviceName & ", " & DeviceMac)
'---- Connect to device here
'Add here conditions to connect to this device or not
'For example:
'if DeviceName="abc" then
' BtSerial_Connect(DeviceMac)
'end if
'If the device has not been previously paired, this connect command will trigger the pairing-request intent
'BtSerial.Connect(DeviceMac)

If DeviceName="YFRobot" Then
BtAdmin.CancelDiscovery
BtSerial.Connect(DeviceMac)
'BtSerial.Listen
'time1.Initialize("time1",5000)
'time1.Enabled=True
'Disconnect_1.Enabled=False
'reconnect_mark=True
'state_check=True

End If
End Sub

Sub BtSerial_Connected (Success As Boolean) ' Event handler
If (Success=True) Then
Log("Connected")
If Main.bluetooth_state_ini=False Then
Main.bluetooth_state_ini=True
StartService(blueService)
Else
Log("Connected for re connect ")
' Main.send_cnt=0
' Main.read_str=""
' blueService.data_send_cnt=0
' blueService.timer1.Enabled = True
' CallSubDelayed (blueService,"btnSend_Click")
StartService(blueService)
End If
Else
Log("Not Connected: " & LastException.Message)
End If
End Sub



The other code :blueService

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private ast As AsyncSteamsText


Dim strReturnValue As String
Dim timer1 As Timer
Dim timer1 As Timer
Dim data_send_cnt As Int
Dim data_send_state As Boolean
Dim nNotify As Notification
End Sub
Sub Service_Create
Log("Service_Create OK " )
Main.send_cnt=0
Main.read_cnt=0
data_send_cnt=0
data_send_state=False

End Sub



Sub Service_Start (StartingIntent As Intent)
' If blueService.IsInitialized Then blueService.Close ' In case the connection was broken and restarted
' blueService.Initialize(clsTestObj.scanSerial.InputStream, clsTestObj.scanSerial.OutputStream, "blueService")
Log("Service_Start OK " )
If ast.IsInitialized = False Then
ast.Initialize(Me, "blueService", blueconnect.BtSerial.InputStream,blueconnect.BtSerial.OutputStream)
End If
timer1.Initialize("Timer1", 300)
timer1.Enabled = True
data_send_cnt=0
data_send_state=False
'btnSend_Click
End Sub

Sub Service_Destroy
ast.Close

End Sub


Sub blueService_NewText(Text As String)
Log("Text: " & Text & "-" & Text.Length )
If Text <> "+CONNECTED" Then
Main.read_str=Text
Main.read_cnt=SInt(Main.read_str)
Log(" send_cnt and read data " & Main.send_cnt & Main.read_cnt )
End If

If IsPaused(Main) = False Then
Log(" IsPaused is False " )
CallSubDelayed (Main,"showdata")
End If

End Sub


Sub blueService_Error
' timer1.Enabled = False
' Main.state_check=False
' 'ast.Close
' Main.BtSerial.Disconnect
' Main.reconnect_mark =True
' If Main.ringtone_state=True Then
' ' mp.Load(rm.GetContentDir, rm.GetDefault(rm.TYPE_NOTIFICATION))
' 'Log("rm.GetContentDir->" & rm.GetDefault(rm.TYPE_NOTIFICATION) )
' Main.mp.Load(Main.rm.GetContentDir,Main.ringtype_conbl)
' Main.mp.Play
' End If
' If Main.tts_state=True Then
' Main.TTS1_Speak="Your bluetooth connection is broken please check it"
' CallSubDelayed (Main,"TTS1_SPEK")
' End If
' Main.BtSerial.Disconnect
' Main.reconnect_mark =True

'CallSubDelayed (Main,"btnStartDiscovery_Click")
'CallSubDelayed (Main,"Activity_Resume")
End Sub

Sub blueService_Terminated
blueService_Error
End Sub

Sub btnSend_Click
Log("btnSend_Click start " )
If Main.send_cnt <200 Then
Main.send_cnt=Main.send_cnt+1
Main.send_str=CStr(Main.send_cnt)
Main.read_str=""
Else
Main.send_cnt=0
Main.read_str=""
End If
Log("btnSend_Write start-> " & Main.send_str )
ast.Write(Main.send_str & Chr(10))



'Main.BtSerial.l
End Sub
Sub Timer1_Tick
If Main.send_cnt = Main.read_cnt AND data_send_cnt<3 Then
Log(" send_cnt1 " )
data_send_state=True
'Main.state_check=True
'Main.re_connect_cnt=0
Else If Main.send_cnt <> Main.read_cnt AND data_send_cnt<3 Then
Log(" send_cnt2 " )
data_send_state=True
' Main.state_check=True
'Main.re_connect_cnt=0
data_send_cnt=data_send_cnt+1
Else If Main.send_cnt <> Main.read_cnt AND data_send_cnt> 2 Then
Log(" send_cnt3 " )
data_send_state=False
data_send_cnt=100

End If
If data_send_state =True AND data_send_cnt <3 Then
Log(" btnSend_Click" )
btnSend_Click
Else If data_send_state =False AND data_send_cnt =100 Then
Log(" blueService_Terminated" )
' Wait(3)
' blueService_Terminated
timer1.Enabled = False
ast.Close
CallSubDelayed (blueconnect,"check_bluetooth")
End If
End Sub


Sub SInt(i As String) As Int

Return "" & i
End Sub
Sub CStr(i As Int) As String
Return "" & i
End Sub
Sub Wait(Seconds As Int)
'Sleep for defined seconds
Dim Ti As Long
Ti = DateTime.Now + (Seconds * 1000)
Do While DateTime.Now < Ti
DoEvents
Loop
End Sub
 
Upvote 0

wantlin

Member
Licensed User
Longtime User
Hi Erel

Thank you ! I will add the [ code ] [ /code ] command for next post .
I try it !
Thank you !
 
Upvote 0
Top