Hello all,
This is my full code for an applicaion that will send one bye to HC-06 Bluetooth.
I keep getting the following error:
Logs:
Main - 54: Array expected.
Main - 96: Unknown member: write
Library 'RandomAccessFile' is not used. (warning #32)
Any help or pointing to the error source appreciated
==================================
This is my full code for an applicaion that will send one bye to HC-06 Bluetooth.
I keep getting the following error:
Logs:
Main - 54: Array expected.
Main - 96: Unknown member: write
Library 'RandomAccessFile' is not used. (warning #32)
Any help or pointing to the error source appreciated
==================================
Array Expected Error:
Sub Process_Globals
Private Serial1 As Serial
' Private AStream As AsyncStreams ' No longer needed in newer B4A versions
Private connected As Boolean = False
Private rp As RuntimePermissions
Private HC06_MAC As String = "00:21:13:01:34:57" ' Replace with your actual MAC address
End Sub
Sub Globals
Private btnPulse As Button
Private lblStatus As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
' Loads the layout file named "layout.bal"
Activity.LoadLayout("layout")
' Disable the pulse button initially
btnPulse.Enabled = False
' Set initial status text
lblStatus.Text = "Requesting permissions..."
' Initialize the Serial object (for Bluetooth Classic)
Serial1.Initialize("")
' Call the subroutine to request necessary permissions
RequestPermissions
End Sub
Sub RequestPermissions
' Android 6.0 (Marshmallow) and above require ACCESS_FINE_LOCATION for Bluetooth scanning/connection
If rp.Check(rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
' Request the permission if not already granted
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
' Wait For Activity_PermissionResult (Permission As String, Result As Boolean) will block here
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
' If permission denied, show a toast message and exit the subroutine
ToastMessageShow("Location permission denied.", True)
Return
End If
End If
' Android 12 (API 31) and above require BLUETOOTH_CONNECT
If rp.Check("android.permission.BLUETOOTH_CONNECT") = False Then
' Request the BLUETOOTH_CONNECT permission
rp.CheckAndRequest("android.permission.BLUETOOTH_CONNECT")
' Wait For Activity_PermissionResult (Permission As String, Result As Boolean) will block here
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
' If permission denied, show a toast message and exit
ToastMessageShow("Bluetooth permission denied.", True)
Return
End If
End If
' If all necessary permissions are granted, proceed to connect
ConnectToHC06()
End Sub
Sub ConnectToHC06()
' Attempt to connect to the specified HC-06 Bluetooth module by its MAC address
Serial1.Connect(HC06_MAC)
' Update status label
lblStatus.Text = "Connecting to HC-06..."
End Sub
Sub Serial1_Connected(Success As Boolean)
' This event is raised after the Serial.Connect call
If Success Then
' In newer B4A versions, Serial object often handles streams directly.
' No need to initialize AsyncStreams separately.
connected = True
lblStatus.Text = "Connected"
btnPulse.Enabled = True ' Enable the button only when connected
Else
' If connection fails
lblStatus.Text = "Connection failed"
End If
End Sub
' Sub AStream_Error ' This subroutine is no longer needed as AsyncStreams is removed
' connected = False
' btnPulse.Enabled = False ' Disable the button as connection is lost
' lblStatus.Text = "Connection lost"
' End Sub
Sub Serial1_Error ' Use this event for errors related to the Serial connection
connected = False
btnPulse.Enabled = False
lblStatus.Text = "Serial connection lost/error"
End Sub
Sub btnPulse_Click
' Check if currently connected to the Bluetooth module
If connected Then
' Send the ASCII value for "1" as a single byte array.
' This is a direct and unambiguous way to pass a byte array to a Write method.
Serial1.Write(Array As Byte(49))
lblStatus.Text = "Pulse sent"
Else
lblStatus.Text = "Not connected"
End If
End Sub