I am trying to establish a HFP (Hands Free Profile) service level connection using the python test code provided in the bluez repository.
In order to even run, the example needed some modifications:
- Fix the deprecated import statement:
import glib
try:
from gi.repository import GObject
except ImportError:
import gobject as GObject
=>
from gi.repository import GLib as glib
- Change the mainloop object:
mainloop = GObject.MainLoop()
=>
mainloop = glib.MainLoop()
- Disable audio since it is not relevant to this test:
audio_supported = False
- Encode commands sent to bytes:
os.write(self.fd, cmd + "\r\n")
=>
os.write(self.fd, f"{cmd}\r\n".encode())
- Decode bytes received from buffer:
buf = buf.strip()
=>
buf = buf.decode('utf8').strip()
- Change BDADDR_ANY to the phone's address:
At this point the example runs, in order to actually attempt a connection I added the following code:
fd = os.open("test.log", os.O_RDWR)
profile.NewConnection(options.path, fd, opts)
At this point I get an error from NewConnection() at the following line: fd = fd.take()
Looking at the code, fd seems to be a simple file descriptor so commenting out this line helps me move on.
The actual problem that I am facing now is that I get no reply from the device after sending the initial AT command (line 78 from the link above). As per the Bluetooth HFP 1.8 spec this is supposed to start the service level connection procedure. I am not even sure that my command is making it to the device.
I also tried another example which is a bit newer. This one is able to open a RFCOMM connection through a socket (BluetoothSocket) but the connection keeps getting reset. This is because the _read_at() function (line 181) does not receive anything from the device and times out.