Android Question ESP32 bleuttoth connection

fernando.oliboni

Active Member
Licensed User
Longtime User
hello i have an esp32 module with grbl. I can connect this esp32 with another app called grbl controller using bluetooth.
I would like to use B4A to:
1) show available devices
2) choose one and connect.
It is possible ?
Does anyone have an example that works?
Thanks
 

sfsameer

Well-Known Member
Licensed User
Longtime User
GRBL Controller takes 1 second, it's almost instantaneous and the connection always happens.
Hello,

1- it may take a long time because the above library searches for any nearby devices which of course would take a long time, so below is what you should do :

One of the GRBL Controller requirements is :
4. Bluetooth module should be already paired with smart phone.

So first pair the device with your phone, this way you will force the library to get the only paired devices which will be much faster.

2- Then using the serial library :
B4A:
Sub Process_Globals
    Private Serial1 As Serial
    
End Sub
Sub Globals
    Private Connected As Boolean
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Serial1.Initialize("Serial1")
    Connect
End Sub

Sub Connect
    Dim PairedDevices As Map
    PairedDevices = Serial1.GetPairedDevices
    Dim l As List
    l.Initialize
    For i = 0 To PairedDevices.Size - 1
        l.Add(PairedDevices.GetKeyAt(i))
        Serial1.Connect(PairedDevices.Get(l.Get(0))) '----- it will connect to the first paird device
    Next
End Sub

Private Sub Serial1_Connected (Success As Boolean)
    If Success Then
        Connected = True
        Serial1.Listen
        
    Else
        Connected = False
    End If
End Sub

Public Sub IsBluetoothOn As Boolean
    Return Serial1.IsEnabled
End Sub

Sub DisConnect
    Serial1.Disconnect
    Connected = False
End Sub


And in the Serial1_Connected sub just add the AsyncStreams and you will be able to connect really fast and communicate real-time.

Thank you,
Saif
 
Upvote 0

fernando.oliboni

Active Member
Licensed User
Longtime User
[QUOTE = "sfsameer, post: 836237, membro: 43084"]
Olá,

1- pode levar muito tempo porque a biblioteca acima procura qualquer dispositivo próximo, o que obviamente levaria muito tempo, então abaixo está o que você deve fazer:

Um dos requisitos do controlador GRBL é:


Portanto, primeiro emparelhe o dispositivo com seu telefone, dessa forma você forçará a biblioteca a obter os únicos dispositivos emparelhados, o que será muito mais rápido.

2- Em seguida, usando a biblioteca serial:
[CODE lang = "b4x" title = "B4A"]
Sub Process_Globals
Private Serial1 As Serial

End Sub
Sub Globais
Privado conectado como booleano
End Sub
Sub Activity_Create (FirstTime As Boolean)
Serial1.Initialize ("Serial1")
Conectar
End Sub

Sub Connect
Dim PairedDevices As Map
PairedDevices = Serial1.GetPairedDevices
Dim l como lista
l.Inicializar
Para i = 0 Para PairedDevices.Size - 1
l.Add (PairedDevices.GetKeyAt (i))
Serial1.Connect (PairedDevices.Get (l.Get (0))) '----- ele se conectará ao primeiro dispositivo par
Próximo
End Sub

Private Sub Serial1_Connected (sucesso como booleano)
Se tiver sucesso, então
Conectado = Verdadeiro
Serial1.Listen

Senão
Conectado = Falso
Fim se
End Sub

Public Sub IsBluetoothOn As Boolean
Retornar Serial1.IsEnabled
End Sub

Sub DisConnect
Serial1.Disconnect
Conectado = Falso
End Sub

[/CÓDIGO]


E no sub Serial1_Connected basta adicionar AsyncStreams e você poderá se conectar muito rápido e se comunicar em tempo real.

Obrigada,
Saif
[/ CITAR]

Vou tentar muito obrigado
 
Upvote 0

fernando.oliboni

Active Member
Licensed User
Longtime User
What if I sent you an esp32 with grbl installed?
So we can test connecting and sending Gcodes to esp32 and receiving feedback from it.
What do you think?
 
Upvote 0
Top