urgent help! bluetooth

Beja

Expert
Licensed User
Longtime User
Hello friends,
Please help how can I use Erel's serial example to send byte array through bluetooth. (max 16 bytes).
bluetooth device id is: 00:12:11:21:08:18 and I want to address it immediately (no search for devices)..
Thanks for your help in advance.
 

Beja

Expert
Licensed User
Longtime User
Thanks KitCarlson,
The link is the chat example... It's great example and covers the serial object in b4a.
Ah, anyway.. will try something...
I am looking for a simple and very short example to send binary data to a device's mac address, via bt.
I am new to bt and b4a and need a headstart by providing a working complete example, but short and focusing on a single job. the I can add to it and expand.
alright, will try something and post it here of there is any need.. thanks KitCarlson again
 
Last edited:
Upvote 0

Vader

Well-Known Member
Licensed User
Longtime User
I am working on a serial app right now. Sending is no problem - it's receiving that is a pain.

Here's how I send:

B4X:
Sub Class_Globals
   Public Port As Serial
   Public PairedMACAddress As String = ""
   Public Stream As AsyncStreams
   Private Queue As List

B4X:
Public Sub Initialize
   InitialiseSerialPort
   GetPairedMACAddress
   
   If Port.IsEnabled Then
      Log("Bluetooth enabled")
      
      If PairedMACAddress = "" Then
         ListPairedDevices
      End If
      
      Try
         If ConnectToPairedDevice Then
            ' This is where things start.
         End If
      Catch
         Log("***** ERROR connecting to device *****")
      End Try
      
   Else
      Log("Bluetooth not enabled")
      ToastMessageShow("Enable Bluetooth and restart application.  Closing application.", True)
      
   End If
End Sub

Private Sub InitialiseSerialPort As Boolean
   ' Initialise the Serial port
   Try
      Port.Initialize("Port")
      Queue.Initialize
      Log("Serial port Initialised")
      Return True
   Catch
      Log("Error initialising Serial port")
      Return False
   End Try
End Sub

Private Sub GetPairedMACAddress
   PairedMACAddress = StateManager.GetSetting("PairedMACAddress")
   Log("Retrieved the following pairing: " & PairedMACAddress)
End Sub

Private Sub Stream_NewData(Buffer() As Byte)
   Private NewData As String = BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1")
   Private i As Int = 0
   
   Log("Received string:" & NewData)
   
   For i = 0 To NewData.Length-1
      Queue.Add(NewData.SubString2(i,i))
   Next   
End Sub

Private Sub Stream_Error
   Log("Stream error")
End Sub

Private Sub Stream_Terminated
   Log("Stream terminated")
   'Stream_Error
End Sub

Private Sub Port_Connected(Success As Boolean)
   If Success = True Then
      If Stream.IsInitialized = False Then
         If InitialiseStream Then
            ' Do the next thing here
         End If
      End If      
   End If
End Sub

Private Sub InitialiseStream As Boolean
   Try
      Stream.Initialize(Port.InputStream, Port.OutputStream, "Stream")
      Log("Stream initialised")
      Return True
   Catch
      Log("** Error initialising stream")
      Return False
   End Try
   
End Sub

Private Sub WriteToSerialPort(Data As Int) 
   Dim StringToSend As String = Chr(Data)
   Dim DataToSend() As Byte = StringToSend.GetBytes("ISO8859_1") 
   
   Stream.Write(DataToSend)
   
   Log("-----------Sent->" & Data )
   
End Sub

Private Sub ListPairedDevices
   ' Now to determine what MAC address to connect to
   Private PairedDevices As Map = Port.GetPairedDevices 
   Private OnscreenList As List 
   Private UserResponse As Int 
   
   Log("Listing paired devices")
   
   OnscreenList.Initialize 
   
   For I = 0 To PairedDevices.Size - 1 
       OnscreenList.Add(PairedDevices.GetKeyAt(I)) 
   Next
   
   UserResponse = InputList(OnscreenList, "Choose device", -1) 'show list with paired devices 
   
   If UserResponse <> DialogResponse.CANCEL Then 
      PairedMACAddress = PairedDevices.Get(OnscreenList.Get(UserResponse)) ' Convert the name to mac address 
      Log("Paired MAC Address (" & PairedMACAddress & ") being saved...")
      StateManager.SetSetting("PairedMACAddress",PairedMACAddress)
      StateManager.SaveSettings       
   End If 
End Sub

Private Sub ConnectToPairedDevice As Boolean
   If PairedMACAddress <> "" Then
      Try
         Port.Connect(PairedMACAddress)
         BluetoothConnected = True
         ToastMessageShow("Bluetooth connected.", False)
         Log("Bluetooth connected")
         If InitialiseSerialPort Then
            ' The remainder of the connection occurs after the Serial Port connection event fires.  See Port_Connected()
            Return True
         Else
            Return False
         End If
      Catch
         BluetoothConnected = False
         ToastMessageShow("Bluetooth connection failure.", False)
         Log("Bluetooth connection failure")
         Return False
      End Try
   End If
End Sub

I hope that helps. I will be asking someone with more serial expertise than me to help determine why I now can't receive data, whereas yesterday I could :BangHead:
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Thanks Vader for sharing this, but I think I am talking from another room.
may be I will need to unlearn something first because MSComm spoiled us
for decades.
 
Upvote 0
Top