|
|
@@ -11,24 +11,11 @@ function getMainWindow() {
|
|
|
return windows.length > 0 ? windows[0] : null;
|
|
|
}
|
|
|
|
|
|
-// 获取已连接的 ADB 设备列表
|
|
|
+// 获取 ADB 设备列表(每次都重新扫描网络)
|
|
|
export async function getADBDevices() {
|
|
|
try {
|
|
|
- const adbPath = getCachedAdbPath();
|
|
|
- const { stdout } = await execAsync(`${adbPath} devices`);
|
|
|
- const lines = stdout.split('\n').slice(1);
|
|
|
- const devices = [];
|
|
|
-
|
|
|
- for (const line of lines) {
|
|
|
- const parts = line.trim().split(/\s+/);
|
|
|
- if (parts.length >= 2 && parts[0] && parts[1] === 'device') {
|
|
|
- devices.push({
|
|
|
- id: parts[0],
|
|
|
- status: parts[1]
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
- return devices;
|
|
|
+ // 每次都重新扫描网络设备,不记录已连接的设备
|
|
|
+ return await scanNetworkDevices(null);
|
|
|
} catch (error) {
|
|
|
console.error('获取设备列表失败:', error);
|
|
|
return [];
|
|
|
@@ -40,18 +27,39 @@ export async function scanNetworkDevices(event) {
|
|
|
const adbPath = getCachedAdbPath();
|
|
|
const baseIP = '192.168.0';
|
|
|
const port = 5555;
|
|
|
- const maxConcurrent = 20; // 限制并发数,避免过载
|
|
|
+ const maxConcurrent = 50; // 限制并发数,避免过载
|
|
|
const connectTimeout = 1500; // 连接超时时间(毫秒)
|
|
|
|
|
|
+ const foundDevices = new Set(); // 使用 Set 避免重复
|
|
|
+
|
|
|
// 生成 IP 地址列表
|
|
|
const ipList = [];
|
|
|
for (let i = 1; i <= 255; i++) {
|
|
|
ipList.push(`${baseIP}.${i}`);
|
|
|
}
|
|
|
|
|
|
- const foundDevices = new Set(); // 使用 Set 避免重复
|
|
|
+ // 推送设备发现的辅助函数
|
|
|
+ const pushDevice = (ipPort) => {
|
|
|
+ if (!foundDevices.has(ipPort)) {
|
|
|
+ foundDevices.add(ipPort);
|
|
|
+ const device = {
|
|
|
+ id: ipPort,
|
|
|
+ status: 'device'
|
|
|
+ };
|
|
|
+ // 实时发送发现的设备
|
|
|
+ if (event && event.sender) {
|
|
|
+ event.sender.send('device-found', device);
|
|
|
+ } else {
|
|
|
+ const mainWindow = getMainWindow();
|
|
|
+ if (mainWindow) {
|
|
|
+ mainWindow.webContents.send('device-found', device);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log('发现设备:', ipPort);
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
- // 分批并发扫描
|
|
|
+ // 分批并发扫描,扫描到一个显示一个
|
|
|
for (let i = 0; i < ipList.length; i += maxConcurrent) {
|
|
|
const batch = ipList.slice(i, i + maxConcurrent);
|
|
|
const promises = batch.map(async (ip) => {
|
|
|
@@ -69,41 +77,30 @@ export async function scanNetworkDevices(event) {
|
|
|
// 连接失败是正常的,继续检查设备列表
|
|
|
}
|
|
|
|
|
|
- // 直接使用 adb -s IP:PORT devices 命令检查该设备是否在列表中
|
|
|
+ // 调用 adb devices 命令获取所有设备列表,检查是否包含端口 5555 的设备
|
|
|
try {
|
|
|
- const { stdout } = await execAsync(`${adbPath} -s ${ipPort} devices`, {
|
|
|
+ const { stdout } = await execAsync(`${adbPath} devices`, {
|
|
|
timeout: 2000,
|
|
|
maxBuffer: 1024 * 1024
|
|
|
});
|
|
|
|
|
|
- // 检查输出中是否包含 IP:PORT,如果有则说明设备存在
|
|
|
- if (stdout.includes(ipPort)) {
|
|
|
- // 发现设备,实时推送
|
|
|
- if (!foundDevices.has(ipPort)) {
|
|
|
- foundDevices.add(ipPort);
|
|
|
- const device = {
|
|
|
- id: ipPort,
|
|
|
- status: 'device'
|
|
|
- };
|
|
|
- // 实时发送发现的设备
|
|
|
- if (event && event.sender) {
|
|
|
- event.sender.send('device-found', device);
|
|
|
- } else {
|
|
|
- const mainWindow = getMainWindow();
|
|
|
- if (mainWindow) {
|
|
|
- mainWindow.webContents.send('device-found', device);
|
|
|
- }
|
|
|
- }
|
|
|
- console.log('发现设备:', ipPort);
|
|
|
- }
|
|
|
+ // 检查输出中是否包含该 IP:PORT(匹配端口 5555)
|
|
|
+ // 使用正则表达式匹配 IP:5555 格式
|
|
|
+ const devicePattern = new RegExp(`${ip.replace(/\./g, '\\.')}:${port}\\s+device`, 'i');
|
|
|
+ if (devicePattern.test(stdout)) {
|
|
|
+ // 发现设备,立即推送显示(不等待整批完成)
|
|
|
+ pushDevice(ipPort);
|
|
|
return ipPort;
|
|
|
}
|
|
|
+ // 如果没有匹配到,说明没有设备
|
|
|
} catch (checkError) {
|
|
|
// 检查失败,忽略
|
|
|
}
|
|
|
return null;
|
|
|
});
|
|
|
|
|
|
+ // 并发执行当前批次,但不等待所有完成才推送
|
|
|
+ // 每个 promise 一旦发现设备就会立即推送
|
|
|
await Promise.all(promises);
|
|
|
}
|
|
|
|