#Region Project Attributes
[INDENT]#ApplicationLabel: POSSYNCH
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False[/INDENT]
#End Region
#Region Activity Attributes
[INDENT]#FullScreen: True
#IncludeTitle: True[/INDENT]
#End Region
Sub Process_Globals
[INDENT]'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Socket1 As Socket
Dim AStreams As AsyncStreams[/INDENT]
End Sub
Sub Globals
[INDENT]'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.'
Dim conv As ByteConverter
Dim Bmp As Bitmap
Private ImageView1 As ImageView
Dim Base64Con As Base64Convert
Dim cIP As String
Dim cCommand As String
Dim cPackage As String[/INDENT]
End Sub
Sub Activity_Create(FirstTime As Boolean)
[INDENT]If FirstTime Then
[INDENT]Activity.Title="Utility"
cIP="192.168.10.32"
Socket1.Initialize("Socket1")
Socket1.Connect(cIP, 2519, 10000)
Activity.LoadLayout("Main")[/INDENT]
End If [/INDENT]
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean
'
End Sub
Sub Activity_Resume
[INDENT]Return True[/INDENT]
End Sub
Sub Activity_Pause (UserClosed As Boolean)
[INDENT]Return True[/INDENT]
End Sub
'----------------------------------SOCKET HANDLING--------------------------------------
Sub Socket1_Connected (Successful As Boolean)
[INDENT]If Successful Then
[INDENT]ToastMessageShow("Connected!", False)
AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
'If lLogin Then
' OpenTable(cSQLQuery)
'End If[/INDENT]
Else
[INDENT]ToastMessageShow("Error while connecting To POS Server !", False)
ErrorConnectionDialog[/INDENT]
End If[/INDENT]
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
Dim su As StringUtils
Dim aData() As Byte
[INDENT]msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
If cCommand="F1" Then
[INDENT]If msg.Contains("PXENDOFIMAGE|") = True Then
[INDENT]'METHOD 1
' USING THIS METHOD IT SAVED THE IMAGE, BUT THE IMAGE DISPLAYED WRONGLY
Dim out As OutputStream
aData = su.DecodeBase64(cPackage)
out = File.OpenOutput(File.DirRootExternal, "newimage.jpg", False)
out.WriteBytes(aData, 0, aData.Length)
out.Close
'===============================================================================
' METHOD 2
' USING THIS METHOD RAISE ERROR
' aData=conv.StringToBytes(cPackage, "UTF8")
' Base64Con.DecodeToImage(cPackage)
'================================================================================
'METHOD 3
' USING THIS METHOD IT SAVED THE IMAGE, BUT THE IMAGE DISPLAYED WRONGLY - SAME RESULT TO METHOD 1
'ImageView1.Bitmap = BytesToImage(aData)
cPackage = ""[/INDENT]
Else
[INDENT]cPackage= cPackage & msg[/INDENT]
End If[/INDENT][/INDENT]
[INDENT]End If[/INDENT]
End Sub
Public Sub BytesToImage(bytes() As Byte) As Bitmap
Dim In As InputStream
[INDENT]In.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim Bmp As Bitmap
Bmp.Initialize2(In)
Return Bmp[/INDENT]
End Sub
Sub SendData2Socket(cData As String)
Dim Buffer() As Byte
Dim data As String
Dim conv As ByteConverter
Dim su As StringUtils
[INDENT]Try
[INDENT]If AStreams.IsInitialized = False Then
[INDENT]ToastMessageShow("Initializing...",False)
ErrorConnectionDialog[/INDENT]
Else[/INDENT]
[INDENT][INDENT]Buffer=conv.StringToBytes(cData,"UTF8")
data=su.EncodeBase64( Buffer)
Buffer=data.getbytes("UTF8")
AStreams.Write(Buffer)[/INDENT]
End If[/INDENT]
Catch
[INDENT]ToastMessageShow(LastException.Message, True)[/INDENT]
End Try [/INDENT]
End Sub
Sub AStreams_Error()
'ErrorConnectionDialog
End Sub
Sub AStreams_Terminated()
'ErrorConnectionDialog
End Sub
'-----------------------------------------------------------END OF SOCKET------------------------------------
Sub ErrorConnectionDialog
Dim cStr As String
Dim Id As InputDialog
[INDENT]Id.InputType = Id.INPUT_TYPE_TEXT
Id.Input = cIP
Id.Hint = ""
Id.HintColor = Colors.ARGB(196, 255, 140, 0)
Bmp.Initialize(File.DirAssets, "stop.png")[/INDENT]
[INDENT]Try
[INDENT]cStr= DialogResponse.CANCEL
cStr= Id.Show( "Connection Error Found On IP " & cIP, "", "Retry", "Cancel", "", Bmp)
If cStr="-1" Then
[INDENT]cIP=Id.Input
Socket1.Close
Socket1.Initialize("Socket1")
Socket1.Connect(cIP, 2519, 10000)[/INDENT]
End If[/INDENT]
Catch
[INDENT]ToastMessageShow(LastException.Message, True)[/INDENT]
End Try[/INDENT]
End Sub
Sub Button1_Click
[INDENT]cCommand="F1"
cPackage=""
' REQUEST TO DOWNLOAD IMAGE ID0001
SendData2Socket ( "F1|ID0001|" )[/INDENT]
End Sub