树莓派蓝牙作为从端被手机发现和配对连接通信
rfkill查看锁定设备列表
rfkill unlock [ID]解锁设备
sudo hciconfig hci0 up 启用设备
hciconfig -h
查看设备详细信息:hciconfig -a hci0或者hciconfig
打开/关闭/重置hci设备:hciconfig hci0 up/down/reset
认证打开/关闭:hciconfig hci0 auth/noauth,直接体现在进行蓝牙连接时,是否输入连接PIN密码,用于PIN配对
查看/改变蓝牙主从状态:hciconfig hci0 lm 、hciconfig hci0 lm slave
查看/设置蓝牙名称:hciconfig hci0 name 、hciconfig hci0 name Donge
开启/关闭广播:hciconfig hci0 leadv/ noleadv
查看支持的链路层状态:hciconfig hci0 lestates
《《《《《《《《《《《《《《《《《《《《《《《《《《《《
1> 查看蓝牙状态 : systemctl status bluetooth
2> 查看蓝牙配置信息 : hciconfig -a
3> 修改树莓派的名字: sudo hciconfig hci0 -name new_name
4> 启动配对发现:sudo hciconfig hci0 piscan
上图红线位置为启动蓝牙时的默认配置,此时手机无法扫描到树莓派,修改配置: sudo hciconfig hci0 piscan,
配置完成后,打开手机蓝牙设置,查找树莓派蓝牙,手机与树莓派配对成功(只是配对,还不能连接)
关闭配对发现:sudo hciconfig hci0 noscan
5> 连接
a> 添加用户组 sudo usermod -G bluetooth -a pi
b> 连接配置
打开配置文件,修改如下配置:(可能需要权限)
vim /etc/systemd/system/dbus-org.bluez.service
=======================
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth
[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd -E -C #要修改的行
ExecStartPost=/usr/bin/sdptool add SP #要添加的行
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full
[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service
=======================
6> 检查哪些连接是打开的:
rfcomm -a
7> 等待外部设备配对连接
sudo rfcomm watch hci0
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
树莓派4B 蓝牙无法连接 SAP error on bluetooth service status 解决方法
启动树莓派4B 蓝牙 配对后 就自动断开 查看服务是 SAP error 解决方法
修改蓝牙服务
vi /etc/systemd/system/bluetooth.target.wants/bluetooth.service
修改
ExecStart=/usr/lib/bluetooth/bluetoothd
新值
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
--------------------------------------------------------------------------------------------------------------------------
重新加载
$ sudo systemctl daemon-reload
重启蓝牙
$ sudo service bluetooth restart
获取蓝牙状态
$ sudo service bluetooth status
bluetooth.service – Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled)
Status: “Running”
CGroup: /system.slice/bluetooth.service
└─12775 /usr/lib/bluetooth/bluetoothd –noplugin=sap
成功解决
=====================================================================
树莓派蓝牙通信源代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <termios.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>
#include <stdint.h>
int fd;
void Usart_Init()
{
struct termios Opt;
memset(&Opt ,0 ,sizeof(Opt));
tcgetattr(fd, &Opt);
cfsetispeed(&Opt,B115200);
cfsetospeed(&Opt,B115200);
Opt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
Opt.c_oflag &= ~OPOST;
Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Opt.c_cflag |= CLOCAL | CREAD;
Opt.c_cflag &= ~CSIZE;
Opt.c_cflag |= CS8;
Opt.c_cflag &= ~PARENB;
Opt.c_cflag &= ~CSTOPB;
Opt.c_cc[VTIME] = 10;
Opt.c_cc[VMIN] = 128;
tcflush(fd ,TCIFLUSH);
tcsetattr(fd,TCSANOW,&Opt);
}
void HexViewer(uint8_t *buf, int rc){
if (rc<=0) return;
int i = 0;
while (i<rc){
printf("%02X ",buf[i++]);
}
printf("\n");
}
int main(int argc, char *argv[]){
fd = open("/dev/rfcomm0",O_RDWR|O_NOCTTY|O_NDELAY);
if (fd<0) return fd;
Usart_Init();
int cn = 0;
uint8_t buf[48];
while (1){
cn = read(fd, buf, 48);
if (cn <=0) continue;
printf("Read Count:%d\n", cn);
HexViewer(buf, cn);
write(fd, buf, cn);
}
}