aardio 文档

aardio 范例: 通过 adb 实现安卓手机投屏

//aardio 通过 adb 实现安卓手机投屏
import fonts.fontAwesome;
import win.ui.simpleWindow;
import process.adb;
import process.adb.clipboard;
import wsock.udp.mdnsClient;
import process.adb.recorder;
import win.ui;
/*DSG{{*/
var winform = win.form(text="安卓投屏与自动化助手";right=920;bottom=620;bgcolor=0xFFFFFF;border="none")
winform.add(
bkActionsTitle={cls="bkplus";text='\uF0AD  快捷操作';left=15;top=250;right=265;bottom=275;align="left";color=0x333333;dl=1;dt=1;font=LOGFONT(h=-14;name='FontAwesome';weight=600);z=15};
bkCodeTitle={cls="bkplus";text='\uF121  自动化代码';left=295;top=60;right=500;bottom=85;align="left";color=0x333333;dl=1;dr=1;dt=1;font=LOGFONT(h=-14;name='FontAwesome';weight=600);z=26};
bkDevicePanel={cls="bkplus";left=0;top=46;right=280;bottom=621;bgcolor=0xF5F5F5;db=1;dl=1;dt=1;z=1};
bkDeviceTitle={cls="bkplus";text='\uF0C9  设备信息';left=15;top=60;right=265;bottom=85;align="left";color=0x333333;dl=1;dt=1;font=LOGFONT(h=-14;name='FontAwesome';weight=600);z=5};
bkHeader={cls="bkplus";left=0;top=0;right=921;bottom=46;bgcolor=0x0078D4;dl=1;dr=1;dt=1;z=2};
bkIpLabel={cls="bkplus";text="IP 地址:";left=15;top=210;right=90;bottom=230;align="left";color=0x666666;dl=1;dt=1;z=13};
bkModelLabel={cls="bkplus";text="设备型号:";left=15;top=135;right=90;bottom=155;align="left";color=0x666666;dl=1;dt=1;z=7};
bkRecordTitle={cls="bkplus";text='\uF111  录制手机操作';left=15;top=425;right=265;bottom=450;align="left";color=0x333333;dl=1;dt=1;font=LOGFONT(h=-14;name='FontAwesome';weight=600);z=23};
bkScreenLabel={cls="bkplus";text="屏幕分辨率:";left=15;top=185;right=90;bottom=205;align="left";color=0x666666;dl=1;dt=1;z=11};
bkTitle={cls="bkplus";text="安卓投屏与自动化助手(A_A)";left=45;top=10;right=467;bottom=36;align="left";color=0xFFFFFF;dl=1;dt=1;font=LOGFONT(h=-18;weight=600);z=3};
bkTitleIcon={cls="bkplus";text='\uF10B';left=12;top=8;right=42;bottom=40;color=0xFFFFFF;dl=1;dt=1;font=LOGFONT(h=-24;name='FontAwesome');z=4};
bkVersionLabel={cls="bkplus";text="安卓版本:";left=15;top=160;right=90;bottom=180;align="left";color=0x666666;dl=1;dt=1;z=9};
chkChareClipboard={cls="plus";text="共享剪贴板";left=116;top=585;right=224;bottom=616;align="left";color=0x333333;db=1;dl=1;font=LOGFONT(h=-14);iconStyle={align="left";font=LOGFONT(h=-15;name='FontAwesome')};iconText='\uF0C8 ';notify=1;textPadding={left=24};z=37};
chkTurnScreenOff={cls="plus";text="关闭屏幕";left=15;top=585;right=118;bottom=616;align="left";color=0x333333;db=1;dl=1;font=LOGFONT(h=-15);iconStyle={align="left";font=LOGFONT(h=-15;name='FontAwesome')};iconText='\uF0C8 ';notify=1;textPadding={left=24};z=40};
comboShareFileTarget={cls="combobox";left=508;top=592;right=649;bottom=618;db=1;dl=1;edge=1;items={"手机","手机 » 微信","手机 » 抖音","手机 »  QQ","手机 » 钉钉"};mode="dropdown";z=38};
editCode={cls="edit";left=295;top=95;right=900;bottom=581;db=1;dl=1;dr=1;dt=1;edge=1;font=LOGFONT(h=-13;name='Consolas');hscroll=1;multiline=1;vscroll=1;z=29};
lbIpValue={cls="plus";text="--";left=95;top=210;right=265;bottom=230;align="left";border={bottom=1;color=0xC5C5C5};color=0x333333;dl=1;dt=1;z=14};
lbModelValue={cls="plus";text="--";left=95;top=135;right=265;bottom=155;align="left";border={bottom=1;color=0xC5C5C5};color=0x333333;dl=1;dt=1;z=8};
lbScreenValue={cls="plus";text="--";left=95;top=185;right=265;bottom=205;align="left";border={bottom=1;color=0xC5C5C5};color=0x333333;dl=1;dt=1;z=12};
lbShareFile={cls="plus";text="拖放文件到本窗口发送到";left=293;top=588;right=494;bottom=616;align="right";db=1;dl=1;iconStyle={align="right";font=LOGFONT(name='FontAwesome')};iconText='\uF10B';textPadding={right=14};z=39};
lbVersionValue={cls="plus";text="--";left=95;top=160;right=265;bottom=180;align="left";border={bottom=1;color=0xC5C5C5};color=0x333333;dl=1;dt=1;z=10};
lnkImTip={cls="syslink";text='<a href="http://imtip.aardio.com">超级热键工具: ImTip</a>';left=17;top=512;right=146;bottom=529;dl=1;dt=1;z=36};
plusBack={cls="plus";text="返回";left=100;top=375;right=175;bottom=405;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='\uF060';notify=1;textPadding={left=24};z=21};
plusClear={cls="plus";text="清空";left=735;top=58;right=795;bottom=83;color=0x666666;dr=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome');padding={left=5}};iconText='\uF1F8';notify=1;textPadding={left=20};z=27};
plusConnect={cls="plus";text="连接手机";left=15;top=285;right=135;bottom=320;align="left";bgcolor=0x0078D4;color=0xFFFFFF;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF0C1';notify=1;textPadding={left=36;bottom=1};z=16};
plusDisconnect={cls="plus";text="断开连接";left=145;top=285;right=265;bottom=320;align="left";bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF127';notify=1;textPadding={left=36;bottom=1};z=17};
plusHome={cls="plus";text="主页";left=15;top=375;right=90;bottom=405;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='\uF015';notify=1;textPadding={left=24};z=20};
plusMenu={cls="plus";text="菜单";left=185;top=375;right=265;bottom=405;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(h=-13;name='FontAwesome');padding={left=8}};iconText='\uF0C9';notify=1;textPadding={left=24};z=22};
plusPower={cls="plus";text='\uF011';left=15;top=543;right=55;bottom=573;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=33};
plusRecent={cls="plus";text='\uF24D';left=60;top=543;right=100;bottom=573;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=34};
plusRotate={cls="plus";text='\uF021';left=105;top=543;right=145;bottom=573;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=35};
plusRunCode={cls="plus";text="运行";left=805;top=58;right=900;bottom=83;align="left";bgcolor=0x28A745;color=0xFFFFFF;dr=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome');padding={left=13;top=1}};iconText='\uF04B';notify=1;textPadding={left=36;bottom=1};z=28};
plusSave={cls="plus";text="保存";left=665;top=58;right=725;bottom=83;color=0x666666;dr=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome');padding={left=5}};iconText='\uF0C7';notify=1;textPadding={left=20};z=30};
plusScreen={cls="plus";text="手机投屏";left=15;top=330;right=135;bottom=365;align="left";bgcolor=0x28A745;color=0xFFFFFF;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF108';notify=1;textPadding={left=36;bottom=1};z=18};
plusScreenshot={cls="plus";text="截图";left=145;top=330;right=265;bottom=365;align="left";bgcolor=0xFFC107;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF030';notify=1;textPadding={left=36;bottom=1};z=19};
plusStartRecord={cls="plus";text="开始录制";left=15;top=460;right=135;bottom=495;align="left";bgcolor=0xDC3545;color=0xFFFFFF;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF111';notify=1;textPadding={left=36;bottom=1};z=24};
plusStatus={cls="plus";text="未连接";left=15;top=95;right=265;bottom=125;align="left";color=0xFF6B6B;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-16;name='FontAwesome');padding={left=8}};iconText='\uF057';textPadding={left=29;bottom=2};z=6};
plusStopRecord={cls="plus";text="停止录制";left=145;top=460;right=265;bottom=495;align="left";bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-13);iconStyle={align="left";font=LOGFONT(h=-14;name='FontAwesome');padding={left=13;top=1}};iconText='\uF04D';notify=1;textPadding={left=36;bottom=1};z=25};
plusVolDown={cls="plus";text='\uF027';left=180;top=543;right=220;bottom=573;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=31};
plusVolUp={cls="plus";text='\uF028';left=225;top=543;right=265;bottom=573;bgcolor=0xE0E0E0;color=0x333333;dl=1;dt=1;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=32}
)
/*}}*/

// 应用按钮样式
var applyButtonStyles = function(){
    winform.plusConnect.skin({
        background={default=0xFF0078D4;hover=0xFF106EBE;active=0xFF005A9E;disabled=0xFFCCCCCC};
        color={default=0xFFFFFFFF}
    });

    winform.plusDisconnect.skin({
        background={default=0xFFE0E0E0;hover=0xFFD0D0D0;active=0xFFC0C0C0;disabled=0xFFE8E8E8};
        color={default=0xFF333333;disabled=0xFFAAAAAA}
    });

    winform.plusScreen.skin({
        background={default=0xFF28A745;hover=0xFF218838;active=0xFF1E7E34;disabled=0xFFCCCCCC};
        color={default=0xFFFFFFFF}
    });

    winform.plusScreenshot.skin({
        background={default=0xFFFFC107;hover=0xFFE0A800;active=0xFFD39E00;disabled=0xFFCCCCCC};
        color={default=0xFF333333}
    });

    var grayBtnSkin = {
        background={default=0xFFE0E0E0;hover=0xFFD0D0D0;active=0xFFC0C0C0;disabled=0xFFE8E8E8};
        color={default=0xFF333333;disabled=0xFFAAAAAA}
    };

    winform.plusHome.skin(grayBtnSkin);
    winform.plusBack.skin(grayBtnSkin);
    winform.plusMenu.skin(grayBtnSkin);
    winform.plusVolUp.skin(grayBtnSkin);
    winform.plusVolDown.skin(grayBtnSkin);
    winform.plusPower.skin(grayBtnSkin);
    winform.plusRecent.skin(grayBtnSkin);
    winform.plusRotate.skin(grayBtnSkin);
    winform.plusStopRecord.skin(grayBtnSkin);

    winform.plusStartRecord.skin({
        background={default=0xFFDC3545;hover=0xFFC82333;active=0xFFBD2130;disabled=0xFFCCCCCC};
        iconColor={default=0xFFFFFFFF;disabled=0xE0DC3545};
        color={default=0xFFFFFFFF}
    });

    winform.plusClear.skin({
        color={default=0xFF666666;hover=0xFFDC3545;active=0xFFC82333}
    });

    winform.plusSave.skin({
        color={default=0xFF666666;hover=0xFF0078D4;active=0xFF005A9E}
    });

    winform.plusRunCode.skin({
        background={default=0xFF28A745;hover=0xFF218838;active=0xFF1E7E34};
        color={default=0xFFFFFFFF}
    });

    winform.chkChareClipboard.skin({
        color={
            active=0xFF00FF00;
            default=0xFF333333;
            disabled=0xEE666666;
            hover=0xFFFF0000        
        };
        checked={
            iconText='\uF14A'       
        }
    })

    winform.chkTurnScreenOff.skin({
        color={
            active=0xFF00FF00;
            default=0xFF333333;
            disabled=0xEE666666;
            hover=0xFFFF0000        
        };
        checked={
            iconText='\uF14A'       
        }
    })

};
applyButtonStyles();

// 添加无边框窗口支持
win.ui.simpleWindow(winform);

// 状态变量
var isConnected = false;
var recordProcess = null;
var maxPosX, maxPosY;
var cxScreen, cyScreen;

// 更新连接状态
var updateStatus = function(connected){
    isConnected = connected;

    var btns = {
        winform.plusDisconnect;
        winform.plusScreen;
        winform.plusScreenshot;
        winform.plusHome;
        winform.plusBack;
        winform.plusMenu;
        winform.plusVolUp;
        winform.plusVolDown;
        winform.plusPower;
        winform.plusRecent;
        winform.plusRotate;
        winform.plusStartRecord;
    };

    if(connected){
        winform.plusStatus.iconText = '\uF058';
        winform.plusStatus.iconColor = 0xFF28A745;
        winform.plusStatus.argbColor = 0xFF28A745;
        winform.plusStatus.text = "已连接";

        for i, btn in btns {
            btn.disabled = false;
        }

        if(winform.chkChareClipboard.checked) {
            import process.adb.clipboard;
            process.adb.clipboard.sync(true)
        }
    } else {
        winform.plusStatus.iconText = '\uF057';
        winform.plusStatus.iconColor = 0xFFFF6B6B;
        winform.plusStatus.argbColor = 0xFFFF6B6B;
        winform.plusStatus.text = "未连接";

        winform.lbModelValue.text = "--";
        winform.lbVersionValue.text = "--";
        winform.lbScreenValue.text = "--";
        winform.lbIpValue.text = "--";

        for i, btn in btns {
            btn.disabled = true;
        }
    }
};

// 获取设备信息
var refreshDeviceInfo = function(){
    if(!process.adb.isConnected()) return;

    var model = process.adb.getProp("ro.product.model");
    if(model) winform.lbModelValue.text = model;

    var version = process.adb.version(true);
    if(version) winform.lbVersionValue.text = "Android " + version;

    cxScreen, cyScreen = process.adb.getScreen();
    if(cxScreen && cyScreen){
        winform.lbScreenValue.text = cxScreen + " × " + cyScreen;
    }

    var ip = process.adb.ip();
    if(ip) winform.lbIpValue.text = ip;

    maxPosX, maxPosY = process.adb.getMaxPos();
    return #model || #version || #ip;
};

winform.chkTurnScreenOff.oncommand = function(id,event){
    if(winform.scrcpy && winform.scrcpy.stillActive()){
        if(isConnected){
            if(winform.chkTurnScreenOff.checked){
                winform.scrcpy.modKey("O")
            }
            else{
                // scrcpy 这样来回点多次会导致错乱,手动按一下手机电源键修复
                winform.scrcpy.modKey("SHIFT","O")
            }
        }
    }
    else{
        if(isConnected){
            if(winform.chkTurnScreenOff.checked){
                if(process.adb.isAwake()){
                    process.adb.keyPower();
                }  
            }
            else{
                if(!process.adb.isAwake()){
                    process.adb.keyPower();
                }  
            }

        }
    }

}

// 连接设备
winform.plusConnect.oncommand = function(){
    owner.disabledText = {'\uF110'};

    import process.adb.connecter;
    if(process.adb.connecter(winform,true)){
        updateStatus(true);
        refreshDeviceInfo();
        winform.editCode.print("// 设备连接成功: " + (winform.lbModelValue.text : "未知设备"));
        winform.chkTurnScreenOff.oncommand();
    } else {
        updateStatus(false);
    }

    owner.disabledText = null;
};

// 断开连接
winform.plusDisconnect.oncommand = function(){
    process.adb.disconnect();
    updateStatus(false);
    winform.editCode.print("// 已断开设备连接");
};

// 投屏
winform.plusScreen.oncommand = function(){
    if(!isConnected){
        winform.msgboxErr("请先连接设备");
        return;
    }

    winform.plusScreen.disabledText = ['\uF254','\uF251','\uF252','\uF253','\uF250']

    import process.scrcpy;
    winform.scrcpy = process.scrcpy( {
        powerOffOnClose = true;
        serial = process.adb.getSerialNo();
        turnScreenOff = winform.chkTurnScreenOff.checked?true:null;
        "--always-on-top";
        "--stay-awake";
        "--window-title": "投屏 - " + (winform.lbModelValue.text : "设备");
    });

    var prcs, err = winform.scrcpy.open();
    if(prcs){
        winform.editCode.print("// 投屏已启动");
        prcs.close();
    } else {
        winform.editCode.print("// 投屏启动失败: " + (err : "未知错误"));
        winform.scrcpy = null;
    }       

    winform.plusScreen.disabledText = null; 

};

// 截图
winform.plusScreenshot.oncommand = function(){
    if(!isConnected) return;

    owner.disabledText = {'\uF110'};

    import fsys.dlg;
    var path = fsys.dlg.save("*.png|*.png", "screenshot" + tostring(time(), "_%Y%m%d_%H%M%S"));
    if(path){
        var ok, err = process.adb.screenCap(path);
        if(ok){
            winform.editCode.print('// 截图已保存: ' + path);
            raw.execute(path);
        } else {
            winform.editCode.print('// 截图失败: ' + (err : "未知错误"));
        }
    }

    owner.disabledText = null;
};

// 快捷按键
winform.plusHome.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyHome();
    winform.editCode.print('process.adb.keyHome()');
};

winform.plusBack.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyBack();
    winform.editCode.print('process.adb.keyBack()');
};

winform.plusMenu.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyMenu();
    winform.editCode.print('process.adb.keyMenu()');
};

winform.plusVolUp.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyVolumeUp();
    winform.editCode.print('process.adb.keyVolumeUp()');
};

