7

树莓派开发笔记(十一):蓝牙的使用,BlueZ协议(双树莓探测rssi并通过蓝牙互传获取的r...

 3 years ago
source link: https://segmentfault.com/a/1190000038445524
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

若该文为原创文章,转载请注明原文出处

本文章博客地址: https://blog.csdn.net/qq21497936/article/details/110940484

长期持续带来更多项目与技术分享,咨询请加QQ:21497936、微信:yangsir198808 红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

树莓派开发专栏

上一篇:《 树莓派开发笔记(十):Qt读取ADC模拟量电压(ADS1115读取电压模拟量)

下一篇:敬请期待...

前话

接下来介绍树莓派蓝牙模块的开发,使用的协议为bluez。

ssh远程登录到树莓派

请参照博客《 树莓派开发笔记(一):入手树莓派3b,成功运行树莓派系统

Demo:蓝牙探测信号rssi强度,并发送给服务器

2ayiquE.png!mobile

客户端bob,服务器alice,探测两方的rssi,并传送给服务器alice

2QvUZf7.gif!mobile

amyEJra.gif!mobile

f6vINvq.gif!mobile

Bluez

简介

BlueZ是官方Linux Bluetooth栈,由主机控制接口(Host Control Interface,HCI)层、Bluetooth协议核心、逻辑链路控制和适配协议(Logical Link Control and Adaptation Protocol,L2CAP)、SCO 音频层、其他 Bluetooth 服务、用户空间后台进程以及配置工具组成。

BlueZ由许多单独的模块组成:

  • 蓝牙内核子系统核心
  • L2CAP和SCO音频内核层
  • RFCOMM,BNEP,CMTP和HIDP内核实现
  • HCI UART,USB,PCMCIA和虚拟设备驱动程序
  • 通用蓝牙和SDP库和守护程序
  • 配置和测试实用程序
  • 协议解码和分析工具

搭建Bluez

步骤一:安装bluez

sudo apt-get install bluez

蓝牙明命令行hciconfig/hcitool的使用

检查蓝牙设备是否加载成功

hciconfig

6bUVjai.png!mobile

打开蓝牙

sudo hciconfig hci0 up

ZJFbIvy.png!mobile

扫描蓝牙

sudo hciconfig iscan

3EzmQn2.png!mobile

蓝牙命令行工具bluetoothctl

(注意:不好用,显示的都是mac地址,而且中文乱码,周围蓝牙多,根本分不清楚)

启动蓝牙程序

bluetoothctl

73YbUv3.png!mobile

启动/关闭蓝牙电源

power on/off

7vUF3yr.png!mobile

获取要配对设备的MAC地址

imY3aqa.png!mobile

电脑上的蓝牙,先打开:

7ZNR7vI.png!mobile

pybluez使用

sudo python3 -m pip install pybluez

关键源码

server.py

# -*-coding: utf-8 -*-
from bluetooth import *
import sys
import time
import os
import struct
import bluetooth._bluetooth as bluez
import bluetooth

global hostRssi

os.system("bluetoothctl power on")

# 获取服务,通过uuid查找目标服务
#uuid = "63078d70-feb9-lle7-9812-dca90488bd22"
#os.system("bluetoothctl discoverable on")
dstuuid   = "11111111-1111-1111-1111-111111111111"
localuuid = "22222222-2222-2222-2222-222222222222"

print("本地服务器,搜索客户端蓝牙rssi")
...
data = client.recv(1024)
print (data)
client.close()

bluetooth_sock.close()

client.py

from bluetooth import *
import sys
import time
import os
import struct
import bluetooth._bluetooth as bluez
import bluetooth

global hostRssi

#开启蓝牙可见
os.system("bluetoothctl power on")
os.system("bluetoothctl discoverable on")
dstuuid   = "22222222-2222-2222-2222-222222222222"
localuuid = "11111111-1111-1111-1111-111111111111"

bluetooth_sock=BluetoothSocket(RFCOMM)
bluetooth_sock.bind(("",PORT_ANY))
bluetooth_sock.listen(1)
...
 data = "server:" + str(hostRssi) + ", client:" + str(clientRssi)
...

入坑

入坑一:打开蓝颜失败

zyUNNnA.png!mobile

sudo vim /lib/systemd/system/bluetooth.service

修改文件内容

#ExecStart=/usr/lib/bluez5/bluetooth/bluetoothd
ExecStart=/usr/lib/bluez5/bluetooth/bluetoothd -E -C

&emso;&emso;然后重启服务

sudo sdptool add SP
sudo systemctl daemon-reload
sudo systemctl restart bluetooth
sudo sdptool browse local

入坑二:“no advertisable device”

nQb6J3q.png!mobile

原因:由于蓝牙不可见导致

上一篇:《 树莓派开发笔记(十):Qt读取ADC模拟量电压(ADS1115读取电压模拟量)

下一篇:敬请期待...

若该文为原创文章,转载请注明原文出处

本文章博客地址: https://blog.csdn.net/qq21497936/article/details/110940484


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK