Android Question Bluetooth AStreams_NewData does not fire

rzv25

Member
Licensed User
Longtime User
Hello,

I am working on an application having also bluetooth support. I have changed a bit the code in the Bluetooth example because I want the app to store the Mac of the last connected bluetooth device and on startup, try to connect automatically to that device.
I see that I can successfully connect to the remembered device (which is a laptop transmitting chunks of data every 1 sec) but the AStreams_NewData event does not fire. If I run the Bluetooth demo, everything works fine.
Can you please tell me if I am missing something ? Here are the relevant parts of my code:

B4X:
Sub Process_Globals
   'Bluetooth connection related variables
  Dim serial1 As Serial
  Dim admin As BluetoothAdmin
  Dim AStream As AsyncStreams
  Type NameAndMac (Name As String, Mac As String)
  Dim connectedDevice As NameAndMac
End Sub

Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
     'Initialize bluetooth
    admin.Initialize("admin")
    serial1.Initialize("serial1")
    connectedDevice.Mac = StateManager.GetSetting2("RemoteMac", "00:00:00:00:00:00")
    serial1.Connect(connectedDevice.Mac)
    ProgressDialogShow ("Connecting to Bluetooth remote device ...")
  End If
End Sub

Sub serial1_Connected (Success As Boolean)
   If Success = False Then
     ToastMessageShow("Error connecting to remote Bluetooth device", True)
   Else
     If AStream.IsInitialized = False Then
       AStream.InitializePrefix(serial1.InputStream, True, serial1.OutputStream, "AStreams")
     End If
     'Save the remote device Mac in registry
     StateManager.SetSetting("RemoteMac", connectedDevice.Mac)
     StateManager.SaveSettings
   End If
   ProgressDialogHide
End Sub

Sub AStreams_NewData (Buffer() As Byte)
   Log("Received " & Buffer.Length  & " bytes")
End Sub

Sub AStream_Error
   ToastMessageShow("Connection is broken. " & LastException.Message, True)
   Log(LastException.Message)
End Sub

Sub AStream_Terminated
   AStream_Error
End Sub

I can see the serial1_Connected() event being called with True parameter. Also, none of the AStream_Error() or AStream_Terminated() events are being called.

Thank you in advance for any idea.
 

mterveen

Member
Licensed User
Longtime User
i would change all your AStream text to AStreams and then make sure all the subs start consistently with "AStreams"
 
Upvote 0

rzv25

Member
Licensed User
Longtime User
Are you sure that you can use the prefix mode here? It will only work if the laptop is sending the data in the "prefix protocol".

Changing from AStream.InitializePrefix to AStream.Initialize seems to have resolved the issue. Thanks Erel for the tip.
Also, thank you mterveen for the observation with regards to the incorrect events name.
 
Upvote 0
Top