winform.plusVolDown.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyVolumeDown();
    winform.editCode.print('process.adb.keyVolumeDown()');
};

winform.plusPower.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyPower();
    winform.editCode.print('process.adb.keyPower()');
};

winform.plusRecent.oncommand = function(){
    if(!isConnected) return;
    process.adb.keyAppSwitch();
    winform.editCode.print('process.adb.keyAppSwitch()');
};

winform.plusRotate.oncommand = function(){
    if(!isConnected) return;
    // 切换屏幕方向
    var orientation = process.adb.shellGet("settings get system accelerometer_rotation");
    if(string.trim(orientation) == "1"){
        process.adb.shellGet("settings put system accelerometer_rotation 0");
        winform.editCode.print('// 已锁定屏幕方向');
    } else {
        process.adb.shellGet("settings put system accelerometer_rotation 1");
        winform.editCode.print('// 已开启自动旋转');
    }
};

// 创建录制器
var recorder = process.adb.recorder();

// 用于计算延时的变量
var lastEventTime = 0;
var minDelayTime = 100;  // 小于此值的延时不记录

thread.invoke( 
    function(){
        import process.adb;
    }
)
recorder.on = function(event, ...){

    // 计算与上次事件的间隔
    var now = time.tick();
    if(lastEventTime > 0){
        var delay = now - lastEventTime;
        if(delay >= minDelayTime){
            winform.editCode.printf("thread.delay(%d)", delay);
        }
    }
    lastEventTime = now;

    // 处理事件
    select(event) {
        case "tap" {
            var x, y = ...;
            winform.editCode.printf("adb.tap(%d, %d)", x, y);
        }
        case "swipe" {
            var x1, y1, x2, y2, duration = ...;
            winform.editCode.printf("adb.swipe(%d, %d, %d, %d, %d)", 
                x1, y1, x2, y2, duration);
        }
        case "long-press-touch" {
            var x, y, duration = ...;
            // 用原地滑动模拟长按
            winform.editCode.printf("adb.swipe(%d, %d, %d, %d, %d)  // 长按", 
                x, y, x, y, duration);
        }
        case "key" {
            var keyName = ...;
            winform.editCode.printf("process.adb.%s()", keyName);
        }
        case "long-press-key" {
            var keyName, duration = ...;
            winform.editCode.printf("adb.%s(true)  // 长按 %dms", keyName, duration);
        }
    }
}

