从终端通过蓝牙在两台计算机之间共享文件

8 windows sharing usb bluetooth console

我有两台相邻的计算机,一台运行带有控制台模式的 linux,另一台运行 Windows 10。我想在两者之间传输文件,而且它们都具有蓝牙功能。一开始,我想到了插一个usb,执行如下步骤:

fdisk -l
mount /dev/sdc1 /media
mkdir /media/myfiles01
cp ~/file1 ~/file2 /media/myfiles01
cd ~
umount /dev/sdc1
Run Code Online (Sandbox Code Playgroud)

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

我可以从终端使用蓝牙,因为那台计算机上没有 GUI?我没有这方面的经验,所以如果可能的话,我需要详细的答案。

emk*_*203 5

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

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

设置


来自 Arch Wiki - 蓝牙

蓝牙控制

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

  • 通过输入打开控制器的电源power on。默认情况下它是关闭的。
  • 输入devices以获取要配对的设备的 MAC 地址。
  • scan on如果设备尚未出现在列表中,请使用命令进入设备发现模式。
  • 用 开启代理agent on
  • 输入pair MAC Address进行配对(选项卡完成工作)。
  • 如果使用没有 PIN 的设备,则可能需要手动信任该设备才能成功重新连接。输入trust MAC Address 这样做。
  • 最后,使用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
Run Code Online (Sandbox Code Playgroud)

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

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

# Set bluetooth power up
ACTION=="add", KERNEL=="hci0", RUN+="/usr/bin/hciconfig %k up"
Run Code Online (Sandbox Code Playgroud)

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

在挂起/恢复周期后,设备可以使用自定义 systemd 服务自动开机:

/etc/systemd/system/bluetooth-auto-power@.service

[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
Run Code Online (Sandbox Code Playgroud)

使用您的蓝牙设备名称启用设备实例,例如bluetooth-auto-power@hci0.service


现在您的设备已配对。检查您是否可以看到另一个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.
Run Code Online (Sandbox Code Playgroud)

进行必要的systemd更改

systemctl --user start obex
sudo systemctl --global enable obex
Run Code Online (Sandbox Code Playgroud)

这可确保您可以发送文件。sudo第一行中的A也会失败

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

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

收到


来自树莓派论坛

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

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

ExecStart=/usr/lib/bluetooth/bluetoothd -C
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

sudo obexpushd -B23 -o /bluetooth -n
Run Code Online (Sandbox Code Playgroud)

使用频道 23。

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

sudo sdptool browse local
Run Code Online (Sandbox Code Playgroud)

它应该列出(在这种情况下在频道 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
Run Code Online (Sandbox Code Playgroud)

在该窗口中,当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
Run Code Online (Sandbox Code Playgroud)

然后,将其设置为自动启动

sudo systemctl enable obexpush
Run Code Online (Sandbox Code Playgroud)

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

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