From c8a6cb94b2e9beed484685d6b4723c14ba18b471 Mon Sep 17 00:00:00 2001 From: AkiChase <1003019131@qq.com> Date: Tue, 30 Apr 2024 22:02:40 +0800 Subject: [PATCH] feat(Device): add wireless connections --- README.md | 2 +- src-tauri/src/adb.rs | 18 ++++++++++++++++++ src-tauri/src/main.rs | 13 +++++++++++++ src/components/Device.vue | 26 ++++++++++++++++++++++++++ src/invoke.ts | 4 ++++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 47bdd96..767a872 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Due to the delay and blurred image quality of the mirror screen. This project fo ## Features -- [ ] Wired and wireless connections to Android devices +- [x] Wired and wireless connections to Android devices - [x] Start scrcpy-server and connect to it - [x] Implement scrcpy client control protocol - [x] Mouse and keyboard mapping diff --git a/src-tauri/src/adb.rs b/src-tauri/src/adb.rs index 9d7404e..6f7985a 100644 --- a/src-tauri/src/adb.rs +++ b/src-tauri/src/adb.rs @@ -166,4 +166,22 @@ impl Adb { .context("Failed to execute 'adb start-server'")?; Ok(()) } + + pub fn cmd_connect(res_dir: &PathBuf, address: &str) -> Result { + let mut adb_command = Adb::cmd_base(res_dir); + let output = adb_command + .args(&["connect", address]) + .output() + .context(format!("Failed to execute 'adb connect {}'", address))?; + + let res = String::from_utf8(output.stdout)?; + Ok(res) + } +} + +#[test] +fn t() { + let res_dir = PathBuf::from("/Users/akichase/Projects/github/scrcpy-mask/src-tauri/resource/"); + let res = Adb::cmd_connect(&res_dir, "127.0.0.1:1234").unwrap(); + println!("{}", res) } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 2de74f3..06e3c6c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -109,6 +109,7 @@ fn start_scrcpy_server( } #[tauri::command] +/// get device screen size fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32), String> { let dir = app.path().resource_dir().unwrap().join("resource"); match ScrcpyClient::get_device_screen_size(&dir, &id) { @@ -118,6 +119,17 @@ fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32 } #[tauri::command] +/// connect to wireless device +fn adb_connect(address: String, app: tauri::AppHandle) -> Result { + let dir = app.path().resource_dir().unwrap().join("resource"); + match Adb::cmd_connect(&dir, &address) { + Ok(res) => Ok(res), + Err(e) => Err(e.to_string()), + } +} + +#[tauri::command] +/// load default key mapping config file fn load_default_keyconfig(app: tauri::AppHandle) -> Result { let dir = app.path().resource_dir().unwrap().join("resource"); let file = ResHelper::get_file_path(&dir, ResourceName::DefaultKeyConfig); @@ -205,6 +217,7 @@ async fn main() { push_server_file, start_scrcpy_server, get_device_screen_size, + adb_connect, load_default_keyconfig ]) .run(tauri::generate_context!()) diff --git a/src/components/Device.vue b/src/components/Device.vue index 136d6a0..034e5d7 100644 --- a/src/components/Device.vue +++ b/src/components/Device.vue @@ -16,10 +16,12 @@ import { forwardServerPort, startScrcpyServer, getDeviceScreenSize, + adbConnect, } from "../invoke"; import { NH4, NP, + NInput, NInputNumber, NButton, NDataTable, @@ -35,6 +37,7 @@ import { DropdownOption, useDialog, useMessage, + NInputGroup, } from "naive-ui"; import { CloseCircle, InformationCircle } from "@vicons/ionicons5"; import { Refresh } from "@vicons/ionicons5"; @@ -48,6 +51,7 @@ const store = useGlobalStore(); const message = useMessage(); const port = ref(27183); +const address = ref(""); const localStore = new Store("store.bin"); @@ -257,6 +261,18 @@ async function refreshDevices() { devices.value = await adbDevices(); store.hideLoading(); } + +async function connectDevice() { + if (!address.value) { + message.error("请输入无线调试地址"); + return; + } + + store.showLoading(); + const msg = await adbConnect(address.value); + store.hideLoading(); + message.info(msg); +}