// 开始录制按钮
winform.plusStartRecord.oncommand = function(){

    if(!isConnected){
        winform.msgboxErr("请先连接设备");
        return;
    }

    if(recorder.recording) return;
    winform.plusRunCode.disabled = true;

    // 重置延时计时器
    lastEventTime = 0;

    winform.plusStartRecord.disabledText = ['\uF10C','\uF042','\uF111']

    var ok, err = recorder.start();
    if(!ok){
        winform.msgboxErr(err);
        return;
    }

    owner.disabled = true;
    winform.plusStopRecord.disabled = false;

    winform.editCode.print(""); 
    winform.editCode.print("// ========== 录制开始 ==========");
    winform.editCode.print("import process.adb;");
    winform.editCode.print("var adb = process.adb.shellInput();");
    winform.editCode.print("");
};

// 停止录制按钮
winform.plusStopRecord.oncommand = function(){
    if(!recorder.recording) return;

    winform.plusStartRecord.disabledText = null;
    winform.plusStopRecord.disabledText = ['\uF254','\uF251','\uF252','\uF253','\uF250',text='正在停止']

    winform.editCode.print('thread.delay(500);\r\n// 正在停止录制,请任意操作手机一次')

    recorder.stop();

    winform.plusStopRecord.disabledText = null;

    //原地更新最后一行文本(如果不是首行则忽略尾部空行)
    winform.editCode.updateLastLine('// ========== 录制结束 =========='); 

    winform.plusRunCode.disabled = false;
};

