#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pfds[2];
int status;
pid_t pid;
if ( pipe(pfds) == -1 )
{
perror("pipe error\n");
exit(1);
}
pid = fork();
if ( pid == 0 ) //child, run the bluetoothctl command
{
close(STDIN_FILENO);
dup2( pfds[0], STDIN_FILENO );
close( pfds[1] );
execl("/usr/bin/bluetoothctl", "bluetoothctl", (char*) NULL);
exit(0);
}
else //father, send the command to bluetoothctl
{
close(STDOUT_FILENO);
dup2( pfds[1], STDOUT_FILENO );
write(STDOUT_FILENO, "power on\n", strlen("power off\n"));
sleep(1);
write(STDOUT_FILENO, "agent on\n", strlen("agent on\n"));
write(STDOUT_FILENO, "default-agent\n", strlen("default-agent\n"));
write(STDOUT_FILENO, "pairable on\n", strlen("pairable on\n"));
write(STDOUT_FILENO, "pair A8:87:B3:1E:D9:66\n", strlen("pair A8:87:B3:1E:D9:66\n"));
write(STDOUT_FILENO, "trust A8:87:B3:1E:D9:66\n", strlen("trust A8:87:B3:1E:D9:66\n"));
write(STDOUT_FILENO, "connect A8:87:B3:1E:D9:66\n", strlen("connect A8:87:B3:1E:D9:66\n"));
sleep(2);
write(STDOUT_FILENO, "exit\n", strlen("exit\n"));
write(STDOUT_FILENO, "connect A8:87:B3:1E:D9:66\n", strlen("connect A8:87:B3:1E:D9:66\n"));
close( pfds[0] );
waitpid((pid_t)pid, &status, 0);
exit(0);
}
}
ref:Bluetooth A2DP
無法啟動 power on
[bluetooth]# power on
Failed to set power on: org.bluez.Error.Blocked
查找原因
$ sudo systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-02-22 17:08:08 CST; 1min 55s ago
Docs: man:bluetoothd(8)
Main PID: 4723 (bluetoothd)
Status: "Running"
Tasks: 1 (limit: 4915)
CGroup: /system.slice/bluetooth.service
└─4723 /usr/lib/bluetooth/bluetoothd
二 22 17:08:08 hello-W230ST systemd[1]: Starting Bluetooth service...
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Bluetooth daemon 5.48
二 22 17:08:08 hello-W230ST systemd[1]: Started Bluetooth service.
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Starting SDP server
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Bluetooth management interface 1.14 initialized
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Failed to set mode: Blocked through rfkill (0x12)
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Endpoint registered: sender=:1.91 path=/MediaEndpoint/A2DPSource
二 22 17:08:08 hello-W230ST bluetoothd[4723]: Endpoint registered: sender=:1.91 path=/MediaEndpoint/A2DPSink
二 22 17:09:55 hello-W230ST bluetoothd[4723]: Failed to set mode: Blocked through rfkill (0x12)
$ sudo systemctl restart bluetooth.service <-沒用
$ sudo rfkill list
0: hci0: Bluetooth
Soft blocked: yes <- 被block住了@@?
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
解決
$ sudo rfkill unblock bluetooth
$ sudo rfkill list
0: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
1: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
ref:
1. Cannot unblock soft-blocked bluetooth
沒有留言:
張貼留言