系統:Ubuntu 14.04
藍牙:綠聯USB2.0藍牙適配器(型號:CM109;芯片:CSR8510)
一、藍牙識別:
1、插入硬件,打開終端,查看是否檢測到設備:
$ lsusbBus 001 Device 003: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
2、查看是否識別為藍牙模塊:
$ hciconfig -ahci0: Type: BR/EDR Bus: USB ...
3、查看藍牙模塊的地址;如果不顯示藍牙模塊及其地址,則需要通過rfkill list命令查看hci0是否blocked,使用rfkill unblock 0(rfkill list顯示的hci0的序號)即可啟用藍牙模塊(hci0)。
$ hcitool devDevices: hci0 00:1A:7D:DA:71:11
4、激活藍牙模塊:
$ sudo hciconfig hci0 up
激活藍牙模塊之后,即可通過手機藍牙正常連接。如果手機搜索不到該藍牙模塊,可能因為Ubuntu下藍牙模塊默認為不可見,需要在Ubuntu上方工具欄中點擊藍牙圖標,設置Visible ON即可(暫時沒有找到Ubuntu下設置藍牙可見性的終端命令)。
5、此次測試設置藍牙模塊為服務端且不需要配對碼:
$ hciconfig hci0 noauth
6、hciconfig和hcitool(BlueZ提供的工具,BlueZ是多數Linux發行版的默認藍牙協議棧)可以實現搜索、連接等功能,此處主要希望通過編程控制藍牙模塊,故對此暫時不做深究。
二、PyBluez安裝:
1、下載並解壓PyBluez-0.22,進入PyBluez-0.22目錄;(https://github.com/karulis/pybluez)
2、安裝PyBluez-0.22:
$ sudo python setup.py install
出現問題:
In file included from bluez/btmodule.c:20:0: bluez/btmode.h:5:33: fatal error: bluetooth/bluetooth.h: No such file or directory #include <bluetooth/bluetooth.h>
解決問題:安裝libbluetooth-dev:
$ sudo apt-get install libbluetooth-dev
三、PyBluez測試(參考PyBluez自帶example實現):
1、查詢設備列表:
import bluetoothnearby_devices = bluetooth.discover_devices(lookup_names=True)for addr, name in nearby_devices: print(" %s - %s" % (addr, name))
2、查詢設備服務:
import bluetoothnearby_devices = bluetooth.discover_devices(lookup_names=True)for addr, name in nearby_devices: print(" %s - %s" % (addr, name)) services = bluetooth.find_service(address=addr) for svc in services: print("Service Name: %s" % svc["name"]) print(" Host: %s" % svc["host"]) print(" Description: %s" % svc["description"]) print(" Provided By: %s" % svc["provider"]) print(" Protocol: %s" % svc["protocol"]) print(" channel/PSM: %s" % svc["port"]) print(" svc classes: %s "% svc["service-classes"]) print(" profiles: %s "% svc["profiles"]) print(" service id: %s "% svc["service-id"]) print("")
3、RFCOMM:
藍牙串口服務端:
import bluetoothif __name__ == "__main__": print("looking for nearby devices...") nearby_devices = bluetooth.discover_devices(lookup_names=True) for addr, name in nearby_devices: print("%s %s" % (addr, name)) server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) server_sock.bind(("", bluetooth.PORT_ANY)) server_sock.listen(1) port = server_sock.getsockname()[1] uuid = "00001101-0000-1000-8000-00805f9b34fb" bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid) print("Waiting for connection on RFCOMM channel %d" % port) client_sock, client_info = server_sock.accept() print(client_info) try: while True: data = client_sock.recv(1024) if len(data) == 0: break print("received [%s]" % data) except IOError: pass client_sock.close() server_sock.close() print("this test is done!")
出現問題:Segmentation Fault(參考rfcomm-server.py給定advertise_service方法6個參數時)
解決問題:首先了解錯誤原因——內存訪問越界,說明PyBluez封裝BlueZ存在着一些bug;然后定位錯誤出現位置:advertise_service;在此之后查看PyBluez源碼——bluetooth文件夾下的bluez.py文件中advertise_service的實現,發現該方法最少只需要前三個參數即可,去除多余參數后,運行成功。
總結問題:這種多余參數出現bug的情況非常典型,在程序實現的初期,通常是針對必要參數進行處理而忽視了多余的/擴展的/輔助的參數,而在一般測試過程中通常也很少能夠檢測到這種參數問題,因此,實際應用時,減少多余參數是繞過bug的一種很實用的方法。
最后,通過手機藍牙串口App連接藍牙串口服務端,成功實現信息傳遞。(^_^)
---------------------
作者:xjEzekiel
來源:CNBLOGS
原文:https://www.cnblogs.com/Ezekiel/p/8547817.html
版權聲明:本文為作者原創文章,轉載請附上博文鏈接!