SPP Bluetooth connection with HW module

theos

Member
Licensed User
Longtime User
Dear Sirs,
I usually make hardware devices that includes the F2M03GLA Bluetooth Wireless Uart, produced by Free2move.(F2M03GLA | Free2Move)
Now I need to connect a B4A application to such device that is configured as
endpoint (slave, accepting all units) to my android phone (v. 2.2.1).
Using a sample application as the serial bluetooth tutorial, if I try to connect to the device, using

Serial1.Connect(PairedDevices.Get(l.Get(res))) or Serial1.Connect('right mac
address')

I get the following error trapped by the sample Serial1_Connected
function:

java.io.IOException: Service Discovery Failed.

Please note that with the same hardware device I can send data (SPP) to a PC (Windows and Linux), to Nokia S60 phones and WM6.5 smartphones.
Android OS show me that the device is correctly paired (but not connected).
Reading some forums I found that sometimes the RFCOMM needs a workaround:
Issue 5427 - android - can't initiate SPP Communication without SDP - Android - An Open Handset Alliance Project - Google Project Hosting

Now I am asking to the community if someone else have found this problem on the way, and if the workaround can be implemented in B4A.
Thanx
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Can you please test the updated Serial library attached to this post?
I've added a new method named Connect3. It accepts the connection port number (1 - 30) and it follows the workaround discussed.

Copy the two files to the internal libraries folder. The version displayed in the libraries tab should be 1.01.
 

Attachments

  • Serial.zip
    7.2 KB · Views: 947

theos

Member
Licensed User
Longtime User
Workaround seems ok

Dear Erel,
I have just done some preliminar tests, but it seems to work.
I can use the connect3 function without any errors, giving the MAC address and the rfcomm port 1. The event Serial1_Connected is triggered with the SUCCESS as true.
Thank you very much for your support: You did a great work.
 

MFX

Member
Licensed User
Longtime User
I'm also having trouble with Bluetooth on my Archos 70 tablet. The Serial tutotrial runs on my HTC desire without errors but wwhen I try and run it on the Archos first I get :-

"Program paused on line: 29
Serial1.Listen 'listen for incoming communications"

Then if I click the "play" icon to continue I get:-

"Error Occured

An error has occured in sub:
main_activity_resume (B4A line: 29)
Serial1.Listen 'listen for incoming conections
java.io.IOException: Not able to register SDP record for B4A
Continue?"

If I select "Yes" the programapeears to run OK but if I try and connect to another Bluetooth device I get :-

"Error connecting.
java.io.IOException: Service discovery failed"

:-(.
 

MFX

Member
Licensed User
Longtime User
Yes, I tried with the original Lib and this one.

Cheers.
Martin.
 

MFX

Member
Licensed User
Longtime User
No ideas? I'm currently developing my app on my HTC Desire but when finished I'd like to move it to the tablet as it has a bigger screen but I won't be able to if I can't get Bluetooth working on it :(

Cheers.
Martin.
 

geocomputing

Member
Licensed User
Longtime User
Bluetooth SPP not working

Hi,

I can't connect to a bluetooth rfcomm serial module on an arduino using the serial chat example on an LG Optimus (it works on my galaxy tab). I tried commenting the listen line, which didn't work. I've also tried using connect3 on port 1, which didn't work either.

Can I ask if there's a reliable bluetooth serial library now that will connect to all spp devices? I can connect to anything with blueterm from the android market, but not using the b4a serial library, so I know it's not an android problem.

The main problem I keep getting is that on some devices the app will connect (I know that from the toast message and the bt modules led) and then the app completely freezes. After a while android pops up a dialog asking if I want to force close. My guess from the code is that the problem occurs when the input and output streams are initialised.

Many thanks,

Andrew.
 
Last edited:

geocomputing

Member
Licensed User
Longtime User
Hi Erel,

I've now tried using different api levels and directly reading the bytes from bluetooth (i.e. No input and output text streams) and neither has helped solve the problem on the lg optimus using android 2.3.5. Howeve those things still work ok on honeycomb 3.1.

I've had a look through the logs and the only useful info I get is that the keydispatch event times out and the app stops responding. I've put my latest logs below in case you can see why the library doesn't work. I've read that android apps can have those issues if bluetooth is connected to in yhe main routine, instead of on a separate thread, but without the library code I can't say if that's a problem.

Thanks,

Andrew.

02-19 20:06:17.851 I/ActivityManager(188): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=anywheresoftware.b4a.samples.serial/.main bnds=[363,92][477,236] } from pid 481
02-19 20:06:17.906 I/ActivityManager(188): process name to start: anywheresoftware.b4a.samples.serial
02-19 20:06:17.906 E/ActivityManager(188): heap conf. property name string trimmed: hs.anywheresoftware.b4a.samples
02-19 20:06:17.929 I/ActivityManager(188): Start proc anywheresoftware.b4a.samples.serial for activity anywheresoftware.b4a.samples.serial/.main: pid=1838 uid=10110 gids={3002}
02-19 20:06:21.445 W/InputManagerService(188): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@406dbe30
02-19 20:06:55.765 I/WindowManager(188): Input event dispatching timed out sending to anywheresoftware.b4a.samples.serial/anywheresoftware.b4a.samples.serial.main
02-19 20:06:56.703 E/ActivityManager(188): ANR in anywheresoftware.b4a.samples.serial (anywheresoftware.b4a.samples.serial/.main)
02-19 20:06:56.703 E/ActivityManager(188): Reason: keyDispatchingTimedOut
02-19 20:06:56.703 E/ActivityManager(188): Load: 2.45 / 1.47 / 1.15
02-19 20:06:56.703 E/ActivityManager(188): CPU usage from 7045ms to 1013ms ago with 99% awake:
02-19 20:06:56.703 E/ActivityManager(188): 17% 188/system_server: 14% user + 2.4% kernel / faults: 140 minor
02-19 20:06:56.703 E/ActivityManager(188): 11% 134/dbus-daemon: 10% user + 1.1% kernel
02-19 20:06:56.703 E/ActivityManager(188): 6.6% 1779/btipsd: 5.3% user + 1.3% kernel
02-19 20:06:56.703 E/ActivityManager(188): 1.8% 1838/anywheresoftware.b4a.samples.serial: 1.4% user + 0.3% kernel / faults: 156 minor
02-19 20:06:56.703 E/ActivityManager(188): 0.6% 63/synaptics_wq: 0% user + 0.6% kernel
02-19 20:06:56.703 E/ActivityManager(188): 0.6% 280/com.android.phone: 0.6% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 0.6% 283/com.android.bluetooth: 0.4% user + 0.1% kernel
02-19 20:06:56.703 E/ActivityManager(188): 0.3% 73/khotplug/0: 0% user + 0.3% kernel
02-19 20:06:56.703 E/ActivityManager(188): 0% 7/events/0: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 0.1% 993/logcat: 0.1% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 37% TOTAL: 31% user + 5.8% kernel + 0.1% iowait
02-19 20:06:56.703 E/ActivityManager(188): CPU usage from 374ms to 911ms later:
02-19 20:06:56.703 E/ActivityManager(188): 14% 188/system_server: 9.2% user + 5.5% kernel
02-19 20:06:56.703 E/ActivityManager(188): 7.4% 246/Thread-30: 7.4% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 1.8% 240/InputDispatcher: 0% user + 1.8% kernel
02-19 20:06:56.703 E/ActivityManager(188): 8% 134/dbus-daemon: 6.6% user + 1.3% kernel
02-19 20:06:56.703 E/ActivityManager(188): 7.4% 1779/btipsd: 7.4% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 3.7% 1779/btipsd: 3.7% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 3.7% 1780/btipsd: 3.7% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 1.4% 1838/anywheresoftware.b4a.samples.serial: 0% user + 1.4% kernel / faults: 31 minor
02-19 20:06:56.703 E/ActivityManager(188): 1.4% 1838/.samples.serial: 0% user + 1.4% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1868/migration/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1869/ksoftirqd/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1870/ipc_rec_queue/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1871/hsi_buf_retry_w: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1872/hsi-write-wq/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1873/hsi-read-wq/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1874/khotplug/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1875/kondemand/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1876/cosmo_charger_w: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1877/pvr_timer/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1878/crypto/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1879/aio/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1880/rpciod/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1881/kblockd/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): +0% 1882/events/1: 0% user + 0% kernel
02-19 20:06:56.703 E/ActivityManager(188): 17% TOTAL: 14% user + 2.9% kernel
02-19 20:06:58.406 W/ActivityManager(188): Force finishing activity anywheresoftware.b4a.samples.serial/.main
02-19 20:06:58.414 I/ActivityManager(188): Killing anywheresoftware.b4a.samples.serial (pid=1838): user's request
02-19 20:06:58.453 I/WindowManager(188): WIN DEATH: Window{40a47358 anywheresoftware.b4a.samples.serial/anywheresoftware.b4a.samples.serial.main paused=true}
02-19 20:06:58.453 I/ActivityManager(188): Process anywheresoftware.b4a.samples.serial (pid 1838) has died.
 

geocomputing

Member
Licensed User
Longtime User
Hi Erel,

I've put the code below. Basically it's pretty much the standard serial example. I hope it helps :)

