how do i broadcast Bluetooth inquiry with python sockets AF_bluetooth socket? - python-3.x

Well at the moment I try to learn more about Bluetooth, I've realized that to connect I need to send a packet to the device called inquiry packet. However for my script I'm more interested in broadcasting it, and even though pybluez provides the high level functions for it. I want to do it with sockets as part of learning experience. Can anyone please tell me, how to specify I want to send the inquiry packet? through the sockets like so
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
and how do I broadcast it, instead of sending to one adress?

Are you asking to create a Serial Port Profile (SPP) socket server?
Any example of that is below.
I have used the pydbus library to get the Bluetooth adapter address but you could just hardcode the mac address if you preferred:
import socket
import pydbus
bus = pydbus.SystemBus()
adapter = bus.get('org.bluez', '/org/bluez/hci0')
adapter_addr = adapter.Address
port = 3 # Normal port for rfcomm?
buf_size = 1024
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.bind((adapter_addr, port))
s.listen(1)
try:
print('Listening for connection...')
client, address = s.accept()
print(f'Connected to {address}')
while True:
data = client.recv(buf_size)
if data:
print(data)
except Exception as e:
print(f'Something went wrong: {e}')
client.close()
s.close()

Related

can't send data through l2cap raw socket for local controller - [Errno 107] Transport endpoint is not connected

I tried to connect to my local bluetooth controller through l2cap layer in order to see which services I am supporting by using SDP protocol.
**don't want to use sdptool
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_L2CAP) as l2cap_sock:
l2cap_sock.bind((local_bd_addr,0))
l2cap_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
l2cap_sock.send(b'SDP PAYLOAD')
ERROR Result:
[Errno 107] Transport endpoint is not connected
any ideas,
thanks

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 4: invalid start byte

I'm a CS student learning network programming. I'm using python to develop a chat application using the server-client network functions like socket function, bind function, receive function and etc. But I encounter this problem in the server code which I don't understand since I have never learnt python programming language before. Could you help me?
The error occurs at
print(f'Received encrypted message from {user["data"].decode("utf-8")}: {encMsg["data"].decode("utf-8")}')
# Get user by notified socket, so we will know who sent the message
user = clients[notified_socket]
print(f'Received encrypted message from {user["data"].decode("utf-8")}: {encMsg["data"].decode("utf-8")}')
# Iterate over connected clients and broadcast message
for client_socket in clients:
# But don't sent it to sender
if client_socket != notified_socket:

BLED112 set Encryption?

I'm using BLED112 and "pygatt BGAPI backend" Python library to communicate to a custom 3rd party BLE device.
Using the "Bluegiga BLE GUI" I can read/write to the device characteristic but only after clicking the "Encrypt" button next to the device.
What is the python library equivalent to setting / enabling the encryption?
I can't seem to find encryption parameter in either adapter initialization or in the device connection functions
Simplified code:
import pygatt
adapter = pygatt.BGAPIBackend()
adapter.start()
device = adapter.connect("01:23:45:67:89", address_type=pygatt.BLEAddressType.random, timeout=10)
raw_read = device.char_read_handle(32)
The library has a function "bond" which performs the same command as the "Encrypt" button.
import pygatt
adapter = pygatt.BGAPIBackend()
adapter.start()
device = adapter.connect("01:23:45:67:89", address_type=pygatt.BLEAddressType.random, timeout=10)
device.bond()
raw_read = device.char_read_handle(32)

SCTP server has an abnormal behaviour when connecting with a client

I have this code on a small server that is getting requests from a client using SCTP connection, I keep getting this error every now and then.
BlockingIOError: [Errno 11] Resource temporarily unavailable
I know that I can avoid it by using Try-except but I wanna have a deep understanding of the issue. any help?
my code is here. this is the server
server = ('', 29168)
sk = sctpsocket_tcp(socket.AF_INET)
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sk.bindx([server])
sk.listen(5)
connection, addr = sk.accept()
while c:
a,b,c,d = connection.sctp_recv(1024)
print(c)
After going again through the SCTP library, I found a closed issue on Github with the solution

join/leave multicast group using libpcap

I need to receive a multicast stream but filter incoming packets by source MAC address on CentOS 5.5.
I'm planning to use libpcap library.
Is it possible to join/leave multicast group using libpcap?
If yes, how to do that?
Thanks
Sure, just construct and send the appropriate IGMP packets.
1.Create dummy socket: sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2.Bind it: rc = bind(sd, (sockaddr*) &addr, sizeof(sockaddr_in));
3.Join multicast group:
ip_mreq mreq;
mreq.imr_interface.s_addr = htonl(InterfaceIp);
mreq.imr_multiaddr.s_addr = htonl(DestIp);
if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
close(sd);
// Error handle...
}
Don't send or receive packets using dummy socket
4.open pcap using pcap_open_live()
The general idea is use regular socket in order to "tell" kernel to send IGMP join packet, and after use pcap in order to capture packets.

Resources