// 清空代码
winform.plusClear.oncommand = function(){
    winform.editCode.text = "";
};

// 保存代码
winform.plusSave.oncommand = function(){
    import fsys.dlg;
    var path = fsys.dlg.save("*.aardio|*.aardio", "automation" + tostring(time(), "_%Y%m%d_%H%M%S"));
    if(path){
        string.save(path, winform.editCode.text);
        winform.msgbox("代码已保存到:\n" + path, "保存成功");
    }
};

// 运行代码
winform.plusRunCode.oncommand = function(){
    if(!isConnected){
        winform.msgboxErr("请先连接设备");
        return;
    }

    var code = winform.editCode.text;
    if(!#string.trim(code)){
        winform.msgboxErr("代码为空");
        return;
    }


    var helpDoc = /*

--------------------------------------------------------
如果运行自动化代码未生效:
1. 请打开「手机设置 » 更多设置 » 开发者选项」
2. 启用「USB 调试」与「USB 调试(安全设置)」这 2 个选项。
3. 如果仍然不行可尝试重启手机。
4. 如果还是不行可尝试用 USB 数据线连接一次。
--------------------------------------------------------
*/

    winform.editCode.print(helpDoc); 
    winform.plusRunCode.disabledText = ['\uF254','\uF251','\uF252','\uF253','\uF250']

    var func, err = loadcode(code);
    if(func){
        winform.plusStartRecord.disabled = true;
        var ok, err = call(func);

        winform.editCode.text = string.replace(winform.editCode.text,
                "@"+helpDoc,""); 

        winform.plusStartRecord.disabled = false;

        if(!ok){
            winform.editCode.print("// 运行出错: " + err);
        } else {
            winform.editCode.print("// 运行完成");
        }
    } else {
        winform.editCode.print("// 代码错误: " + err);
    }

    winform.plusRunCode.disabledText = null;
};


