有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android 10设备未开始发现wifi direct

我目前正在开发自己的应用程序,可以通过WiFi将信息直接发送到连接的其他设备。在安卓9和更低版本上,设备的发现已经成功启动,但在安卓10上却没有。 这是我的主要活动。java文件:

package com.shuzo.musicshare;

import 安卓.Manifest;
import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.IntentFilter;
import 安卓.content.pm.PackageManager;
import 安卓.net.wifi.WifiManager;
import 安卓.net.wifi.p2p.WifiP2pConfig;
import 安卓.net.wifi.p2p.WifiP2pDevice;
import 安卓.net.wifi.p2p.WifiP2pDeviceList;
import 安卓.net.wifi.p2p.WifiP2pInfo;
import 安卓.net.wifi.p2p.WifiP2pManager;
import 安卓.os.Bundle;
import 安卓.os.Handler;
import 安卓.os.Message;
import 安卓.os.StrictMode;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.CompoundButton;
import 安卓.widget.EditText;
import 安卓.widget.ListView;
import 安卓.widget.Switch;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓x.core.app.ActivityCompat;
import 安卓x.core.content.ContextCompat;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button btnDiscover, btnSend;
    Switch switchOnOff;
    ListView listView;
    TextView readMsgBox, connectionStatus;
    EditText writeMsg;
    WifiManager wifiManager;
    WifiP2pManager mManager;
    WifiP2pManager.Channel mChannel;
    BroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;
    List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
    String[] deviceNameArray;
    WifiP2pDevice[] deviceArray;
    static final int MESSAGE_READ = 1;
    ServerClass serverClass;
    List<ClientClass> clientClass = new ArrayList<>();
    SendReceive sendReceive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        initialWork();
        exqListener();
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_READ:
                    byte[] readBuff = (byte[]) msg.obj;
                    String tempMsg = new String(readBuff, 0, msg.arg1);
                    readMsgBox.setText(tempMsg);
                    break;
            }
            return true;
        }
    });

    private void exqListener() {
        switchOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                wifiManager.setWifiEnabled(b);
            }
        });

        btnDiscover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
                            ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.ACCESS_COARSE_LOCATION },1);
                        }
                        connectionStatus.setText("Discovery started");
                    }

                    @Override
                    public void onFailure(int i) {
                        connectionStatus.setText("Discovery starting failed");
                    }
                });
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final WifiP2pDevice device = deviceArray[i];
                WifiP2pConfig config = new WifiP2pConfig();
                config.deviceAddress = device.deviceAddress;

                mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Toast.makeText(getApplicationContext(), "Connected to " + device.deviceName, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(int i) {
                        Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msg=writeMsg.getText().toString();
                sendReceive.write(msg.getBytes());
            }
        });
    }

    private void initialWork() {
        btnDiscover = findViewById(R.id.discover);
        btnSend = findViewById(R.id.sendButton);
        switchOnOff = findViewById(R.id.switchOnOff);
        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        switchOnOff.setChecked(wifiManager.isWifiEnabled());
        listView = findViewById(R.id.peerListView);
        readMsgBox = findViewById(R.id.readMsg);
        connectionStatus = findViewById(R.id.connectionStatus);
        writeMsg = findViewById(R.id.writeMsg);
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    }

    WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList peerList) {
            if (!peerList.getDeviceList().equals(peers)) {
                peers.clear();
                peers.addAll(peerList.getDeviceList());

                deviceNameArray = new String[peerList.getDeviceList().size()];
                deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];
                int index = 0;

                for (WifiP2pDevice device : peerList.getDeviceList()) {
                    deviceNameArray[index] = device.deviceName;
                    deviceArray[index] = device;
                    index++;
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), 安卓.R.layout.simple_list_item_1, deviceNameArray);
                listView.setAdapter(adapter);
            }

            if (peers.size() == 0) {
                Toast.makeText(getApplicationContext(), "No devices found", Toast.LENGTH_SHORT).show();
            }
        }
    };

    WifiP2pManager.ConnectionInfoListener connectionInfoListener = new WifiP2pManager.ConnectionInfoListener() {
        @Override
        public void onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo) {
            final InetAddress groupOwnerAddress = wifiP2pInfo.groupOwnerAddress;

            if (wifiP2pInfo.groupFormed && wifiP2pInfo.isGroupOwner) {
                connectionStatus.setText("Host");
                serverClass = new ServerClass();
                serverClass.start();
            } else if (wifiP2pInfo.groupFormed) {
                connectionStatus.setText("Client");
                ClientClass client = new ClientClass(groupOwnerAddress);
                clientClass.add(client);
                client.start();
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    private class SendReceive extends Thread {
        private Socket socket;
        private InputStream inputStream;
        private OutputStream outputStream;

        public SendReceive(Socket skt) {
            socket = skt;
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            byte[] buffer = new byte[1024];
            int bytes;

            while (socket != null) {
                try {
                    bytes = inputStream.read(buffer);
                    if (bytes > 0) {
                        handler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void write(byte[] bytes) {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class ServerClass extends Thread {
        Socket socket;
        ServerSocket serverSocket;

        @Override
        public void run() {
            try {
                serverSocket = new ServerSocket(8888);
                socket = serverSocket.accept();
                sendReceive = new SendReceive(socket);
                sendReceive.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class ClientClass extends Thread {
        Socket socket;
        String hostAdd;

        public ClientClass (InetAddress hostAddress) {
            hostAdd = hostAddress.getHostAddress();
            socket = new Socket();
        }

        @Override
        public void run() {
            try {
                socket.connect(new InetSocketAddress(hostAdd, 8888), 500);
                sendReceive = new SendReceive(socket);
                sendReceive.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这是我的WiFiDirectBroadcastReceiver类:

package com.shuzo.musicshare;

import 安卓.content.BroadcastReceiver;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.net.NetworkInfo;
import 安卓.net.wifi.p2p.WifiP2pManager;
import 安卓.widget.Toast;

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private MainActivity mActivity;

    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
                                       MainActivity activity) {
        super();
        this.mManager = manager;
        this.mChannel = channel;
        this.mActivity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);

            if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                Toast.makeText(context, "WiFi is enabled", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "WiFi is disabled", Toast.LENGTH_SHORT).show();
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

            if (mManager != null) {
                mManager.requestPeers(mChannel, mActivity.peerListListener);
            }
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            if (mManager == null) {
                return;
            }

            NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

            if (networkInfo.isConnected()) {
                mManager.requestConnectionInfo(mChannel, mActivity.connectionInfoListener);
            } else {
                mActivity.connectionStatus.setText("Device disconnected");
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
        }

    }
}

我还请求清单文件中所需的所有权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.shuzo.musicshare">

    <uses-permission 安卓:name="安卓.permission.ACCESS_WIFI_STATE" />
    <uses-permission 安卓:name="安卓.permission.CHANGE_WIFI_STATE" />
    <uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission 安卓:name="安卓.permission.CHANGE_NETWORK_STATE" />
    <uses-permission 安卓:name="安卓.permission.INTERNET" />
    <uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE" />

    <application
        安卓:allowBackup="true"
        安卓:icon="@mipmap/ic_launcher"
        安卓:label="@string/app_name"
        安卓:roundIcon="@mipmap/ic_launcher_round"
        安卓:supportsRtl="true"
        安卓:theme="@style/AppTheme">
        <activity 安卓:name=".MainActivity">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的活动是主要的。xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/ConstraintLayout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        安卓:id="@+id/discover"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignBaseline="@id/connectionStatus"
        安卓:layout_alignBottom="@id/connectionStatus"
        安卓:layout_centerHorizontal="true"
        安卓:layout_marginStart="16dp"
        安卓:layout_marginTop="16dp"
        安卓:text="discover"
        app:layout_constraintStart_toEndOf="@+id/switchOnOff"
        app:layout_constraintTop_toBottomOf="@+id/connectionStatus" />

    <ListView
        安卓:id="@+id/peerListView"
        安卓:layout_width="match_parent"
        安卓:layout_height="200dp"
        安卓:layout_alignParentStart="true"
        安卓:layout_marginTop="16dp"
        安卓:background="@安卓:color/holo_orange_light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/discover" />

    <TextView
        安卓:id="@+id/readMsg"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_below="@+id/peerListView"
        安卓:layout_alignParentStart="true"
        安卓:layout_alignParentEnd="true"
        安卓:layout_marginTop="16dp"
        安卓:text="Message"
        安卓:textAlignment="center"
        安卓:textSize="20sp"
        安卓:textStyle="italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/peerListView" />

    <EditText
        安卓:id="@+id/writeMsg"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentStart="true"
        安卓:layout_alignParentBottom="true"
        安卓:layout_toStartOf="@+id/sendButton"
        安卓:ems="10"
        安卓:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        安卓:id="@+id/sendButton"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentEnd="true"
        安卓:layout_alignParentBottom="true"
        安卓:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/writeMsg" />

    <TextView
        安卓:id="@+id/connectionStatus"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentStart="true"
        安卓:layout_alignParentTop="true"
        安卓:layout_marginTop="16dp"
        安卓:text="Connection Status"
        安卓:textAlignment="center"
        安卓:textColor="@安卓:color/holo_blue_dark"
        安卓:textSize="18sp"
        安卓:textStyle="italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Switch
        安卓:id="@+id/switchOnOff"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="16dp"
        安卓:layout_marginTop="24dp"
        安卓:text="WiFi"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/connectionStatus" />
</安卓x.constraintlayout.widget.ConstraintLayout>

当我按下Android 9上的“发现”按钮时,活动顶部的文本视图显示“发现已启动”,如果我使用运行Android 8的第二台设备执行此操作,我可以通过ListView连接这两台设备。如果我想在Android 10设备上执行此操作,TextView会显示“发现启动失败”。 手机上的活动如下所示:

Activity on the phone

有人知道如何修复Android 10上的这个发现失败吗

编辑:Android 10上的测试设备是三星Galaxy A40


共 (2) 个答案

  1. # 1 楼答案

    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "please enable location services", Toast.LENGTH_LONG).show();
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    

    使用此代码这将有所帮助。我已经用上面的代码解决了安卓10上的问题。请记住,wifi direct在没有Gps定位的情况下将不起作用

  2. # 2 楼答案

    根据documentation,您需要权限android.permission.ACCESS_FINE_LOCATION,而不是android.permission.ACCESS_COARSE_LOCATION