# ================================ # Bluetooth monitoring script # ================================ Add-Type -AssemblyName System.Windows.Forms function Is-BluetoothEnabledAndInUse { Write-Host "Checking Bluetooth status..." $btStatus = Get-Service -Name bthserv -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Status if ($btStatus -ne 'Running') { Write-Host "Service bthserv is not running." return $false } $btDevices = Get-PnpDevice -Class Bluetooth | Where-Object { $_.Status -eq 'OK' } if ($btDevices.Count -gt 0) { Write-Host "Bluetooth devices detected and OK." return $true } else { Write-Host "No active Bluetooth devices found." return $false } } Write-Host "Script started. Waiting 30 seconds..." Start-Sleep -Seconds 30 while ($true) { if (-not (Is-BluetoothEnabledAndInUse)) { Write-Host "Bluetooth is off or no devices. Stopping services..." Stop-Service -Name BluetoothUserService -Force -ErrorAction SilentlyContinue Stop-Service -Name BTAGService -Force -ErrorAction SilentlyContinue Stop-Service -Name BthAvctpSvc -Force -ErrorAction SilentlyContinue sc.exe stop bthserv | Out-Null Get-Service | Where-Object { $_.Name -like "DevicesFlowUserSvc*" } | ForEach-Object { Stop-Service -Name $_.Name -Force -ErrorAction SilentlyContinue } Write-Host "Stopping finished. Script will now exit." $result = [System.Windows.Forms.MessageBox]::Show( "Bluetooth and related services have been stopped.", "Information", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) if ($result -eq [System.Windows.Forms.DialogResult]::OK) { Write-Host "OK clicked – exiting script." break } } else { Write-Host "Bluetooth is in use. Waiting another 30 seconds..." } Start-Sleep -Seconds 30 }