import fsys.table;
var cfgTable = fsys.table(io.appData("/aardio/std/A_A/setting.table"));
winform.chkChareClipboard.checked = cfgTable.chkChareClipboard;
winform.chkTurnScreenOff.checked = cfgTable.chkTurnScreenOff;
winform.comboShareFileTarget.selIndex  = cfgTable.selShareFileTarget || 1;
winform.show();

// 初始状态
updateStatus(false);
winform.plusStopRecord.disabled = true;

// 检查是否已连接
if(process.adb.isConnected()){
    updateStatus(true);
    refreshDeviceInfo();
    winform.editCode.print("// 检测到已连接的设备");
    winform.editCode.print("// 设备: " + (winform.lbModelValue.text : "未知"));
}

var mdns = wsock.udp.mdnsClient(); 
mdns.onServiceResolved = function(service) {

    if( service.serviceType == "_adb-tls-connect._tcp.local"){
        if(!isConnected  && !process.adb.isConnected()){
            var result, err = ..process.adb.connect(service.ip + ":" + service.port);
            if(result){
                updateStatus(true);
                refreshDeviceInfo();
                winform.editCode.print("// 设备连接成功: " + (winform.lbModelValue.text : "未知设备"));

                winform.chkTurnScreenOff.oncommand();
            } 
            else{
                mdns.clearCache();
            } 
        }
    }
}

