Python Bluetooth how to send a file to a phone
Asked Answered
K

3

5

in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so

nearby_devices = bluetooth.discover_devices(
    duration=8, lookup_names=True, flush_cache=True, lookup_class=False)

and then later trying to send the files like so

port = 1
for addr, name in nearby_devices:
    bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))

sock.send("download-app")
sock.close()

Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)

Kho answered 16/2, 2017 at 3:27 Comment(0)
O
8

You're most of the way there...

As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).

In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!

The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.

from bluetooth import *
from PyOBEX.client import Client
import sys

addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)

service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
    print("Couldn't find the service.")
    sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()

Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.

Obe answered 24/2, 2017 at 14:6 Comment(2)
bro... thank you. Like seriously thank you. this is one of those "really need for my project" type deals; you will probably help not just me but many others in this exact situation.Kho
I had made a fork of PyOBEX called nOBEX with a bunch of improvements, new features, and bug fixes, along with Python 3 support. You can take a look here: github.com/nccgroup/nOBEXQuadrivial
S
2

I haven't personally explored it but check out this blog -

http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html

The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.

Sicilia answered 22/2, 2017 at 1:50 Comment(3)
I mean I've viewed this exact page probably about ten times; problem comes with lightblue being *nix based and the target os for use for this project being windows based. my thoughts so far are that inevitably I will have to write my own library in c to accomplish thisKho
I get it. It seems that the problem is that windows does not support openOBEX which is what this is all based on. But I had an unusual idea that might work with a bit of tinkering. You might be able to customize the pybluez module to send using FTP? I am not sure if you need obexftp, openobex or whatever else on the android device but installing those should be a easy enough. Sorry, I couldn't be more help.Sicilia
Honestly I ended up finding an obex library after days of banging my head against the wall but for some reason it was still blocked by my phone. it looks like it's off to c for meKho
M
1

I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.

Motherwell answered 11/12, 2019 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.