python安装包怎么用蓝牙传送文件_Python蓝牙如何将文件发送到手机

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)

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值