Mar*_*cka 36 windows bluetooth command-line windows-10
这么简单的任务,有人会说,我还没有找到令人满意的解决方案。我尝试过的(通过蓝牙连接的扬声器播放音乐以真正了解收音机的状态):
devcon disable USB\VID_8087&PID_07DC&REV_0001
这是我的蓝牙适配器的硬件 ID)...需要重新启动才能工作...Disable-NetAdapter "Bluetooth Network Connection 3"
这是我的蓝牙适配器名称的翻译)...它禁用了 PAN 驱动程序,但蓝牙扬声器继续播放音乐...net stop bthserv
...实际上并没有关闭收音机(BT 扬声器继续播放音乐)ms-settings:bluetooth
或explorer.exe %LocalAppData%\Packages\windows.immersivecontrolpanel_cw5n1h2txyewy\LocalState\Indexed\Settings\cs-CZ\AAA_SettingsPagePCSystemBluetooth.settingcontent-ms
...打开蓝牙设置面板,但我仍然需要点击切换我不敢相信微软会如此无知,不提供这样的命令......
Ben*_*n N 36
由于与 WinRT 的必要互操作,这具有挑战性,但在纯 PowerShell 中是可能的:
[CmdletBinding()] Param (
[Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
)
If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
Run Code Online (Sandbox Code Playgroud)
要使用它,请将其保存为 PS1 文件,例如bluetooth.ps1
. 如果您还没有,请按照PowerShell 标记 wiki的启用脚本部分中的说明启用脚本在您的系统上的执行。然后您可以从 PowerShell 提示符运行它,如下所示:
.\bluetooth.ps1 -BluetoothStatus On
Run Code Online (Sandbox Code Playgroud)
要关闭蓝牙,请Off
改为通过。
要从批处理文件运行它:
powershell -command .\bluetooth.ps1 -BluetoothStatus On
Run Code Online (Sandbox Code Playgroud)
警告:如果蓝牙支持服务未运行,脚本会尝试启动它,否则 WinRT 将看不到蓝牙无线电。唉,如果脚本不是以管理员身份运行,则无法启动该服务。为了避免这种情况,您可以将该服务的启动类型更改为自动。
现在做一些解释。前三行确定脚本采用的参数。在认真开始之前,我们确保蓝牙支持服务正在运行,如果没有,请启动它。然后我们加载System.Runtime.WindowsRuntime
程序集,以便我们可以使用该WindowsRuntimeSystemExtensions.AsTask
方法将 WinRT 样式的任务(.NET/PowerShell 不理解)转换为 .NET Task
s。这个特定的方法有一大堆不同的参数集,这些参数集似乎会阻碍 PowerShell 的重载解析,所以在下一行中,我们得到了只需要一个有结果的 WinRT 任务的特定方法。然后我们定义一个函数,我们将多次使用该函数从异步 WinRT 任务中提取适当类型的结果。在该函数的声明之后,我们从 WinRT 元数据加载两个必要的类型. 脚本的其余部分几乎只是您在答案中编写的 C# 代码的 PowerShell 翻译;它使用Radio
WinRT 类来查找和配置蓝牙无线电。
在放弃寻找现成的解决方案后,我发现通用 Windows 平台应用程序可以访问无线电控制 API。
所以我将一个示例无线电控制应用程序与一个命令行控制的应用程序合并,结果在这里:https : //github.com/peci1/RadioControl。它已在 Windows Store上发布。
一般来说,不可能让应用程序在后台运行,所以你需要先启动 GUI,然后你可以从命令行调用它,比如
radiocontrol.exe 0 on
radiocontrol.exe Bluetooth off
Run Code Online (Sandbox Code Playgroud)
这个应用程序需要的核心功能是:
using Windows.Devices.Radios;
await Radio.RequestAccessAsync();
var radios = await Radio.GetRadiosAsync();
await radios[0].SetStateAsync(RadioState.On);
Run Code Online (Sandbox Code Playgroud)
然后,您还需要将 Radios 功能添加到应用清单中:
<DeviceCapability Name="radios" />
Run Code Online (Sandbox Code Playgroud)
并且要允许从命令行进行控制,您需要覆盖protected override async void OnActivated(IActivatedEventArgs args)
App.xaml.cs 中的方法并在这样的分支中进行处理:
switch (args.Kind)
{
case ActivationKind.CommandLineLaunch:
Run Code Online (Sandbox Code Playgroud)
这个 Autohotkey 脚本似乎可以解决问题。我对那里感到困扰Sleep
,但似乎窗口在准备好接收我的击键之前就变得活跃了。
SendMode Input
Run, ms-settings:bluetooth
WinWaitActive, Settings
Sleep 300
Send,{Tab}{Space}
WinClose, A
Run Code Online (Sandbox Code Playgroud)
交叉发布到/sf/ask/3409018791/以找出Sleep 300
可以替换的内容。
归档时间: |
|
查看次数: |
119880 次 |
最近记录: |