2013-07-01 71 views
3

我有這些片斷的蟒蛇與pybluez框架:如何在Debian上連接pybluez RFCOMM服務器套接字?

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" 

,也是我在安卓這個其他片段連接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插座。 我認爲我用來連接的方式是錯誤的,我不知道如何正確連接或通知我的服務器插座

回答

2

我提供了一個賞金,但我自己找到了解決方案。 :)張貼在另一個答案,但這也可能適用於您的問題。在某些版本的Debian(Raspbian等)以及其他一些發行版中。默認情況下,server_sock.accept()只會掛起,從不接受連接 - 即使是來自配對設備!我在某些情況下甚至確信套接字根本沒有打開。但是,解決這個問題非常簡單。

更新您的/etc/bluetooth/main.conf文件,添加一行或改變現有的,所以它看起來是這樣的:

DisablePlugins = pnat 

然後重啓藍牙服務:

sudo invoke –rc.d bluetooth restart 

它現在可能已被修復。

祝你好運!

參考:RFCOMM without pairing using PyBluez on Debian?

+1

您可以添加引用你從這個信息後?給予那些應得的榮譽是很好的。 :) – allprog

+1

完成。添加到底部。 – user1840255

相關問題