Skip to content

Commit

Permalink
fix: 🐛 Solve the problem of Windows disabling video forwarding and cr…
Browse files Browse the repository at this point in the history
…ashing
  • Loading branch information
viarotel committed Dec 19, 2024
1 parent 14306b2 commit 10b902a
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 55 deletions.
52 changes: 42 additions & 10 deletions electron/exposes/adb/helpers/adbConnectionMonitor/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Bonjour } from 'bonjour-service'
import net from 'node:net'
import appStore from '$electron/helpers/store.js'

export const MDNS_CONFIG = {
PAIRING_TYPE: 'adb-tls-pairing',
Expand Down Expand Up @@ -78,6 +79,7 @@ export class AdbConnectionMonitor {
this.deviceScanner = new DeviceScanner()
this.isActive = false
this.adb = null
this.onStatus = () => {}
}

async startQrCodeScanning(options) {
Expand All @@ -87,28 +89,47 @@ export class AdbConnectionMonitor {
adb,
password,
onStatus = () => {},
onError = () => {},
} = options

this.adb = adb
this.isActive = true
this.onStatus = onStatus

try {
const device = await this.scanForDevice(onStatus)
this.onStatus('pairing')
const device = await this.scanForDevice()
await this.pairWithDevice(device, password)
onStatus('Paired successfully, waiting to connect...')

const connectDevice = await this.waitForDeviceConnect(device)
console.log('connectDevice', connectDevice)
await this.connectToDevice(connectDevice)
this.onStatus('connecting')
try {
const connectDevice = await this.waitForDeviceConnect(device)
await this.connectToDevice(connectDevice)
}
catch (error) {
if (error.code === ERROR_CODES.TIMEOUT) {
this.onStatus('connecting-fallback')
// 使用回退端口尝试连接
const fallbackPort = this.getBackPort(device)
await this.connectToDevice({
...device,
port: fallbackPort,
})
}
else {
throw error
}
}

this.onStatus('connected')

return {
success: true,
device,
}
}
catch (error) {
onError(error.message)
this.onStatus('error', error.message)

return {
success: false,
error: error.message,
Expand All @@ -134,9 +155,7 @@ export class AdbConnectionMonitor {
}
}

async scanForDevice(onStatus) {
onStatus('Waiting for device to scan QR code...')

async scanForDevice() {
return new Promise((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
this.dispose()
Expand Down Expand Up @@ -205,6 +224,19 @@ export class AdbConnectionMonitor {
}
}

getBackPort(device) {
const devices = appStore.get('device')

const value = Object.entries(devices).reduce((port, [key, value]) => {
if (key.includes(device.address)) {
port = key.split(':')[1]
}
return port
}, 5555)

return value
}

dispose() {
this.deviceScanner.dispose()
this.isActive = false
Expand Down
3 changes: 2 additions & 1 deletion electron/exposes/adb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,14 @@ async function pair(host, port, code) {
return shell(`pair ${host}:${port} ${code}`)
}

async function connectCode(password) {
async function connectCode(password, options = {}) {
return adbConnectionMonitor.startQrCodeScanning({
password,
adb: {
pair,
connect,
},
...options
})
}

Expand Down
Binary file added electron/resources/extra/win/scrcpy/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"fs-extra": "11.2.0",
"husky": "9.0.11",
"lodash-es": "4.17.21",
"multicast-dns": "^7.2.5",
"nanoid": "5.0.7",
"pinia": "2.1.7",
"pinia-plugin-persistedstate": "3.2.1",
Expand Down
10 changes: 10 additions & 0 deletions src/locales/languages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"common.warning": "Warning",
"common.info": "Message",
"common.danger": "Error",
"common.connecting": "Connecting",

"common.language.name": "Language",
"common.language.placeholder": "Select language",
Expand Down Expand Up @@ -62,6 +63,10 @@
"device.remark": "Remark",
"device.permission.error": "Device permission error, please reconnect device and allow USB debugging",
"device.terminal.name": "Terminal",
"device.status": "Status",
"device.status.connected": "Connected",
"device.status.offline": "Offline",
"device.status.unauthorized": "Unauthorized",

"device.task.name": "Scheduled Task",
"device.task.tips": " Note: Please ensure that your computer stays awake, otherwise scheduled tasks will not be executed properly.",
Expand All @@ -83,6 +88,11 @@
"device.wireless.name": "Wireless",
"device.wireless.mode": "Wireless",
"device.wireless.mode.error": "Do not get the local area network connection address, please check the network",
"device.wireless.connect.qr": "QR Code Connection",
"device.wireless.connect.qr.pairing": "Pairing",
"device.wireless.connect.qr.connecting": "Connecting",
"device.wireless.connect.qr.connected": "Connection Successful",
"device.wireless.connect.qr.error": "QR Code Connection",
"device.wireless.connect.name": "Connect",
"device.wireless.connect.error.title": "Connect failed",
"device.wireless.connect.error.detail": "Error details",
Expand Down
10 changes: 10 additions & 0 deletions src/locales/languages/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"common.warning": "Предупреждение",
"common.info": "Сообщение",
"common.danger": "Ошибка",
"common.connecting": "Подключение",

"common.language.name": "Язык",
"common.language.placeholder": "Выберите язык",
Expand Down Expand Up @@ -62,6 +63,10 @@
"device.remark": "Примечание",
"device.permission.error": "Ошибка разрешения устройства, пожалуйста, переподключите устройство и разрешите отладку по USB",
"device.terminal.name": "Терминал",
"device.status": "Статус",
"device.status.connected": "Подключено",
"device.status.offline": "Не в сети",
"device.status.unauthorized": "Не авторизован",

"device.task.name": "Запланированная задача",
"device.task.tips": "Примечание: Пожалуйста, убедитесь, что ваш компьютер не переходит в спящий режим, иначе запланированные задачи не будут выполнены правильно.",
Expand All @@ -83,6 +88,11 @@
"device.wireless.name": "По WiFi",
"device.wireless.mode": "беспроводной",
"device.wireless.mode.error": "Не удалось получить адрес подключения локальной сети, проверьте сеть",
"device.wireless.connect.qr": "Подключение по QR-коду",
"device.wireless.connect.qr.pairing": "Сопряжение",
"device.wireless.connect.qr.connecting": "Подключение",
"device.wireless.connect.qr.connected": "Подключение успешно",
"device.wireless.connect.qr.error": "Подключение по QR-коду",
"device.wireless.connect.name": "Подключиться",
"device.wireless.connect.error.title": "Не удалось подключиться",
"device.wireless.connect.error.detail": "Детали ошибки",
Expand Down
4 changes: 4 additions & 0 deletions src/locales/languages/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
"device.wireless.mode": "无线模式",
"device.wireless.mode.error": "没有获取到局域网连接地址,请检查网络",
"device.wireless.connect.qr": "二维码连接",
"device.wireless.connect.qr.pairing": "配对中",
"device.wireless.connect.qr.connecting": "连接中",
"device.wireless.connect.qr.connected": "连接成功",
"device.wireless.connect.qr.error": "二维码连接",
"device.wireless.connect.name": "连接设备",
"device.wireless.connect.error.title": "连接设备失败",
"device.wireless.connect.error.detail": "错误详情",
Expand Down
10 changes: 10 additions & 0 deletions src/locales/languages/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"common.warning": "警告",
"common.info": "消息",
"common.danger": "錯誤",
"common.connecting": "連接中",

"common.language.name": "語言",
"common.language.placeholder": "選擇你要的語言",
Expand Down Expand Up @@ -62,6 +63,10 @@
"device.remark": "備註",
"device.permission.error": "裝置權限錯誤,請重新連接裝置並允許 USB 偵錯",
"device.terminal.name": "終端偵錯",
"device.status": "狀態",
"device.status.connected": "已連接",
"device.status.offline": "已離線",
"device.status.unauthorized": "未授權",

"device.task.name": "計劃任務",
"device.task.tips": "注意:請確保您的電腦保持唤醒状态,否則計劃任務將無法正常執行。",
Expand All @@ -83,6 +88,11 @@
"device.wireless.name": "無線",
"device.wireless.mode": "無線模式",
"device.wireless.mode.error": "未取得區域網路連接位址,請檢查網路",
"device.wireless.connect.qr": "二維碼連接",
"device.wireless.connect.qr.pairing": "配對中",
"device.wireless.connect.qr.connecting": "連接中",
"device.wireless.connect.qr.connected": "連接成功",
"device.wireless.connect.qr.error": "二維碼連接",
"device.wireless.connect.name": "連接裝置",
"device.wireless.connect.error.title": "連接裝置失敗",
"device.wireless.connect.error.detail": "錯誤詳細資訊",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/device/components/DevicePopover/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<el-popover
ref="popoverRef"
placement="right"
:width="300"
:width="450"
trigger="hover"
popper-class="!p-0 !overflow-hidden !rounded-xl"
>
<template #reference>
<el-link type="primary" :underline="false" icon="InfoFilled" class="mr-1"></el-link>
</template>

<el-descriptions class="!w-full" border>
<el-descriptions class="!w-full" border label-width="80">
<el-descriptions-item :label="$t('device.id')">
{{ device.id }}
</el-descriptions-item>
Expand Down
53 changes: 53 additions & 0 deletions src/pages/device/components/RemoveAction/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<template>
<EleTooltipButton
type="danger"
text
:loading
:icon="loading ? '' : 'Delete'"
placement="top"
:content="$t('common.remove')"
@click="handleClick(device)"
>
</EleTooltipButton>
</template>

<script>
import { sleep } from '$/utils'
export default {
props: {
device: {
type: Object,
default: () => ({}),
},
handleRefresh: {
type: Function,
default: () => {},
},
},
data() {
return {
loading: false,
}
},
methods: {
async handleClick(device = this.device) {
this.loading = true
const devices = { ...window.appStore.get('device') }
delete devices[device.id]
window.appStore.set('device', devices)
this.handleRefresh()
await sleep()
this.loading = false
},
},
}
</script>

<style></style>
13 changes: 11 additions & 2 deletions src/pages/device/components/WirelessGroup/QrAction/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class="flex-none !border-none"
@click="handleClick"
>
{{ loading ? $t('common.connecting') : $t('device.wireless.connect.qr') }}
{{ loading ? loadingText : $t('device.wireless.connect.qr') }}
</el-button>
</template>

Expand All @@ -24,6 +24,7 @@

<script setup>
import { generateAdbPairingQR } from '$/utils/device/generateAdbPairingQR/index.js'
import { computed } from 'vue'
const props = defineProps({
handleRefresh: {
Expand All @@ -36,14 +37,22 @@ const dataUrl = ref('')
const loading = ref(false)
const loadingText = ref('')
function onStatus(type) {
loadingText.value = window.t(`device.wireless.connect.qr.${type}`)
}
async function handleClick() {
const data = await generateAdbPairingQR()
dataUrl.value = data.dataUrl
loading.value = true
try {
await window.adb.connectCode(data.password)
await window.adb.connectCode(data.password, {
onStatus,
})
}
catch (error) {
console.warn(error.message)
Expand Down
30 changes: 15 additions & 15 deletions src/pages/device/components/WirelessGroup/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,25 @@ export default {
},
},
},
watch: {
wirelessList: {
handler(val) {
const lastIndex = val.length - 1
const lastWireless = val[lastIndex] || {}
async created() {
if (this.wirelessList?.length) {
const lastIndex = this.wirelessList.length - 1
const lastWireless = this.wirelessList[lastIndex]
if (lastWireless) {
this.formData = {
host: lastWireless.host,
port: lastWireless.port || 5555,
port: lastWireless.port,
id: `${lastWireless.host}:${lastWireless.port}`,
}
},
immediate: true,
},
},
async created() {
const autoConnect = this.$store.preference.data.autoConnect
if (autoConnect) {
await this.handleBatch()
this.handleRefresh()
}
const autoConnect = this.$store.preference.data.autoConnect
if (autoConnect) {
await this.handleBatch()
this.handleRefresh()
}
}
},
methods: {
Expand Down
Loading

0 comments on commit 10b902a

Please sign in to comment.