如何在Debian的连接pybluez RFCOMM的服务器套接字? [英] How to connect pybluez RFCOMM server socket on Debian?

查看:473
本文介绍了如何在Debian的连接pybluez RFCOMM的服务器套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些片断的蟒蛇与pybluez框架:

I have these snippet in python with pybluez framework:

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服务器套接字:

and also I have this other snippet in Android to connect the pybluez rfcomm server socket:

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。
我认为,我用它来连接的方式是错误的,我不知道如何正确连接或宣传自己的服务器套接字

My problem is that Android device could not connect to the pybluez socket. I think that the way I use to connect is wrong, and I don't know how to connect correctly or advertise my server socket

推荐答案

我提供的赏金,却发现自己的解决方案。 :)发布另一种答案,但是这可能也适用于您的问题。在Debian的某些版本(Raspbian等),也许一些其他的发行版。在 server_sock.accept()默认情况下将只是挂,从来没有接受连接 - 甚至从配对设备!我在某些情况下甚至说服套接字未打开的。然而,这种解决方案是非常简单的。

I offered a bounty, but found the solution myself. :) Posted on another answer but this may also apply to your problem. On certain versions of Debian (Raspbian etc) and maybe some others distros. The server_sock.accept() will by default just hang and never accept a connection - even from a paired device! I'm in some cases even convinced the socket isn't open at all. However, a solution to this is really simple.

更新您的 /etc/bluetooth/main.conf 的文件,添加一行或改变现有的,所以它看起来是这样的:

Update your /etc/bluetooth/main.conf file, add a line or change the existing so it looks like this:

DisablePlugins = pnat

然后重新启动蓝牙服务:

Then restart the Bluetooth service:

sudo invoke –rc.d bluetooth restart

现在或许已得到修复。

It now MAY have been fixed.

祝你好运!

参考:<一href=\"http://stackoverflow.com/questions/14618277/rfcomm-without-pairing-using-pybluez-on-debian/14827036#14827036\">RFCOMM不Debian上的PyBluez配对?

这篇关于如何在Debian的连接pybluez RFCOMM的服务器套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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