Connecting from PC to phone via RFCOMM, OBEX
Hi,
Could anyone clarify on how to connect to the phone over RFCOMM using python? I'm sure this is meant to be easy but for some reason my python code on the phone does not receive the connection from the PC. My phone code looks like this: (borrowed from the other thread talking about the socket._sock bug)
from socket import *
s = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(s._sock)
s.bind(("", p))
bt_advertise_service( u"asdf", s._sock, True, RFCOMM)
set_security(s._sock, AUTHOR)
print 'Listening...'
s.listen(1)
print 'After Listening'
conn, addr = s.accept()
print "connected to", addr
data = conn.recv(1)
Then I connect to the phone from my PC using PySerial, connecting to COM4. The code on the phone never receives the connection: it gets as far as to print the 'After Listening' message but it never receives a connection to print the "connected to" message. But, and I found this weird: I don't think I'm connecting to the wrong COM port, because the phone still comes up with the "do you want to accept the connection from <me>?" prompt.
So it seems the connection is getting to my phone, but not my python code? This seems confirmed by the fact my code is using AUTHOR so it should authorize incoming connections but when I connect, it still prompts for authentication.
My PySerial code is very simple, just:
import serial
ser = serial.Serial(3)
# after the above line, the phone pops up the authentication message
ser.write("hello")
# now the code will just hang, I guess it has nothing at the other end to write to?
Thanks very much if anyone can help, it would be greatly appreciated.
(On another note, can OBEX files be sent from the PC to the phone using python? Can a file object be somehow wrapped into an OBEX object?)
Re: Connecting from PC to phone via RFCOMM, OBEX
Did you manage to make the phone announce the serial service??
How can you pass throught it? You didn't care to the anounce and simple inserted the address?
Thanks,
Filipe aguiar - portugal
Re: Connecting from PC to phone via RFCOMM, OBEX
I'm not sure exactly what you mean, so maybe you could explain your problem in a bit more detail. When you say "announce", do you mean the bit about advertising the service? If you need to advertise a service on the phone you just have to call bt_advertise_service() (as in the code example further up on this page).
My last post isn't that clear, so I'll try to explain it a bit more here.
The PC can't connect to the phone because the code simply opens up a Bluetooth Serial Port, connects to the phone and then starts sending data. The key thing is that it just "connects" -- it doesn't specify exactly which port to connect to on the phone. So the phone is probably just letting it connect to its generic Serial Port service, or something, which is why you still get the "Do you want to accept the connection from ...?" message. Your code is successfully connecting to a service on the phone, but it's not connecting to the service that you actually want.
I'm not sure if you can specify the service to connect to the from the PC if you're just using pyserial and not an actual Bluetooth library. A Bluetooth library that supports RFCOMM sockets will allow you to connect to your service on the phone, because you'll be able to actually specify the channel/service that you want to connect to on the phone.