Android Bluetooth Serial/RFCOMM/SPP, How to Change the BAUD RATE?

ZiGi picture ZiGi · Apr 7, 2011 · Viewed 27.5k times · Source

This is very frustrating as I've been trying for weeks and not even a clue on the internet.

I am working on a project that reads/writes bluetooth serial data on different farming hardware.

most of these hardware are old and changing the baud rate of their SPP is not an option.

I'm using the android bluetooth chat as a basis to read/write, I've only changed the UUID to allow connection to the devices I have

I've managed to read from a tag reader at a bad rate of 19,200 beautifully, and formatted nicely

However, I'm stuck now with the scales which has a baud rate of 9,600 and shows funny characters in my app, not even close to the format that should come from the scales

This project was done previously on a old Windows Mobile and baud rates could be changed in their API

Is there a way to do this in Android or am I barking up the wrong tree?

Answer

Timmmm picture Timmmm · May 27, 2014

Edit: Fixed some errors.

There's no good information on the internet about this - and lots of it is wrong - don't trust anyone!

In particular, these two things are not true:

  • RFCOMM and SPP (Serial Port Profile) are two names for the same thing. (They are however very closely related.)
  • There's no need to set the baud rate of an RFCOMM / SPP connection.

Here is what I have found:

Bluetooth serial connections are made as described in the SPP using RFCOMM. RFCOMM is a connection that runs over L2CAP and allows sending serial data and control parameters in frames. It is heavily based on an ancient standard called TS 07.10. SPP is the bluetooth profile.

This framing also allows you to send out-of-band data for things like controlling serial signals (DTR, CTS, etc) and for setting the baud rate.

From the SPP specification:

DevA may inform DevB of RS232 port settings with the Remote Port Negotiation Command, directly before DLC establishment. There is a requirement to do so if the API to the RFCOMM adaptation layer exposes those settings (e.g. baud rate, parity).

Sadly Android doesn't expose this functionality at all. I suppose it may be possible to send the Remote Port Negotiation command ourselves but I'm pretty sure that would require reimplementing RFCOMM which requires access to L2CAP that we don't have.

The Remote Port Negotiation command is detailed here page 31. Note "There are default values assigned on all parameters, if no negotiation is performed, the default value is chosen."

Therefore it is impossible to set the baud rate as desired on Android. It will use the default baud rate that your bluetooth adapter uses (probably 9600).

Also note that Windows does support setting the baud rate of bluetooth adapters.