蓝牙控制

蓝牙控制

我有两台相邻的计算机,一台运行 Linux 控制台模式,另一台运行 Windows 10。我想在两台计算机之间传输文件,它们都具有蓝牙功能。起初,我想到插入 USB 并执行以下步骤:

fdisk -l
mount /dev/sdc1 /media
mkdir /media/myfiles01
cp ~/file1 ~/file2 /media/myfiles01
cd ~
umount /dev/sdc1

然后,我会移除 USB 设备并将其插入另一台计算机,并将文件传输给它;但是我想,为什么不使用蓝牙直接共享文件呢?

我可以从终端使用蓝牙吗?因为我的电脑上没有 GUI?我之前没有这方面的经验,所以如果可能的话,我需要详细的答案。

答案1

首先,您需要apt install bluez-tools obexpushd

要发送和接收文件,您需要先设置并配对设备。

设置


来自 Arch Wiki - 蓝牙

蓝牙控制

启动bluetoothctl交互式命令。可以在此处输入help以获取可用命令列表。

  • 输入 打开控制器电源power on。默认情况下,它是关闭的。
  • 输入devices以获取要配对的设备的 MAC 地址。
  • scan on如果设备尚未在列表中,则使用命令进入设备发现模式。
  • 使用 打开代理agent on
  • 输入 Enterpair MAC Address进行配对(Tab 键补全有效)。
  • 如果使用没有 PIN 的设备,可能需要手动信任该设备才能成功重新连接。trust MAC Address 按 Enter 即可执行此操作。
  • 最后使用connect MAC_address建立连接。

最后两点对于文件传输的发送部分来说不是必需的,但connect对于接收部分来说则需要后者。

示例会话可能如下所示:

# bluetoothctl 
[NEW] Controller 00:10:20:30:40:50 pi [default]
[bluetooth]# agent KeyboardOnly 
Agent registered
[bluetooth]# default-agent 
Default agent request successful
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:10:20:30:40:50 Discovering: yes
[NEW] Device 00:12:34:56:78:90 myLino
[CHG] Device 00:12:34:56:78:90 LegacyPairing: yes
[bluetooth]# pair 00:12:34:56:78:90
Attempting to pair with 00:12:34:56:78:90
[CHG] Device 00:12:34:56:78:90 Connected: yes
[CHG] Device 00:12:34:56:78:90 Connected: no
[CHG] Device 00:12:34:56:78:90 Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 00:12:34:56:78:90 Paired: yes
Pairing successful
[CHG] Device 00:12:34:56:78:90 Connected: no
[bluetooth]# connect 00:12:34:56:78:90
Attempting to connect to 00:12:34:56:78:90
[CHG] Device 00:12:34:56:78:90 Connected: yes
Connection successful

为了使更改永久生效,并为了使设备在重启后处于活动状态,udev需要一条规则:

/etc/udev/rules.d/10-local.rules

# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig %k up"

提示:替换KERNEL=="hci0"KERNEL=="hci[0-9]*"以匹配所有 BT 接口。

在挂起/恢复循环之后,可以使用自定义 systemd 服务自动启动设备:

/etc/systemd/system/[email protected]

[Unit]
Description=Bluetooth auto power on
After=bluetooth.service sys-subsystem-bluetooth-devices-%i.device suspend.target

[Service]
Type=oneshot
ExecStart=/usr/bin/hciconfig %i up

[Install]
WantedBy=suspend.target

使用您的蓝牙设备名称启用该设备的实例,例如[email protected]


现在您的设备已配对。检查您是否可以通过 看到另一台设备bt-device -l

发送

接下来,您必须发送systemd基础设施以使发送工作正常进行,否则您会收到以下错误:

Acquiring proxy failed: Error calling StartServiceByName for org.bluez.obex: GDBus.Error:org.freedesktop.systemd1.LoadFailed: Unit dbus-org.bluez.obex.service failed to load: No such file or directory.

systemd使用以下方式进行必要的更改

systemctl --user start obex
sudo systemctl --global enable obex

这确保你可以发送文件。sudo第一行也将失败

您现在可以通过 发送文件bluetooth-sendto --device=12:34:56:78:9A:BC filename filename2。如果传输在 100% 时挂起,则ctrlc会完成传输(或提前中止)。

要知道您的设备名称(12:34:56:78:9A:BC),您可以发出bt-device -l

收到


来自 Raspberry Pi 论坛

我们想要建立一个 OBEX 推送服务器,这就是为什么obexpushd需要它。

需要蓝牙守护程序上的兼容性标志,您必须/etc/systemd/system/dbus-org.bluez.service使用您选择的编辑器进行编辑,将-C标志添加到行尾ExecStart=。它应该看起来像这样:

ExecStart=/usr/lib/bluetooth/bluetoothd -C

编辑后,重新启动或使用 重启服务sudo systemctl daemon-reload。选择放置接收文件的特定目录,例如通过sudo mkdir /bluetooth

使用 启动服务器sudo obexpushd -B -o /bluetooth -n,它应该响应:

obexpushd 0.11.2 Copyright (C) 2006-2010 Hendrik Sattler
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Listening on bluetooth/[00:00:00:00:00:00]:9

如果这不起作用,你会得到:

obexpushd 0.11.2 Copyright (C) 2006-2010 Hendrik Sattler
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
BtOBEX_ServerRegister: Address already in use
net_init() failed

这可能是因为您正在运行另一个守护进程或程序,它占用了obexpushd默认使用的 rfcomm 通道 9。在这种情况下,请将通道更改为 23,如下所示:

sudo obexpushd -B23 -o /bluetooth -n

使用频道 23。

运行后obexpushd,打开第二个终端窗口。您可以验证 OBEX 服务是否已注册到

sudo sdptool browse local

它应该列出(在本例中为 23 频道)以下内容:

Service Name: OBEX Object Push
Service Description: a free OBEX server
Service Provider: obexpushd
Service RecHandle: 0x10005
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 23
  "OBEX" (0x0008)
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100

在该窗口中,当obexpushd仍在运行时,使用bluetoothctl进行设置discoverable on。现在从您的其他设备配对。必须在 运行时进行配对obexpushd,否则其他设备将无法识别可用的服务。如果手机已配对,请将其从您的其他设备中移除,使用 将bluetoothctl其从 Ubuntu 计算机中移除,然后重新配对。

连接后(上面列表中的最后一项),您应该能够接收文件。它们将出现在目录中/bluetooth。请注意,它们将归 root 所有,因此您需要 sudo 才能访问它们。或者您可以chmod 0777 /bluetooth对公共交换目录执行此操作,因为蓝牙身份验证是基于设备的,而不是基于用户的。

为了自动化 obexpushd 命令,请创建文件/etc/systemd/system/obexpush.service

[Unit]
Description=OBEX Push service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/obexpushd -B23 -o /bluetooth -n

[Install]
WantedBy=multi-user.target

然后将其设置为自动启动

sudo systemctl enable obexpush

重新启动或重启服务后sudo systemctl daemon-reload,您现在应该能够双向发送和接收文件。

尝试接收文件时不要忘记连接设备。

相关内容