Best wishes,

Andrew.
B4X:
Sub Process_Globals
   Dim Serial1 As Serial
   Dim TextReader1 As TextReader
   Dim TextWriter1 As TextWriter
   Dim Timer1 As Timer
   Dim connected As Boolean
End Sub

Sub Globals
   Dim btnSend As Button
   Dim txtLog As EditText
   Dim txtSend As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      Serial1.Initialize("Serial1")
      Timer1.Initialize("Timer1", 200)
   End If
   Activity.LoadLayout("1")
   Activity.AddMenuItem("Connect", "mnuConnect")
   Activity.AddMenuItem("Disconnect", "mnuDisconnect")
   txtLog.Left=0
   txtLog.Width=Activity.Width
   txtLog.Top=0
   txtLog.Height=Activity.Height-txtSend.Height
   txtSend.top=txtLog.Top+txtLog.Height
   txtSend.Left=0
   txtSend.Width=Activity.Width-btnSend.width
   btnSend.Top=txtSend.Top
   btnSend.Left=txtSend.Left+txtSend.Width
End Sub

Sub Activity_Resume
   If Serial1.IsEnabled = False Then
      Msgbox("Please enable Bluetooth.", "")
   Else
      Serial1.Listen 'listen for incoming connections
   End If
End Sub

