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

标签 java android android-studio

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

主要 Activity .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");
    }

activity_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 次握手,所以可能需要广播、接收、确认之类的东西?

关于java - 有没有办法通过 WiFi 以编程方式检测 ADB 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59122301/

相关文章:

android - Android Studio 模拟器没有声音

visual-studio - Android Studio中如何使用Visual Studio快捷键?

java - 如何用最短的代码 Java 中的 console.next() 接受 1 或 2 个字符串变量?

Java REST API 查询注释

android 使布局在软键盘打开时可滚动,但不向上移动

android - IntelliJ 主项目 View 中的 "External Libraries"条目是什么意思?

java - 正则表达式匹配数字后跟空格

java - 错误: Expecting ")" before end of parameters

Android - 如何在单击按钮时删除自定义 ListView 项?

Android Studio依赖冲突(程序类型已经存在)