Connect to local bluetooth - python

I am using pybluez to develop a bluetooth application on linux in python. I want to know if it is possible to connect to a "localhost" for bluetooth so I can run the client and server on the same machine (as most people do for web development).
If this is not possible, how to most people develop bluetooth applications? Do they just run the client and server on different devices or is there a more clever way to handle this?
Eventually the server will run on a raspberry pi and the client will be any bluetooth enabled device (cell phone, laptop, etc.) but during development it would be great if I could run both on the same machine.
Here is my server:
import bluetooth as bt
socket = bt.BluetoothSocket(bt.RFCOMM)
host = ""
socket.bind((host, bt.PORT_ANY))
port = socket.getsockname()[1]
print("port: " + str(port))
socket.listen(1)
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
# bt.advertise_service(socket, "BTServer", uuid)
print("Listening on " + host + ":" + str(port))
client_sock, addr = socket.accept()
print("Connection accepted from " + addr)
data = client_sock.recv(1024)
print(data)
client_sock.close()
socket.close()
And when I call services = bt.find_service(name=None, uuid=None, address="localhost") on the client it cannot find any services.

Through further research I have found that it is not possible to run a bluetooth client and server on the same device with the same bluetooth adapter. For local testing you can either use two bluetooth enabled computers or get a bluetooth dongle.

It is not possible to run a Bluetooth client and server on the same device.
I used the pybluez python module.
When I run bluetooth.discover_devices(lookup_names=True) in the client code on my machine, it returns all other Bluetooth devices around it except my machine.
As we can not discover the machine, we can not connect to it over Bluetooth and can not use it as a Bluetooth Server.

Related

Can't connect to local Machine IP through TCP From Arduino Uno using SIM900 Shield

