device-info.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // 返回默认值
  28. return { success: true, width: 1280, height: 2400 };
  29. }
  30. }
  31. // 注册 IPC 处理器
  32. export function registerIpcHandlers() {
  33. // IPC 处理程序:获取设备分辨率
  34. ipcMain.handle('get-device-resolution', async (event, ipPort) => {
  35. return await getDeviceResolution(ipPort);
  36. });
  37. }