Ubuntu蓝牙识别及PyBluez实现蓝牙串口测试

摘要:
如果手机无法搜索蓝牙模块,可能是因为Ubuntu下的蓝牙模块默认不可见。您需要单击Ubuntu上方工具栏中的蓝牙图标并设置VisibleON。2、 PyBluez安装:1。下载并解压缩PyBluez-0.22,然后进入PyBluez-0.22目录;2.安装PyBluez-0.22:$sudopythonsetup.pinstall:Infileincludedfromluez/btmodule时出现问题。c: 20:0:bluez/bt模式。h: 5:33:致命错误:蓝牙/蓝牙。h: Nosuchfileordirectory#包含 解决方案:安装libbluetooth-dev:$sudoapt getinstalllibbluetograph-dev3。PyBluez测试:1。查询设备列表:importbluetoothearby_devices=bluetooth.discover _ devicesforaddr,nameinnearby_ devices:打印2。查询设备服务:importbluetoothenarby_devices=bluetooth.discover _ devicesfaddr,nameinnearby_ devices:printservices=bluetooth.find_ Serviceforvcinservices:printprintprintprintPrintprintprintprintprint printprintprintPrint(“”)3。RFCOMM:蓝牙串行端口服务器:importbluetoothif__name__==“__main__”:printnearby_devices=Bluetooth.discover_ devicesforaddr,nameinnearby_ devices:printserver_sock=Bluetooth.BluetoothSocketserver_sock.bindserver_sock.listenport=server_sock.getsockname()[1]uuid=“00001101-0000-1000-8000-00805f9b34fb”Bluetooth.advertise _ serviceprintclient_sock,client_info=server_sock.accept()printtry:whileTrue:data=client_sock.recvflen==0:breakprintexceptIOError:passclient_sock.close()server_sock.cclose()print(“thiestisdone!

系统:Ubuntu 14.04

蓝牙:绿联USB2.0蓝牙适配器(型号:CM109;芯片:CSR8510)

一、蓝牙识别:

  1、插入硬件,打开终端,查看是否检测到设备:

$ lsusb
Bus 001 Device 003: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

  2、查看是否识别为蓝牙模块:

$ hciconfig -a
hci0:    Type: BR/EDR    Bus: USB
            ...

  3、查看蓝牙模块的地址;如果不显示蓝牙模块及其地址,则需要通过rfkill list命令查看hci0是否blocked,使用rfkill unblock 0(rfkill list显示的hci0的序号)即可启用蓝牙模块(hci0)。

$ hcitool dev
Devices:
    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 bluetooth

nearby_devices = bluetooth.discover_devices(lookup_names=True)for addr, name in nearby_devices:
    print("  %s - %s" % (addr, name))

  2、查询设备服务:

import bluetooth

nearby_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 bluetooth

if __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连接蓝牙串口服务端,成功实现信息传递。(^_^)

免责声明:文章转载自《Ubuntu蓝牙识别及PyBluez实现蓝牙串口测试》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇cesium 模拟飞行以及波束跟随WebView2简单试用(一)—— 开始下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

安卓手机连接苹果蓝牙耳机声音变小

安卓手机连接苹果蓝牙耳机声音变小 打开安卓手机------点击设置-----关于手机------版本号----连续点击7下,就可以进去开发者模式------如果手机设置有密码,需要打开密码,提示进入开发者模式后退出 然后点击-----设置-----点击 系统和更新 ----- 然后 点击 开发人员协助---进去选择 蓝牙绝对音量 【如果一开始进去是打开那...

android 蓝牙通信编程讲解

以下是开发中的几个关键步骤: 1,首先开启蓝牙 2,搜索可用设备 3,创建蓝牙socket,获取输入输出流 4,读取和写入数据 5,断开连接关闭蓝牙 下面是一个demo 效果图: SearchDeviceActivity.java [java]view plaincopy package com.hello.project;      impo...

Logitech k480 蓝牙键盘连接 ubuntu 系统

  k480 能同时连接三台蓝牙设备,支持 Windows、Android、Chrome、Mac OS X 和 iOS 系统。奈何官方并不支持 Ubuntu。   有压迫就有反抗,呃...,不对,总是有办法在 Ubuntu 系统下连接 k480 的。 Ubuntu 系统下连接 k480 的方法是: 安装蓝牙工具 $sudo apt-get insta...

无线Mesh网络技术基础与应用

无线Mesh网络主要包含三类节点,构成了Mesh的基本服务集。 1、与有线网络相连的节点(GateWay节点),其主要负责实现无线Mesh网络和有线网络的数据交换。 2、可以进行Mesh组网并拥有Routing功能的STA(Station),其同时具备终端STA和路由器的特点,即其自身可以获得Mesh网络所提供的服务,也可以为其他STA进行数据路由转发。...

Android蓝牙开发技术学习总结

Android开发,提供对蓝牙的通讯栈的支持,允许设别和其他的设备进行无线传输数据。应用程序层通过安卓API来调用蓝牙的相关功能,这些API使程序无线连接到蓝牙设备,并拥有P2P或者多端无线连接的特性。 蓝牙的功能: 1、扫描其他蓝牙设备 2、为可配对的蓝牙设备查询蓝牙适配器 3、建立RFCOMM通道(其实就是尼玛的认证) 4、通过服务搜索来链接其他的设备...

蓝牙协议分析(4)_IPv6 Over BLE介绍

1. 前言 蓝牙是个奇葩的家伙:它总是以后来者的身份出现,很喜欢打仗,而且还不落下风(有点像某讯的风格)。90年代末期和Wi-Fi的无线标准之争如此,当前和802.15.4系(ZigBee、RF4CE、Thread等)的IoT之争,也如此。 90年代末期,蓝牙刚出道的时候,就曾叫嚣着把Wi-Fi(802.11)从地球上抹去。反过来,1999年Wi-Fi...