1. sudo apt-get install blueman bluez*
2. vi /etc/bluetooth/main.conf
去掉行[Policy]和AutoEnable前的注释,将AutoEnable 设置为True
#FastConnectable = false
[Policy]
#
# The ReconnectUUIDs defines the set of remote services that should try
# in later on. Defaults to 'false'.
AutoEnable=true
3. sudo vi /lib/udev/rules.d/50-bluetooth-hci-auto-poweron.rules
屏蔽下面行
# Set bluetooth power up
#ACTION=="add", SUBSYSTEM=="bluetooth", KERNEL=="hci[0-9]*", RUN+="/bin/hciconfig %k up"
4. 重启系统
5. 检查蓝牙服务是否开启
#开启蓝牙
sudo rfkill unblock bluetooth
sudo systemctl status bluetooth
sudo systemctl status nvwifibt.service
6. 现在可以查看是否有蓝牙适配器
root@tegra-ubuntu:/home/nvidia# hciconfig
hci0: Type: BR/EDR Bus: UART
BD Address: 00:04:4B:8D:3B:FF ACL MTU: 1021:8 SCO MTU: 64:1
UP RUNNING
RX bytes:836 acl:0 sco:0 events:58 errors:0
TX bytes:4531 acl:0 sco:0 commands:58 errors:0
二 蓝牙配对
运行bluetoothctl命令
# bluetoothctl
列出可用的蓝牙控制器
[bluetooth]# list
选择使用的蓝牙控制器
[bluetooth]# select 01:23:45:67:89:AB
显示蓝牙控制器的详细信息
bluetooth]# show
打开蓝牙
[bluetooth]# power on
开始扫描
[bluetooth]# scan on
结束扫描
[bluetooth]# scan off
选择蓝牙设备配对
[bluetooth]# pair 34:88:5D:87:C0:A6(你的蓝牙设备)
连接到蓝牙设备
[bluetooth]# connect FF:FF:98:00:FC:B0
信任设备
[bluetooth]# trust FF:FF:98:00:FC:B0
三 配对成功后,生成 在/dev/input/event4 设备输入文件,我连接的是个蓝牙键盘
编写测试用例 检测 蓝牙键盘
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int key_fd=-1;
key_fd = open("/dev/input/event4", O_RDONLY);
if(key_fd < 0)
{
printf("open /dev/input/event4 failded...\n");
}
while(1){
struct input_event t;
if (read (key_fd, &t, sizeof (t)) == sizeof(t)){
if (t.type == EV_KEY)
printf("key code %d value %d\n",t.code,t.value );
}
}
}