Siddharth Gaur
Siddharth Gaur

Reputation: 111

android bluetooth serversocket with python-bluez client

I am trying to connect my laptop(as client) to my android phone(as listener) using python-bluez on the laptop and android-bluetooth API on the phone.

I use the following code for my phone:

BluetoothServerSocket tmp = badapter.listenUsingRfcommWithServiceRecord(
    badapter.getName(), MY_UUID);
BluetoothServerSocket bserversocket = tmp;
if(bserversocket != null)
{
    BluetoothSocket acceptsocket = bserversocket.accept(timeout);   
}
//timeout is set to about 15 sec
if(acceptsocket != null)
{
    out.append("got the connection...\n");   
}

and the following in python for my laptop client:

from bluetooth import *

btooth_addr = "38:EC:E4:57:1F:1B"

sock = BluetoothSocket(RFCOMM)
sock.connect((btooth_addr, 2))

print "Connected"
sock.close()

the listener time-outs without acknowledging any connections from the laptop, while the sender moves on to print 'Connected' on all attempts on different ports.

the problem is that I don't know and can't set the port/channel the android phone is listening on, and also that I am required to fill in a port number as second argument of 'connect'(2 in this snippet).

please help me out - my sole goal at this time is to get the connection attempt acknowledged by the phone.

Upvotes: 2

Views: 2096

Answers (1)

Radu
Radu

Reputation: 2074

Have a look at the pybluez documentation(source code) for establishing client connections.

You can get the correct port for the supplied Bluetooth address and UUID using find_service. Then connect your socket just as you do in your code, replacing hardcoded port value with the correct one.

Don't forget to vote up!

Upvotes: 1

Related Questions