Skip to content

Instantly share code, notes, and snippets.

@newyear2006
Last active April 21, 2021 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newyear2006/08bdf97500e2ec54cf9c116fd3561e9a to your computer and use it in GitHub Desktop.
Save newyear2006/08bdf97500e2ec54cf9c116fd3561e9a to your computer and use it in GitHub Desktop.
Bluetooth, WLAN und LTE-Modem über Powershell abfragen, ein- und ausschalten und weitere Spielereien mit Bluetooth
# Grundlage: Diese geniale Antwort auf Superuser: https://superuser.com/questions/1168551/turn-on-off-bluetooth-radio-adapter-from-cmd-powershell-in-windows-10/1293303#1293303
# Das Besondere daran, die Verwendung von Async und Await mit WindowsRuntime und alles in purem Powershell!
# hier nur die BluetoothVariante aber man kann durch Ändern der Kind-Abrage nach Wifi, MobileBroadband, FM und Other vorgehen. https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radiokind
Function Bluetooth {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
)
If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
# fügt Reference zu System.Runtime.WindowsRuntime hinzu, genauer:
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
}
# Anwendung
Bluetooth -Verbose -BluetoothStatus Off
Bluetooth -Verbose -BluetoothStatus On
# Vom Microsoft BLE Explorer folgende Zeilen ermitteln erst mal die verfügbaren Bluetooth-Geräte, erst nach diesen Zeilen wird der
# AdvertisementWatcher gestartet
# dieses erste Konstrukt kommt von: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client
[Collections.Generic.List[String]] $requestedProperties=@(
"System.Devices.Aep.Category",
"System.Devices.Aep.ContainerId",
"System.Devices.Aep.DeviceAddress",
"System.Devices.Aep.IsConnected",
"System.Devices.Aep.IsPaired",
"System.Devices.Aep.IsPresent",
"System.Devices.Aep.ProtocolId",
"System.Devices.Aep.Bluetooth.Le.IsConnectable",
"System.Devices.Aep.SignalStrength")
[Windows.Devices.Enumeration.DeviceInformation,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Enumeration.DeviceInformationKind,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
$BTLEDeviceWatcherAQSString='(System.Devices.Aep.ProtocolId:="{bb7bb05e-5972-42b5-94fc-76eaa7084d49}")'
# Alternativ den AQSString so erstellen:
$bleAQSString=[Windows.Devices.Bluetooth.BluetoothLEDevice]::GetDeviceSelectorFromDeviceName("01136B")
$deviceWatcher=[Windows.Devices.Enumeration.DeviceInformation]::CreateWatcher(($BTLEDeviceWatcherAQSString),$requestedProperties,[Windows.Devices.Enumeration.DeviceInformationKind]::AssociationEndpoint)
$deviceWatcher.Add_Added({"DW Added"})
$deviceWatcher.Add_Updated({"DW Updated"})
$deviceWatcher.Add_Removed({"DW Removed"})
$deviceWatcher.Start()
# eine BluetoothDeviceInfoID sieht z. B. so aus:
$deviceInfoID="BluetoothLE#BluetoothLE00:02:72:c8:10:72-04:a3:16:97:64:c3"
[Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceService,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
# Um mit Bluetooth weitere Dinge machen zu können, werden weitere Funktionen benötigt, dieser Part ist aber noch nicht ausgegoren:
# Doku: https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement
# https://docs.microsoft.com/de-de/windows/uwp/devices-sensors/ble-beacon
# https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementwatcher
[Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher,Windows.System.Devices,ContentType=WindowsRuntime]
$bleaw=New-Object Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher
$bleaw
$bleaw.SignalStrengthFilter
[Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData,Windows.System.Devices,ContentType=WindowsRuntime]
$blemd=New-Object Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
$blemd
$bleaw.Add_Received(
{
"Added $_"
}
)
$bleaw.Start()
$bleaw
$bleaw.ScanningMode=[Windows.Devices.Bluetooth.Advertisement.BluetoothLEScanningMode]::Active
$bleaw.Stop()
# UWP APIs von Desktop-Anwendungen ansprechen: https://blogs.windows.com/buildingapps/2017/01/25/calling-windows-10-apis-desktop-application/#DFAWBvUZF1uPs1Or.97
# hier ein Beispiel in C#, wie Bluetooth LE funktioniert: https://blog.cinlogic.com/2017/02/01/getting-started-with-ibeacons-and-windows-10/
# hier sind Probleme mit dem BLE-Scanner Intervall genannt und mögliche Lösung: https://stackoverflow.com/questions/37307301/ble-scan-interval-windows-10
# für BLE wichtig, die Company Identifier: https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers
# WPF-Beispiel auf Github: https://github.com/CarterAppleton/Win10Win32Bluetooth
# Channel 9 Video zu BLE: https://channel9.msdn.com/Events/Build/2017/P4178
# offizielle SDK-Beispiele zu Bluetooth LE: https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothLE
# Bluetooth Team Blog: https://blogs.msdn.microsoft.com/btblog/
# UWP Beispiel: https://blogs.msdn.microsoft.com/cdndevs/2017/04/28/uwp-working-with-bluetooth-devices-part-1/
@newyear2006
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment