Python蓝牙如何将文件发送到手机 [英] Python Bluetooth how to send a file to a phone

查看:343
本文介绍了Python蓝牙如何将文件发送到手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,要求通过蓝牙将Windows计算机上的文件发送到android设备,除了标准状态(当然还有成对的蓝牙连接)以外,电话上没有任何其他内容.我查看了pybluez,它看起来很简单,可以在客户端和服务器体系结构之间发送文件(实际上,它可以在我的笔记本电脑和台式机之间快速发送),但是我一生无法找到任何方法来获取python建立连接后,将文件从计算机发送到android;我的尝试一直是从设备中获取类似东西的蓝牙mac地址

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()

当然,使用pybluez文档给出的示例脚本,我可以在客户端和服务器之间无缝发送文件,但是我仍然无法将文件发送到选定的android设备(即使我指定了它的地址和知道它在范围内)

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)

推荐答案

您已经准备就绪了...

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).

就我而言,我的电话支持"OBEX对象推送"服务,因此我只需要连接到该服务并使用合适的客户端来谈论正确的协议.幸运的是,PyOBEX和PyBluez的结合在这里起到了作用!

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!

以下代码(从PyOBEX和PyBluez示例中快速修补而成)在我的Windows 10,Python 2.7安装上运行,并在手机上创建了一个简单的文本文件.

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()

不过,看起来PyOBEX是一个非常小的软件包,并且与Python 3不兼容,因此如果需要的话,您可能需要做些移植工作.

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.

这篇关于Python蓝牙如何将文件发送到手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