device-info.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { ipcMain } from 'electron';
  2. import { exec } from 'child_process';
  3. import { promisify } from 'util';
  4. import { getCachedAdbPath } from '../config.js';
  5. const execAsync = promisify(exec);
  6. // 获取设备分辨率
  7. export async function getDeviceResolution(ipPort) {
  8. if (!ipPort) {
  9. return { success: false, error: '缺少设备 ID' };
  10. }
  11. try {
  12. // 使用 wm size 命令获取设备分辨率
  13. const adbPath = getCachedAdbPath();
  14. const { stdout } = await execAsync(`${adbPath} -s ${ipPort} shell wm size`);
  15. // 输出格式通常是: "Physical size: 1080x2400" 或 "1080x2400"
  16. const match = stdout.match(/(\d+)x(\d+)/);
  17. if (match) {
  18. return {
  19. success: true,
  20. width: parseInt(match[1], 10),
  21. height: parseInt(match[2], 10)
  22. };
  23. }
  24. // 如果解析失败,返回默认值
  25. return { success: true, width: 1280, height: 2400 };
  26. } catch (error) {
  27. console.error('获取设备分辨率失败:', error);
  28. // 返回默认值
  29. return { success: true, width: 1280, height: 2400 };
  30. }
  31. }
  32. // 注册 IPC 处理器
  33. export function registerIpcHandlers() {
  34. // IPC 处理程序:获取设备分辨率
  35. ipcMain.handle('get-device-resolution', async (event, ipPort) => {
  36. return await getDeviceResolution(ipPort);
  37. });
  38. }