擅长:python、mysql、java
<p>你大部分时间都在那里。。。</p>
<p>如您所知,您需要在蓝牙连接的另一端进行通话。您只需要用一个著名的服务(通常是<a href="https://www.bluetooth.com/specifications/assigned-numbers/service-discovery" rel="nofollow noreferrer">these options</a>)替换您的自定义服务器。</p>
<p>在我的例子中,我的手机支持“OBEX对象推送”服务,所以我只需要连接到该服务并使用合适的客户端来讨论正确的协议。幸运的是,PyOBEX和PyBluez的结合在这里起到了作用!</p>
<p>下面的代码(从PyOBEX和PyBluez示例快速拼凑而成)运行在我的windows10python2.7安装中,并在手机上创建一个简单的文本文件。</p>
<pre><code>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()
</code></pre>
<p>不过,看起来PyOBEX是一个非常小的包,并且与Python 3不兼容,所以如果需要的话,您可能需要进行一些移植。</p>