#Region Project Attributes
#ApplicationLabel: Raspberry BT
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region
Sub Process_Globals
Private astream As AsyncStreams
Private serial As Serial
Private connected As Boolean
End Sub
Sub Globals
Private txtMAC As EditText
Private lblState As Label
Private txtLogs As EditText
Private btnConnect As Button
Private btnDisconnect As Button
Private btn_accendi As Button
Private lbl_led As Label
Dim stato_led As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
serial.Initialize("serial")
End If
Activity.LoadLayout("1")
txtMAC.Text = "B8:27:EB:D6:79:DF"
End Sub
Sub Activity_Resume
UpdateState
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub UpdateState
btnConnect.Enabled = serial.IsEnabled And Not(connected)
btnDisconnect.Enabled = connected
btn_accendi.Enabled = connected
' For Each v As View In pnlSwitches
' v.Enabled = connected
' Next
If connected Then
lblState.TextColor = Colors.Green
lblState.Text = "Connected"
Else
lblState.TextColor = Colors.Red
lblState.Text = "Not connected"
End If
End Sub
'ON / OFF led
Sub btn_accendi_Click
'If Checked Then state = 1 Else state = 0
'astream.Write(Array As Byte(v.Switch.Tag, state))
'ON
Dim c2 As ColorDrawable
If stato_led = 1 Then
astream.Write(Array As Byte(1, 0))
stato_led = 0
c2.Initialize2(Colors.Red,50dip,1dip,Colors.White)
txtLogs.Text = $"State Remote Led 1 is OFF"$
Else
astream.Write(Array As Byte(1, 1))
stato_led = 1
c2.Initialize2(Colors.Green,50dip,1dip,Colors.White)
txtLogs.Text = $"State Remote Led 1 is ON"$
End If
lbl_led.Background = c2
End Sub
Sub btnConnect_Click
Try
serial.Connect(txtMAC.Text)
Catch
Log(LastException)
ToastMessageShow(LastException.Message, True)
End Try
End Sub
Sub Serial_Connected (Success As Boolean)
Log($"Connected ${Success}"$)
If Success Then
If astream.IsInitialized Then astream.Close
astream.InitializePrefix(serial.InputStream, True, serial.OutputStream, "astream")
connected = True
UpdateState
End If
End Sub
Sub btnDisconnect_Click
serial.Disconnect
astream.Close
connected = False
UpdateState
End Sub
Sub astream_NewData (Buffer() As Byte)
Dim state As String
If Buffer(1) = 1 Then state = "ON" Else state = "OFF"
txtLogs.Text = $"State Remote Led ${Buffer(0)} is ${state}"$
Dim c2 As ColorDrawable
If Buffer(1) = 1 Then
c2.Initialize2(Colors.Green,50dip,1dip,Colors.White)
Else
c2.Initialize2(Colors.Red,50dip,1dip,Colors.White)
End If
lbl_led.Background = c2
End Sub
Sub astream_Error
astream_Terminated
End Sub
Sub astream_Terminated
connected = False
UpdateState
End Sub