Skip to content

Instantly share code, notes, and snippets.

@qgustavor
Created May 30, 2023 13:24
Show Gist options
  • Save qgustavor/3a7798606fb8109c22609780115b71ac to your computer and use it in GitHub Desktop.
Save qgustavor/3a7798606fb8109c22609780115b71ac to your computer and use it in GitHub Desktop.
import * as dnssd from 'npm:ya-dns-sd'
const timeout = Number(Deno.args[0]) || 30
// Check if it's needed and prompt
const connectedDevices = new TextDecoder().decode(await Deno.run({
cmd: ['adb', 'devices'],
stdout: 'piped'
}).output()).split(/\r?\n/g).filter(e => e).slice(1).map(e => e.split('\t')[0])
if (connectedDevices.length > 0) {
console.log('Already connected to ' + connectedDevices.join(' '))
console.log('Press y to disconnect and start scanning')
if (!confirm()) Deno.exit()
await Deno.run({
cmd: ['adb', 'disconnect']
}).status()
}
const controller = new AbortController()
const timer = setTimeout(() => {
console.error(`Nothing found after ${timeout} seconds`)
controller.abort()
Deno.exit(1)
}, timeout * 1000)
for await (
const service of dnssd.browse({
multicastInterface: new dnssd.MulticastInterface(),
service: {
protocol: 'tcp',
type: 'adb-tls-connect',
},
signal: controller.signal
})
) {
if (!service.isActive) continue
const ipPortPair = service.host + ':' + service.port
console.log('Connecting to', ipPortPair)
await Deno.run({
cmd: ['adb', 'connect', ipPortPair]
}).status()
clearTimeout(timer)
Deno.exit(0)
}
@qgustavor
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment