RK3568蓝牙程序开发过程

news/2024/7/6 1:37:55 标签: bluetooth, 蓝牙, RK3568, 蓝牙服务

1、搭建蓝牙开发环境

     蓝牙开发可以使用C语言开发或python语言开发,使用的是蓝牙开发库为bluez库。

     本文开发使用python语言开发,安装bluez库,可以使用pip install PyBluez来安装。

      如果安装不上的话,可以使用sudo apt install python3-bluez来安装。

      安装成功后可以通过pip list来查看一下是否安装成功。如下看到PyBluez 0.22表示安装成功了。

firefly@firefly:/usr/lib/bluetooth$ pip list
Package                Version             
---------------------- --------------------
attrs                  23.1.0              
blinker                1.4                 
certifi                2023.7.22           
chardet                3.0.4               
command-not-found      0.3                 
cryptography           2.8                 
dbus-python            1.2.16              
distro                 1.4.0               
entrypoints            0.3                 
exceptiongroup         1.1.2               
h11                    0.14.0              
httplib2               0.14.0              
idna                   2.8                 
keyring                18.0.1              
launchpadlib           1.10.13             
lazr.restfulclient     0.14.2              
lazr.uri               1.0.3               
netifaces              0.10.4              
oauthlib               3.1.0               
onboard                1.4.1               
outcome                1.2.0               
pip                    20.0.2              
PyBluez                0.22                
pycairo                1.16.2              
PyGObject              3.36.0              
PyJWT                  1.7.1               
PyQt5                  5.14.1              
PySocks                1.7.1               
python-apt             2.0.0+ubuntu0.20.4.8
PyYAML                 5.3.1               
requests               2.22.0              
requests-unixsocket    0.2.0               
SecretStorage          2.3.1               
selenium               4.10.0              
setuptools             45.2.0              
simplejson             3.16.0              
sip                    4.19.21             
six                    1.14.0              
sniffio                1.3.0               
sortedcontainers       2.4.0               
ssh-import-id          5.10                
trio                   0.22.2              
trio-websocket         0.10.3              
ubuntu-advantage-tools 27.10               
urllib3                1.21.1              
wadllib                1.3.3               
wheel                  0.34.2              
wsproto                1.2.0       

2、建立蓝牙服务端程序

       蓝牙的程序编写与tcp/ip协议很像,基于socket进行编程的。所以有客户端与服务器的角色。服务器的程序代码如下,代码在服务器端建立一个zsm-server的服务器,对应的uuid为00001101-0000-1000-8000-00805F9B34FC。服务端程序运行在设备A上面。

#! /usr/bin/python3.8
import bluetooth

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-00805F9B34FC"


bluetooth.advertise_service(server_sock, "zsm-service", uuid)

print("Waiting for connection on RFCOMM channel %d" % port)

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

while True:
    data = client_sock.recv(1024)
    print("Received ", data)
    client_sock.send(data)

client_sock.close()
server_sock.close()

       运行时发现程序报错,报错的函数为bluetooth.advertise_service(server_sock, "zsm-service", uuid), 报错的内容为(2, 'No such file or directory')。

      经过分析与百度,查找到的解决办法为蓝牙服务启动时需要增加-E -C的选项。解决办法参考:树莓派-蓝牙

         

  bluetoothd -E -C 是蓝牙守护进程(bluetoothd)运行时的命令行选项。下面是每个选项的功能:

  • -E:此选项启用扩展模式。在扩展模式下,蓝牙守护进程将提供更多的功能和调试选项。这些功能包括支持调试输出、启用 experimental 模式、允许加载外部插件和驱动程序等。

  • -C:此选项指定了蓝牙守护进程的配置文件路径。在默认情况下,配置文件是在/etc/bluetooth/main.conf。使用-C选项可以指定一个不同的配置文件路径,让蓝牙守护进程使用自定义的配置文件。

