Eccomi!
Ho provato a realizzare il programma, ho dovuto usare anche la funzione asyncstream però, visto che con solo il socket non riesco a mandare i dati allo strumento.
Ecco il codice che ho scritto:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim Server As ServerSocket
Dim Socket1 As Socket
Dim AStreams As AsyncStreams
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Button1 As Button
Private EditText1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Main")
If FirstTime Then
Server.Initialize(5555, "Server")
Server.Listen
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Button1_Click
Socket1.Initialize("Socket1")
Socket1.Connect("192.168.1.2", 5555, 5000)
End Sub
Sub Socket1_Connected(Connected As Boolean)As Boolean
If Connected = True Then
ToastMessageShow("Connesso",True)
Else
ToastMessageShow("Errore",True)
End If
End Sub
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
AStreams.Initialize(Socket1.InputStream, Socket1.OutputStream, "AStreams")
If Successful Then
Socket1 = NewSocket
AStreams.write(Array As Byte(0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x2C))
ToastMessageShow("Dati inviati",True)
End If
Server.Listen
End Sub
Sub AStreams_NewData (Buffer() As Byte)
Dim a As Byte
a = AStreams.StreamReceived
EditText1.Text = a
End Sub
La stringa di byte "0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x2C", direi che venga mandata correttamente allo strumento, ma non ricevo nessuna risposta da esso. Non so se ho sbagliato qualcosa io nel programma oppure il problema è altrove (visto che è ancora un prototipo, ma penso che sia il mio codice che ha qualche problema).
Quando la connessione tramite socket avviene, mi viene correttamente visualizzato "Connesso". Compare anche la scritta "Dati inviati".
Ora, mi stavo chiedendo: non è che per caso devo usare una libreria per il wifi-direct anzichè collegare tramite wifi il telefono allo strumento "normalmente"? Potrebbe essere questo il problema? Un pò lo escludo però, visto che almeno il socket sembra collegarsi senza problemi....
P.s. L' indirizzo e la porta della scheda sono corretti
Dimenticavo una cosa importantissima: lo strumento come risposta deve restituirmi la stessa stringa che gli ho inviato cioè "0x16, 0x08, 0x00, 0x00, 0x00, 0x00, 0xE3, 0x2C", non so se con o senza le virgole.