windows定时开关蓝牙(也可用于其他定时执行)

个人需要定时开启关闭蓝牙,遂尝试,目前已成功,网络上资料较少,做记录。

首先,定时执行,需要用到windows10定时任务计划程序。

然后,蓝牙的开关使用powershell控制。

网上看了一圈,好像没有很直接的指令直接开启关闭蓝牙。在外网找到了个解决方案。

新建一个ps1脚本

[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

直接新建文本改名改后缀为

bluetooth.ps1

即可。

然后使用powershell执行试试效果

.\bluetooth.ps1 -BluetoothStatus On

on开启off关闭就不用说了吧

能正常使用后,建立计划程序

脚本或程序选择 powershell.exe

参数如下

-Command "C:\Users\Administrator\bluetooth.ps1 -BluetoothStatus Off"

记得要用绝对路径执行,我这块懂得不多,不知道直接启用是从哪里执行的,所以直接用绝对路径。

搞好了自己运行一下看看有没有什么问题,没有就ok

如果显示被管理员拒绝

打开“控制面板->管理工具->本地安全策略”,选择“安全设置->本地策略->安全选项”,在右边列表中找到“域控制器:允许服务器操作者计划任务”,将状态改为“已启用”。

基本就这样吧。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值