mdns.discover("_adb-tls-connect._tcp"); 

// 定时检测设备连接状态
winform.setInterval(
    function(){
        if(isConnected){
            // 轻量级检测:执行一个简单命令
            if(!process.adb.isConnected()){
                updateStatus(false);
                winform.editCode.print("// ⚠ 设备已断开连接");

                // 重新启动 mDNS 发现,等待自动重连
                mdns.discover("_adb-tls-connect._tcp"); 
            }
        }
    }, 3000  // 每3秒检查一次,可根据需要调整
);

// 窗口关闭时清理
winform.onDestroy = function(){ 
    cfgTable.chkChareClipboard = winform.chkChareClipboard.checked;
    cfgTable.chkTurnScreenOff = winform.chkTurnScreenOff.checked;
    cfgTable.selShareFileTarget = winform.comboShareFileTarget.selIndex;
    cfgTable.save();

    process.adb.clipboard.sync(false) 

    mdns.close();
    if(recorder){
        recorder.stop();
        recorder.close();
    }

    if(winform.keyHook){
        winform.keyHook.close();
    }
};

winform.chkChareClipboard.oncommand = function(id,event){

    if(!owner.checked){ 
        import process.adb.clipboard;
        process.adb.clipboard.sync(false) 
    }
    else{
        import process.adb.clipboard;
        process.adb.clipboard.sync(true)    
    }
}

