Android Question BLE2 write not getting to RN4871 connected to micro

dxptech

Member
Licensed User
Longtime User
I'm using the BLE example. I added a button to send a "G" cr to a connected RN4871 microchip BLE module connected to a micro thru a uart. When the micro receives a "G" cr, it turns on an led. I know the RN4871/micro works because I've sent "G" cr from the Microchip App and, with the BLE Ext library example, but, I can't seem to get the BLE2 example to work.
Here is the code:
B4X:
Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public connected As Boolean = False
    Public ConnectedName As String
    Private ConnectedServices As List
    Public rp As RuntimePermissions
    Public DATA_SERVICE As String = "49535343-fe7d-4ae5-8fa9-9fafd205e455" 'data service for RN-BLE RN4871, write to pc
    Public DATA_UART_RX As String = "49535343-8841-43f4-a8d4-ecbe34729bb3" 'write writenoresponse
    Public DATA_UART_TX As String = "49535343-1e4d-4bd9-ba61-23c647249616" 'write writenoresponse Notify Indicate, THIS AS 'SET.NOTIFY'
    
End Sub

Public Sub WriteData()
    Dim d() As Byte
    d(0)=0x47    'G
    d(1)=0x0d    'cr
    
    manager.WriteData(DATA_SERVICE,DATA_UART_TX,d)
End Sub

Sub Service_Create
    manager.Initialize("manager")
End Sub

The WriteData is called from main when a send button is clicked, which I press after Scan and Connect button and a connection has been made.
 

dxptech

Member
Licensed User
Longtime User
I added the code from another example that uses WriteComplete as shown in the code.
B4X:
Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public connected As Boolean = False
    Public ConnectedName As String
    Private ConnectedServices As List
    Public rp As RuntimePermissions
    Public DATA_SERVICE As String = "49535343-fe7d-4ae5-8fa9-9fafd205e455" 'data service for RN-BLE RN4871, write to pc
    Public DATA_UART_RX As String = "49535343-8841-43f4-a8d4-ecbe34729bb3" 'write writenoresponse
    Public DATA_UART_TX As String = "49535343-1e4d-4bd9-ba61-23c647249616" 'write writenoresponse Notify Indicate, THIS AS 'SET.NOTIFY'
    Private messagesToSend As List
End Sub

Public Sub SendMessage(msg() As Byte)
    messagesToSend.Add(TrimMessage(msg))
    'messagesToSend.Add(msg)
    If messagesToSend.Size = 1 Then
        Do While messagesToSend.Size > 0
            Try
                manager.WriteData(DATA_SERVICE, DATA_UART_TX, messagesToSend.Get(0))
            Catch
                FailedToSend
                Return
            End Try
            Wait For Manager_WriteComplete (Characteristic As String, Status As Int)
            If Status <> 0 Then
                FailedToSend
            End If
            If connected = False Or messagesToSend.Size = 0 Then Return
            messagesToSend.RemoveAt(0)
        Loop
    End If
End Sub


Sub FailedToSend
    Log("Failed to send message. Disconnecting.")
    manager.Disconnect
End Sub

Sub TrimMessage(msg() As Byte) As Byte()
    If msg.Length > 20 Then
        Dim bc As ByteConverter
        Dim NewMsg(20) As Byte
        bc.ArrayCopy(msg, 0, NewMsg, 0, 20)
        Return NewMsg
    End If
    Return msg
End Sub
I call SendMessage(msg) from Main when the Send Button is clicked.
I place d(0)=0x47, d(1)=0x0d and pass it to Starter with CallSub2(Starter, "SendMessage",d)
Everything looks fine,
The Status is 0.
It seems to be working without Failed to Send being called but, I still don't receive the "G" cr on the RN4871/Micro.
I am using the service UUID for Transparent Uart mode of the RN4871
 
Upvote 0
Top