Sub mnuConnect_Click
   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))
   Next
   Dim res As Int
   res = InputList(l, "Choose device", -1) 'show list with paired devices
   If res <> DialogResponse.CANCEL Then
      Serial1.Connect(PairedDevices.Get(l.Get(res))) 'convert the name to mac address
      'Serial1.Connect3(PairedDevices.Get(l.Get(res)),1)
   End If
End Sub

Sub Serial1_Connected (Success As Boolean)
   If Success Then
      ToastMessageShow("Connected successfully", False)
      TextReader1.Initialize(Serial1.InputStream)
      TextWriter1.Initialize(Serial1.OutputStream)
      timer1.Enabled = True
      connected = True
   Else
      connected = False
      Timer1.Enabled = False
      Msgbox(LastException.Message, "Error connecting.")
   End If
End Sub

Sub mnuDisconnect_Click
   Serial1.Disconnect
   connected = False
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnSend_Click
   If connected Then
      TextWriter1.WriteLine(txtSend.Text)
      TextWriter1.Flush
      txtSend.Text = ""
   End If
End Sub

Sub Timer1_Tick
   If connected Then
      If TextReader1.Ready Then 'check if there is any data waiting to be read
         txtLog.Text = txtLog.Text & TextReader1.ReadLine & CRLF
         txtLog.SelectionStart = txtLog.Text.Length
      End If
   End If
End Sub
 

geocomputing

Member
Licensed User
Longtime User
Hi Erel,

Thanks. I tried commenting out the line and it doesn't freeze, but I've no idea why that should only be an issue on some devices. I'll have a play and post a serial chat version that works on all my devices soon if I get it working.

Best wishes,

Andrew.
 
Top