Buenas tardes,
Tengo una APP que conecta a traves de Bluetooth "clasico" con una cerradura electronica, la APP funciona correctamente, pero ahora la nueva version de la cerradura usa Bluetooth BLE para ahorrar bateria y ya no funciona la APP.
Dejo aqui el codigo que uso en la APP (uso la clase Asynstreamtext) a ver si podeis orientarme en alguna manera de adaptarla para el BLE.
Y esta es la clase AsynsStreamText:
Tengo una APP que conecta a traves de Bluetooth "clasico" con una cerradura electronica, la APP funciona correctamente, pero ahora la nueva version de la cerradura usa Bluetooth BLE para ahorrar bateria y ya no funciona la APP.
Dejo aqui el codigo que uso en la APP (uso la clase Asynstreamtext) a ver si podeis orientarme en alguna manera de adaptarla para el BLE.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim btAdmin As BluetoothAdmin
Dim btDispositivo As Serial
Dim AStreams As AsyncStreamsText
End Sub
Sub Activity_Create
btAdmin.Initialize("BlueTeeth")
btDispositivo.Initialize("InicializarDispositivo")
InicializarDispositivo
End Sub
Sub InicializarDispositivo As Boolean
Dim PairedDevices As Map
Dim L As List
Dim Res As Int
Dim Resultado As Boolean
Resultado = True
EscribirLog("Conectando con la cerradura...")
PairedDevices.Initialize
Try
PairedDevices = btDispositivo.GetPairedDevices
Catch
EscribirLog("Error de conexión...")
btDispositivo.Disconnect
Resultado = False
End Try
If PairedDevices.Size = 0 Then
EscribirLog("Error Conectando - Ningun dispositivo encontrado...")
Resultado = False
Else
Dim PorDefecto As Int = -1
L.Initialize
For i = 0 To PairedDevices.Size - 1
L.Add(PairedDevices.GetKeyAt(i))
If Main.IDDispositivo = PairedDevices.GetValueAt(i) Then PorDefecto = i
Next
If (Main.IDDispositivo <> "") And (PorDefecto <> -1) Then
'ToastMessageShow(Main.IDDispositivo, True)
btDispositivo.Connect(PairedDevices.Get(L.Get(PorDefecto)))
Else
Res = InputList(L, "Elige dispositivo", -1)
If Res <> DialogResponse.CANCEL Then
Main.IDDispositivo = PairedDevices.GetValueAt(Res)
funciones.EscConfig
btDispositivo.Connect(PairedDevices.Get(L.Get(Res)))
Else
Resultado = False
End If
End If
End If
Return Resultado
End Sub
Public Sub SendMessage (msg As String)
EscribirLog(">> " & msg)
AStreams.Write(msg&Chr(10)&Chr(13))'.GetBytes("utf8"))
End Sub
Sub InicializarDispositivo_Connected (Success As Boolean)
If Success Then
EscribirLog("Conexión establecida")
If AStreams.IsInitialized Then AStreams.Close
AStreams.Initialize(Me, "AStreams", btDispositivo.InputStream, btDispositivo.OutputStream)
EscribirLog("Enviando comando a la cerradura...")
SendMessage(CEI & "CM:FT" & CEF)
'AQUI HABRIA QUE ESPERAR A QUE NOS DEVUELVA ALGO LA CERRADURA
Wait for AStreams_NewText(Respuesta As String)
EscribirLog("Respuesta: " & Respuesta)
End If
End Sub
Y esta es la clase AsynsStreamText:
B4X:
#Event: NewText (Text As String)
#Event: Terminated
'version: 1.00
'Class module
Sub Class_Globals
Private mTarget As Object
Private mEventName As String
Private astreams As AsyncStreams
Public charset As String = "UTF8"
Private sb As StringBuilder
End Sub
Public Sub Initialize (TargetModule As Object, EventName As String, In As InputStream, out As OutputStream)
mTarget = TargetModule
mEventName = EventName
astreams.Initialize(In, out, "astreams")
sb.Initialize
End Sub
'Sends the text. Note that this method does not add end of line characters.
Public Sub Write(Text As String)
astreams.Write(Text.GetBytes(charset))
End Sub
Private Sub astreams_NewData (Buffer() As Byte)
Dim newDataStart As Int = sb.Length
sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
Dim s As String = sb.ToString
Dim start As Int = 0
For i = newDataStart To s.Length - 1
Dim c As Char = s.CharAt(i)
If i = 0 And c = Chr(10) Then '\n...
start = 1 'might be a broken end of line character
Continue
End If
If c = Chr(10) Then '\n
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
start = i + 1
Else If c = Chr(13) Then '\r
CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
i = i + 1
End If
start = i + 1
End If
Next
If start > 0 Then sb.Remove(0, start)
End Sub
Private Sub astreams_Terminated
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Private Sub astreams_Error
Log("error: " & LastException)
astreams.Close
CallSubDelayed(mTarget, mEventName & "_Terminated")
End Sub
Public Sub Close
astreams.Close
End Sub