So you have a basic understanding of the parts im using, I have:
Arduino Uno
Seeed Studio GPRS Shield v2.0 (http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0)
Ultimate GPS for Adafruit V3.3 (https://www.adafruit.com/products/746?gclid=Cj0KEQjw3-W5BRCymr_7r7SFt8cBEiQAsLtM8qn4SCfVWIvAwW-x9Mu-FLeB6hLmVd0PAPVU8IAXXPgaAtaC8P8HAQ)
Here is my problem:
I have tested the Arduino stacked with the GPRS shield, and it works fine with regards to accessing the internet through TCP, sending SMS, etc.. However, my application requires me to send GPS data from the adafruit GPS to a web server that I have already coded with Django and postgresql. The backend is set up.
I need to send the data from the Uno (client) to my laptop (server), which I coded in python (This is just to check whether it is creating a connection):
#!/usr/bin/env python
import socket
# import postgres database functions
TCP_IP = '192.168.1.112'
TCP_PORT = 10000
BUFFER_SIZE = 40
server_address = (TCP_IP,TCP_PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created.'
# Bind socket to TCP server and port
try:
s.bind(server_address)
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket Bind Complete.'
# Start Listening on socket
s.listen(1) # Puts socket into server mode
print 'Listening on port: ', TCP_PORT
# Now Keep Talking with the client
while (1):
# Wait to accept a connection
conn, addr = s.accept() # Wait for incoming connection with accept()
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
if not data: break
print "recieved data: data", data
conn.send(data) #echo
conn.close()
I dont think there is a problem with this. From this I will post data to my postgreSQL database. However, When I try to use AT commands on the SIM900 module to connect to the server using port 10000, I cannot connect:
AT+CIPSHUT
SHUT OK
AT+CGATT?
+CGATT: 1
OK
AT+CIPMUX=0
OK
AT+CSTT="fast.t-mobile.com","",""
OK
AT+CIICR
OK
AT+CIFSR
6.60.94.49
AT+CIPSTART="TCP","192.168.1.112,"10000"
OK
STATE: TCP CLOSED
CONNECT FAIL
I have tried connecting through TCP and replaced the AT+CIPSTART line with the below statement and it worked, so I know TCP works:
AT+CIPSTART="TCP","www.vishnusharma.com", "80"
Is the IP i'm using wrong? I'm new to this, but if it makes a difference, im using Ubuntu 16.04 partitioned on my Mac OSX. I have also checked the APN for T-mobile and it seems fine.
Any help would be greatly appreciated. Thank You!
The IP you're using is inside a NAT since it starts with 192.168. Unless you have a private apn with the mobile operator you're using, you won't be able to reach your Ubuntu from a public IP. Your ISP gives you a public IP address which ir administrated by your router, so if you want this to work, you'll have to do a port forwarding from your router to your Ubuntu.
To do the port forwarding you have to get in the router's configuration page (Typically 192.168.1.1 but depends on the model) an there you'll have to redirect the port XXX to 192.168.1.112:10000. After that you have to obtain your public IP (curl ifconfig.co) and use it to access from the SIM900.
First of all as a suggestion, you can combine the two shields by using SIM908 (unless you are getting more precision on your GPS shield). Since your TCP connection is working, I bet that the port 10000 on your ubuntu is blocked by the firewall. You can first try to turn off your firewall and see if it works. If it did not worked its something else. If it worked, turn on your firewall and then unblock the tcp port using the following command:
sudo ufw allow 10000/tcp

Pairing bluetooth devices with Passkey/Password in python - RFCOMM (Linux)

I am working on a Python script to search for bluetooth devices and connect them using RFCOMM. This devices has Passkey/Password. I am using PyBlueZ and, as far as I know, this library cannot handle Passkey/Password connections (Python PyBluez connecting to passkey protected device).
I am able to discover the devices and retrieve their names and addresses:
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
But if tried to connect to a specific device using:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
I get an error 'Device or resource busy (16)'.
I tried some bash commands using the hcitool and bluetooth-agent, but I need to do the connection programmatically. I was able to connect to my device using the steps described here: How to pair a bluetooth device from command line on Linux.
I want to ask if someone has connected to a bluetooth device with Passkey/Password using Python. I am thinking about to use the bash commands in Python using subprocess.call(), but I am not sure if it is a good idea.
Thanks for any help.
Finally I am able to connect to a device using PyBlueZ. I hope this answer will help others in the future. I tried the following:
First, import the modules and discover the devices.
import bluetooth, subprocess
nearby_devices = bluetooth.discover_devices(duration=4,lookup_names=True,
flush_cache=True, lookup_class=False)
When you discover the device you want to connect, you need to know port, the address and passkey. With that information do the next:
name = name # Device name
addr = addr # Device Address
port = 1 # RFCOMM port
passkey = "1111" # passkey of the device you want to connect
# kill any "bluetooth-agent" process that is already running
subprocess.call("kill -9 `pidof bluetooth-agent`",shell=True)
# Start a new "bluetooth-agent" process where XXXX is the passkey
status = subprocess.call("bluetooth-agent " + passkey + " &",shell=True)
# Now, connect in the same way as always with PyBlueZ
try:
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.connect((addr,port))
except bluetooth.btcommon.BluetoothError as err:
# Error handler
pass
Now, you are connected!! You can use your socket for the task you need:
s.recv(1024) # Buffer size
s.send("Hello World!")
Official PyBlueZ documentation is available here
Is there a way to connect two phones via Bluetooth , the script should be running on a Linux host. Any suggestions of using pybluez or any other APIs?
I have seen some examples where a Linux host is used as Client and is connect a phone (which is a server), but here I'm want to use Linux host as just a device to run the script and make two phones connect via Bluetooth.

socket programming using python to connect raspberry pi and windows PC

I want to send and receive data between raspberry pi and windows PC. If I placed both server and client program on raspberry pi then it works fine. but when client is run from windows pc then it shows an error 'no connection could be made because the target machine actively refuse it'. I am simply connecting pc and pi through a LAN cable. and firewall of my PC is disabled.
server.py
import socket
s=socket.socket()
host=socket.gethostname()
port=12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'got connection from',addr
c.send('Thank you for connecting')
c.close()
client.py
import socket
s = socket.socket()
host = '192.168.0.3'# ip of raspberry pi
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
If it works on the loopback interface (both scripts on the Raspberry Pi), this means your code is correct.
Why it doesn't work over the network is not really programming related. A few things to check:
As Akash Nil noted, check if you're using a crossover cable. Does the little LED on your LAN port light up when you connect the cable?
Does ping work?
The RPi doesn't have any firewalling active, either, right?
If you're using Windows, put the server on the RPi or be 100% sure you've whitelisted Python in your firewall (or deactivated it).
Does it work if you use netcat as client or as server?

Mininet socket programming in python

I emulated a network topology using mininet. The topology contains two hosts connected by several switches. On host 1 we run a client application which creates a socket and tries to connect to the server application on host 2, it fails however. If I run the client- and server-script locally on one of the two hosts it connects with no problems.
server.py:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 10021))
s.listen(5)
while 1:
(clientsocket, address) = s.accept()
#DO STH.
clientsocket.close()
client.py:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((args['ip'], 10021))
while 1:
#DO STH.
s.close()
Here the code used to execute the commands to start the server and client application
topology.py:
server = net.getNodeByName('host2')
client = net.getNodeByName('host1')
server.cmd('./server.py & > serveroutput')
client.cmd('./client.py -i %serverIP > clientfile' % server.getIP())
Are you using OVS openflow switches in your topology?
If they are openflow enabled, you need to have a SDN controller like Ryu or POX running too. The controller would create a path between the two hosts.
Right host 1 is trying to connect to host 2. Sends some TCP messages to the switch, but the switch doesn't know what to do with so it needs to ask a SDN controller for help. But there is no controller. So the connection failes.
If it was not openflow enabled switches it would have found its way to host 2.
So check if the switch is using openflow.
If you don't use a controller, you should configure the OVS's flow table and allow you data flow.Can you check the connectiong between two host using ping and iperf ?

