adb 检测摄像头是否连接
$lostDeviceIds = @()
.\adb.exe devices | ? { $_ -match '\bdevice\b' } | Select-String '.*(?=\t)' | ForEach-Object { ($_.Matches.Value) } | % {
$info = .\adb.exe -s $_ shell ' dumpsys media.camera | grep Facing '
$output = "$_ $info"
if ( -not (($info -like '*Back*' ) -and ($info -like '*Front*' )) ) {
Write-Host $output -ForegroundColor Red
$lostDeviceIds += $_
}
else {
echo $output
}
}
echo "缺少摄像头编号"
$lostDeviceIds | % { echo $_ }
过 ip 连接检测
$lostDeviceIds = @()
1..80 |
ForEach-Object { "10.10.104.$($_)" } | # 拼接 ip
ForEach-Object {
$result = Test-Connection $_ -Count 1 -Quiet; # ping 地址
$msg = "$_ $result"
if (-not $result) {
# 手机 ip 不存在
Write-Host $msg -ForegroundColor Red # 失败仅输出
}
else{
# 检测到手机对应的 ip 地址
Write-Host $msg -ForegroundColor Green
# 拼接 adb 连接地址
$mobileAddress = "$($_):6667"
adb connect $mobileAddress | Out-Null
if(-not $?)
{
# 连接手机失败
Write-Host "$_ 连接失败" -ForegroundColor Red
}
else {
# # 调用 nc 获取外网 ip
# adb -s $mobileAddress shell "echo -e 'GET / HTTP/1.1\r\nHost: ip.3322.net\r\nConnection: close\r\n\r\n' | nc ip.3322.net 80 " | Select-String "(\d+\.){3}\d+" | % Matches | % Value
# 获取摄像头信息
$info = adb -s $mobileAddress shell ' dumpsys media.camera | grep Facing '
$output = "$_ $info"
# 检测是否包含前后摄像头
if( -not (($info -like '*Back*' ) -and ($info -like '*Front*' )) ){
Write-Host $output -ForegroundColor Red
$lostDeviceIds += $_
}
else{
Write-Output $output
}
# 断开 adb 连接
adb disconnect $mobileAddress | Out-Null
}
}
Write-Output ""
}
Write-Output "单独输出缺少摄像头编号"
$lostDeviceIds | % { echo $_ }