Danilo
Danilo

Reputation: 41

Python Socket Bluetooth

I'm trying to pair my cardio bluetooth sensor to my windows pc. After some searches I found pyBluez for x64 systems and now i'm able to discover bluetooth devices around me, their names,address and services. My Polar sensor has an L2CAP protocol and teorically is too symple to listen what transmits.

I found an example like this

server_sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)

port = 5
server_sock.bind(port)
server_sock.listen(1)

client_sock = server_sock.accept()
print ("Accepted connection from "+address)

data = client_sock.recv(1024)
print ("received [%s]" % data)

Also with different ports
when i run this code, i never read "accepted connection...."

Probably the reason is the lack of L2CAP for pyBluez windows version. I tried also a socket connection but the "socket.AF_BLUETOOTH" method isn't available for windows too. Have you any suggestion?

Thanks a lot and sorry for my englis

Upvotes: 4

Views: 20170

Answers (2)

Harish Nedunuri
Harish Nedunuri

Reputation: 1

@murtaza-haji - the value a4:50:4f:f8:44:66 is the Bluetooth address.

You can use the discover_devices method from pybluez to discover this.

import bluetooth  # from pip install pybluez

nearby_devices = bluetooth.discover_devices(lookup_names=True)
print(f"Found {len(nearby_devices)} devices.")

for addr, name in nearby_devices:
    print(f"  {addr} - {name}")

Upvotes: 0

Nuri can
Nuri can

Reputation: 31

you try socket library.

import socket

baddr = 'a4:50:4f:f8:44:66'
channel = 4
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, 
socket.BTPROTO_RFCOMM)
s.connect((baddr,channel))
s_sock = server_sock.accept()
print ("Accepted connection from "+address)

data = s_sock.recv(1024)
print ("received [%s]" % data)

s.listen(1)

I have tried pybluez but it didn't run. I try on Linux. I hope it works on windows...

Upvotes: 3

Related Questions