winform.onDropFiles = function(files){
    var path = files[1]

    if(!isConnected){
        winform.msgboxErr("请先连接手机!");
    }

    if(process.adb.isLockScreen()){
        winform.msgboxErr("手机已锁屏,请先解锁!"); 
    }

    var selTarget = winform.comboShareFileTarget.selIndex;
    if(selTarget==2){
        selTarget=`com.tencent.mm/.ui.tools.ShareImgUI`
    }
    elseif(selTarget==3){
        selTarget="com.ss.android.ugc.aweme/.share.SystemShareActivity"
    }
    elseif(selTarget==4){
        selTarget="com.tencent.mobileqq/com.tencent.mobileqq.activity.JumpActivity"
    }
    elseif(selTarget==5){
        selTarget="com.alibaba.android.rimet"
    }
    else{
        selTarget=null;
    }

    winform.lbShareFile.disabledText = ['\uF254','\uF251','\uF252','\uF253','\uF250']
    thread.invoke( 
        function(winform,path,selTarget){
            import process.adb;
            var ok,err = process.adb.shareFile(path,selTarget);
            winform.lbShareFile.disabledText = null;

            if(!ok){
                winform.msgboxErr(err,"A_A - 分享文件到手机时遇到错误");
            }
        },winform,path,selTarget
    )   
}

win.loopMessage();
Markdown 格式