Raspberry PI Server/Client Socket in Python

I am trying to set up a Python socket between my Raspberry Pi (running Raspbian) and my Macbook Pro (running Mavericks).
Both devices are connected to the same WiFi network in my appt. I run the server code on my RPi and then the client code on my Macbook (I have also tried the reverse). I think that I am missing a set up step because the code I am using I found on multiple sites, so I assume it works. I also checked that my Macbook has firewall turned off.
Server Code:
from socket import *
host = "127.0.0.1"
print host
port = 7777
s = socket(AF_INET, SOCK_STREAM)
print "Socket Made"
s.bind((host,port))
print "Socket Bound"
s.listen(5)
print "Listening for connections..."
q,addr = s.accept()
data = raw_input("Enter data to be sent: ")
q.send(data)
Client Code:
from socket import *
host = "127.0.0.1"
print host
port=4446
s=socket(AF_INET, SOCK_STREAM)
print "socket made"
s.connect((host,port))
print "socket connected!!!"
msg=s.recv(1024)
print "Message from server : " + msg
I get the error:
Traceback (most recent call last):
File "TCPclient.py", line 9, in <module>
s.connect((host,port))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 61] Connection refused
My process for executing the code is:
type "python TCPserver.py" into RPi terminal
type "python TCPclient.py into Macbook terminal
Then I receive the error message on my Macbook, no error on the RPi
My questions are:
Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
Does the input for host have to be the same for the client and server code?
Should the RPi and Macbook both be connected to the same WiFi network?
Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
Do you know why I am receiving this error and how to fix it?
Your help is greatly appreciated!!
127.0.0.1 is a special IP address for local machine.
You must set the real IP address (on your LAN) of you mac in the client code.
You should also bind on this IP on the server, or on 0.0.0.0 to bind on all available IP addresses.
You must also use the same port number on both client and server.
And to reply to your questions:
Is 127.0.0.1 the proper input for "host"? (please note I also tried "localhost")
127.0.0.1 is the same than localhost, it means the local machine. This will work if you run the client and the server on the same machine, else you need the real IP address of your mac. Try ifconfig in a console.
Does the input for host have to be the same for the client and server code?
Yes and no. On the server you bind to a port and an address, so you'll wait for connections on this port and address. You can use the IP address, or 0.0.0.0.
Should the RPi and Macbook both be connected to the same WiFi network?
Yes and no. It will work with the same WiFi network, but it will also work with two different WiFi networks if they are connected together directly or with a IP router. Most of the time though they are connected to the internet through a NAT (network address translator), and then it will not work.
Is there any set up that needs to be done on either the RPi or my Macbook in order for this to work (Please note my RPi is Model B, new, and I did not set up anything else on it before this)
I don't know much about the RPi but it looks like standard TCP sockets, that should work out of the box.
Do you know why I am receiving this error and how to fix it?
As I stated at the beginning: You try to connect to the RPi (127.0.0.1) on the wrong port.
You have created a listener on port 7777, then you connected on 4446 !!!!!
just connect on the same port you are listening to =)

Categories