3、建立蓝牙客户端程序

      客户端的程序相对简单一些,客户端程序如下所示,运行在设备B上面。

#! /usr/bin/python3.8
# -*- coding: utf-8 -*-
import bluetooth
from bluetooth import *
import time


nearby_devices = bluetooth.discover_devices(duration=10, lookup_names=True)
print("Found %d devices" % len(nearby_devices), nearby_devices)

con_addr = ""
con_name = "WL18xx Device"
for addr, name in nearby_devices:
    print("Find bluetooth device  %s - %s" % (addr, name))
    
    if name == con_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("\n")
        con_addr = addr


if con_addr == "":
    print("not find!")
else:    
    # Create the client socket
    client_socket=BluetoothSocket( RFCOMM )

    client_socket.connect((con_addr, 3))
    
    print("connect the device:%s addr:%s"%(con_name, con_addr))

    client_socket.send("Hello World")

    print("Finished")

    while True:
        time.sleep(1)


    client_socket.close()

4、测试蓝牙通信

      先启动设备A的服务器程序,再启动设备B的客户端程序,如果一切正常的话,设备B会收到发送出去的数据,再接收到服务器返回来的数据。


http://www.niftyadmin.cn/n/4932346.html

相关文章

手机两个卡槽的正确使用方法,您用对了吗?

手机上有两个卡槽,该如何搭配才能使话费降到最低?你又是怎么搭配的? 这篇文章小编就来告诉你,如何在不换号的情况下,将自己的话费降到最低。 首先卡槽一我们就用8元保号套餐。 卡槽二,我们就可以办理一张…

Blazor 简单组件(0):简单介绍

文章目录 前言说明环境安装 前言 Blazor 这个技术还是比较新,相关的UI组件还在完善,我这里提供一下我个人的组件开发。 说明 本UI组件是基于BootstrapBlazor(以下简称BB)开发。 BootstrapBlazor 文档 环境安装 C#小轮子:Visual Studio自…

win10 + VS2022 安装opencv C++

最近需要用到C opencv,看了很多帖子都需要自己编译opencv源码。为避免源码编译,可以使用VS来配置opencv C。下面是主要过程: 目录 1. 从官网下载 opencv - Get Started - OpenCV 2. 点击这个exe文件进行安装 3. 配置环境变量 4. VS中的项…

【网络】传输层——UDP | TCP(协议格式确认应答超时重传连接管理)

🐱作者:一只大喵咪1201 🐱专栏:《网络》 🔥格言:你只管努力,剩下的交给时间! 现在是传输层,在应用层中的报文(报头 有效载荷)就不能被叫做报文了,而是叫做数…

MySQL不走索引的情况分析

未建立索引 当数据表没有设计相关索引时,查询会扫描全表。 create table test_temp (test_id int auto_incrementprimary key,field_1 varchar(20) null,field_2 varchar(20) null,field_3 bigint null,create_date date null );expl…

Day 22 C++ STL常用容器——string容器

string容器 概念本质string和char 区别:特点string构造函数构造函数原型 string赋值操作赋值的函数原型示例 string字符串拼接函数原型:示例 string查找和替换函数原型示例 string字符串比较比较方式 字符串比较是按字符的ASCII码进行对比函数原型示例 s…

Linux系统USB转串口芯片 GPIO使用教程

一、简介 WCH的多款USB转单路/多路异步串口芯片,除串口接口以外,还提供独立的GPIO接口,各GPIO引脚支持独立的输出输入,GPIO功能的使用需要与计算机端厂商驱动程序和应用软件配合使用。各芯片的默认GPIO引脚状态有所区别&#xff…

SSH远程Ubuntu教程

SSH远程Ubuntu教程 目录 什么是SSH?SSH的优点在Ubuntu上启用SSH服务连接到远程Ubuntu服务器SSH的常用命令 1. 什么是SSH? SSH(Secure Shell)是一种网络协议,用于在不安全的网络中安全地远程登录和执行命令。它使用…