Files

71 lines
2.3 KiB
PowerShell
Raw Permalink Normal View History

# Monitor and pairing script for LOGI K370 B
$deviceName = "LOGI K370 B"
# 1. 시스템에 등록된 LOGI K370 B 장치 조회
$devs = @(Get-PnpDevice -FriendlyName "*$deviceName*" -ErrorAction SilentlyContinue)
$hasConnected = $false
$connectedInstance = $null
foreach ($d in $devs) {
$prop = Get-PnpDeviceProperty -InstanceId $d.InstanceId -KeyName "{83DA6326-97A6-4088-9453-A1923F573B29} 15" -ErrorAction SilentlyContinue
$isConn = ($prop -and $prop.Data -eq $true)
if ($isConn) {
$hasConnected = $true
$connectedInstance = $d
}
}
function Format-Mac($raw) {
$parts = @()
for ($i = 0; $i -lt $raw.Length; $i += 2) {
$parts += $raw.Substring($i, 2).ToLower()
}
return [string]::Join(":", $parts)
}
# 2. 이미 정상 연결된 경우 -> 과거 잔여 인스턴스가 있다면 정리하고 CONNECTED 반환
if ($hasConnected) {
foreach ($d in $devs) {
if ($d.InstanceId -ne $connectedInstance.InstanceId) {
$addr = (Get-PnpDeviceProperty -InstanceId $d.InstanceId -KeyName "DEVPKEY_Bluetooth_DeviceAddress" -ErrorAction SilentlyContinue).Data
if ($addr) {
$fMac = Format-Mac $addr
& .\btpair.exe unpair-by-mac --mac $fMac --type BluetoothLE | Out-Null
}
}
}
Write-Output "CONNECTED"
exit 0
}
# 3. 연결 해제 상태: 과거 잔여 페어링 unpair 정리
foreach ($d in $devs) {
$addr = (Get-PnpDeviceProperty -InstanceId $d.InstanceId -KeyName "DEVPKEY_Bluetooth_DeviceAddress" -ErrorAction SilentlyContinue).Data
if ($addr) {
$fMac = Format-Mac $addr
Write-Output "[DEBUG] Unpairing stale instance: $fMac"
& .\btpair.exe unpair-by-mac --mac $fMac --type BluetoothLE | Out-Null
}
}
# 4. 실시간 BLE 광고 스캔 및 발견 즉시 자동 페어링 시도 (스캔 2초, PIN 입력 대기 45초)
$pairRes = & .\BlePairer.exe watch-and-pair 2 45
foreach ($outLine in $pairRes) {
if ($outLine -match "Active advertisement detected|Device found|PairingRequested|SUCCESS|FAILED|Timeout|Exception") {
Write-Output "[DEBUG] $outLine"
}
if ($outLine -match "PIN_CODE:(\d+)") {
Write-Output "PIN_REQUIRED: $($Matches[1])"
}
}
$allOut = $pairRes -join ' '
if ($allOut -match "SUCCESS") {
Write-Output "PAIRED_AND_CONNECTED"
exit 0
}
Write-Output "CLEANED_AND_WAITING"