繁体   English   中英

有没有办法通过 WiFi 以编程方式检测 ADB 连接?

[英]Is there a way to programmatically detect an ADB connection over WiFi?

我正在开发一个应用程序,它自动将客户端设备连接到计算机上的 adb 服务器(通过 wifi)。 我一直在检测它是否通过 wifi 成功连接。 是否有 java 侦听器方法可以无线检测到设备的连接?

主要活动.java

    public class MainActivity extends AppCompatActivity {

    private Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Check for Wireless connection


        // Check for USB connection
        thread = new Thread() {
            @Override
            public void run() {
                try {
                    while (!thread.isInterrupted()) {
                        Thread.sleep(100);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(usbDebuggingConnected()){
                                    WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                                    assert manager != null;
                                    WifiInfo info = manager.getConnectionInfo();
                                    String address = info.getMacAddress();
                                    TextView t = findViewById(R.id.usb_connection);
                                    t.setText("Connected with the address - " + address);
                                    t.setTextColor(getResources().getColor(R.color.green));
                                } else {
                                    TextView t = findViewById(R.id.usb_connection);
                                    t.setText("USB Debugging Disconnected");
                                    t.setTextColor(getResources().getColor(R.color.red));
                                }
                            }
                        });
                    }
                } catch (InterruptedException ignored) {
                }
            }
        };
        thread.start();
    }

    public boolean usbDebuggingConnected() {
        Intent intent = registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
        assert intent != null;
        return Objects.requireNonNull(intent.getExtras()).getBoolean("connected");
    }

活动_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    style="@style/Theme.AppCompat.Transparent">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.06">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Wireless Connection Status:"
            android:textAlignment="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/wireless_connection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Disconnected"
            android:textAlignment="center"
            android:textColor="#D50000"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="USB Connection Status:"
            android:textAlignment="center"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/usb_connection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Disconnected"
            android:textAlignment="center"
            android:textColor="#D50000"
            android:textSize="18sp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

提前致谢!

我找到了解决方案。 基本上我使应用程序具有可选择端口的 TCP 客户端功能。 我认为您需要集成 ASyncTask 才能使其工作。 我知道它需要完成 3 次握手,所以可能是广播、接收、确认之类的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM