無法運行 python-bluez RFCOMM 服務器示例腳本 (Cannot run python-bluez RFCOMM server example script)


問題描述

無法運行 python‑bluez RFCOMM 服務器示例腳本 (Cannot run python‑bluez RFCOMM server example script)

我正在嘗試在我的 Android 應用程序和 Raspberry Pi 3 之間建立藍牙通信鏈接。我嘗試在我的 Raspberry Pi 上使用 pybluez。我的問題是,當我嘗試運行 pybluez 附帶的示例代碼(見下文)時,我收到以下錯誤消息:

Traceback (most recent call last):
  File "/usr/share/doc/python‑bluez/examples/simple/rfcomm‑server.py", line 20, in <module>
    profiles = [ SERIAL_PORT_PROFILE ],
  File "/usr/lib/python2.7/dist‑packages/bluetooth/bluez.py", line 176, in advertise_service
    raise BluetoothError (str (e))
BluetoothError: (2, 'No such file or directory')

這是我要運行的腳本:似乎問題出在advertise_service方法中...

# file: rfcomm‑server.py
# auth: Albert Huang <albert@csail.mit.edu>
# desc: simple demonstration of a server application that uses RFCOMM sockets
#
# $Id: rfcomm‑server.py 518 2007‑08‑10 07:20:07Z albert $   


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 = [ OBEX_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連接,因為我無法為Android中的套接字設置端口等。

import bluetooth

server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_sock.bind(("",port))
server_sock.listen(1)

client_sock,address = server_sock.accept()
print("Accepted connection from ",address)

data = client_sock.recv(1024)
print("received [%s]" % data)

client_sock.close()
server_sock.close()

我收到官方示例腳本的錯誤消息可能是什麼原因???我已經按照此處描述的步驟進行操作:https:


參考解法

方法 1:

This question may be old, but maybe this helps somebody: I think you simply don't have everything you need installed. For pybluez to work correctly you need the two packages bluetooth and libbluetooth‑dev.

sudo apt‑get install bluetooth libbluetooth‑dev

(by N. Fruehvvombat)

參考文件

  1. Cannot run python‑bluez RFCOMM server example script (CC BY‑SA 2.5/3.0/4.0)

#Python #sockets #Android #bluetooth #rfcomm






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論