我在带有 pybluez 框架的 python 中有这些片段:
from bluetooth import *
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "SampleServer",
service_id = uuid
# service_classes = [ uuid, SERIAL_PORT_CLASS ],
# profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ RFCOMM_UUID ]
)
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
except IOError:
pass
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
而且我在Android中有这个其他片段来连接pybluez rfcomm服务器套接字:
private static final UUID MY_UUID = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");
....
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(myServerMacAddress);
....
BluetoothSocket tmp= device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
我的问题是 Android 设备无法连接到 pybluez 套接字。我认为我用来连接的方式是错误的,我不知道如何正确连接或宣传我的服务器套接字