Bluetooth-Any
view release on metacpan or search on metacpan
lib/Bluetooth/Any.pm view on Meta::CPAN
system "rfkill", "unblock", "bluetooth";
unless ($?) {
return [200, "OK", undef, {'func.method'=>'rfkill'}];
}
}
[500, "Failed, no methods succeeded"];
}
$SPEC{'turn_off_bluetooth'} = {
v => 1.1,
summary => 'Turn off Bluetooth',
description => <<'_',
Will try:
- rfkill
_
};
sub turn_off_bluetooth {
my %args = @_;
RFKILL:
{
unless (which("rfkill")) {
log_trace "Cannot find rfkill, skipping using rfkill";
last;
}
log_trace "Using rfkill to turn bluetooth off";
system "rfkill", "block", "bluetooth";
unless ($?) {
return [200, "OK", undef, {'func.method'=>'rfkill'}];
}
}
[500, "Failed, no methods succeeded"];
}
$SPEC{'bluetooth_is_on'} = {
v => 1.1,
summary => 'Return true when bluetooth is on, or 0 otherwise',
description => <<'_',
Will try:
- rfkill
_
};
sub bluetooth_is_on {
my %args = @_;
RFKILL:
{
unless (which("rfkill")) {
log_trace "Cannot find rfkill, skipping using rfkill";
last;
}
log_trace "Using rfkill to check bluetooth status";
my $out;
system {capture_stdout=>\$out}, "rfkill", "list", "bluetooth";
last if $?;
my $in_bt;
my $unblocked;
for (split /^/m, $out) {
if (/^\d/) {
if (/bluetooth/i) {
$in_bt = 1;
} else {
$in_bt = 0;
}
next;
} else {
if (/blocked:\s*yes/i) {
return [200, "OK", 0, {'func.method'=>'rfkill', 'cmdline.result'=>'Bluetooth is OFF', 'cmdline.exit_code'=>1}];
} elsif (/blocked:\s*no/i) {
$unblocked = 0;
}
}
}
if (defined $unblocked) {
return [200, "OK", 1, {'func.method'=>'rfkill', 'cmdline.result'=>'Bluetooth is on', 'cmdline.exit_code'=>0}];
} else {
log_warn "Cannot detect 'blocked: no' from 'rfkill list' output, skipping using rfkill";
last;
}
}
[500, "Failed, no methods succeeded"];
}
1;
# ABSTRACT: Common interface to bluetooth functions
__END__
=pod
=encoding UTF-8
=head1 NAME
Bluetooth::Any - Common interface to bluetooth functions
=head1 VERSION
This document describes version 0.002 of Bluetooth::Any (from Perl distribution Bluetooth-Any), released on 2018-10-24.
=head1 DESCRIPTION
This module provides common functions related to bluetooth.
=head1 FUNCTIONS
=head2 bluetooth_is_on
Usage:
bluetooth_is_on() -> [status, msg, payload, meta]
Return true when bluetooth is on, or 0 otherwise.
Will try:
- rfkill
This function is not exported by default, but exportable.
No arguments.
Returns an enveloped result (an array).
First element (status) is an integer containing HTTP status code
(200 means OK, 4xx caller error, 5xx function error). Second element
(msg) is a string containing error message, or 'OK' if status is
200. Third element (payload) is optional, the actual result. Fourth
element (meta) is called result metadata and is optional, a hash
that contains extra information.
Return value: (any)
=head2 turn_off_bluetooth
( run in 1.537 second using v1.01-cache-2.11-cpan-39bf76dae61 )