Announcement

Collapse
No announcement yet.

Turning Bluetooth off and on

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Turning Bluetooth off and on

    I'm looking for a way to have Homeseer HS3 turn off Bluetooth and then turn it back on again. Does anyone have any ideas on how to do this?
    I'm using Windows 10.

    I have a Powershell script that does it and it works when I kick it off from a batch file but it fails when I execute the batch file from HS3. I have no experience with Powershell so I'm flummoxed. Perhaps there are some Powershell experts out there who could help?

    Thanks
    Jez

    #2
    Sounds like a Windows user rights issue. Are the windows users the same in both scenarios you describe above?


    Sent from my Pixel 2 using Tapatalk

    Comment


      #3

      Comment


        #4
        Create a Windows shortcut to the BAT file and set that to run as an Administrator. Then get Homeseer to run the shortcut.
        Jon

        Comment


          #5
          Good suggestion but no, it still fails.

          Comment


            #6
            Are you running Homeseer as an administrator?
            Jon

            Comment


              #7
              Yup. Just tried that as well.

              This is the Powershell code which is in script bluetoothOff.ps1. Excuse the extraneous bits.....

              Set-ExecutionPolicy Unrestricted -Scope Process
              Get-ExecutionPolicy -List

              $BluetoothStatus = "Off"

              #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)
              $netTask.Result
              }
              [Windows.Devices.Radios.Radio,Windows.System.Devices,ContentT ype=WindowsRuntime]
              [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devi ces,ContentType=WindowsRuntime]
              Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus])
              $radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
              $radios
              $bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
              [Windows.Devices.Radios.RadioState,Windows.System.Devices,Con tentType=WindowsRuntime]
              Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus])

              BluetoothOff.ps1 is executed by jongo.bat which contains.....
              C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell -noexit -executionpolicy Bypass c:\data\scripts\bluetoothOff.ps1

              - Run the batch from Windows Explorer and it turns off Bluetooth.
              - Execute it from HS3 and I get:
              You cannot call a method on a null-valued expression.
              At C:\data\scripts\bluetoothOff.ps1:22 char:1
              + Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices. ...
              + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
              + CategoryInfo : InvalidOperation: ( [], RuntimeException
              + FullyQualifiedErrorId : InvokeMethodOnNull

              I suspect its to do with the execution policy but I'm stumped. Any help gratefully received.

              Comment

              Working...
              X