我写了一个Android应用程序,允许设备通过Wi-Fi P2P发现来发现附近的设备。我正在关注this doc。
该应用程序可在我的Samsung Tab E平板电脑上很好地工作-当该应用程序在平板电脑上运行时,我能够扫描并查找其他设备。但是当我在Samsung Galaxy S10 Plus上运行应用程序时,我什至无法激活对等发现过程。除非我手动进入Galaxy S10 Plus的设置并启用Wi-Fi Discovery Mode
,否则我的Samsung s10 Plus甚至不会出现在平板电脑的可用对等设备列表中。到那时,我的Tab E平板电脑可以“看到”我的Galaxy S10 Plus,但是一旦退出Galaxy s10 Plus上的设置屏幕,Wi-Fi发现功能就会关闭,并且我再也看不到它了一个潜在的p2p对等体。
有没有办法允许在三星s10 Plus上进行对等发现?没有它,我将无法测试我的应用程序,宁愿不必购买另一个Tab E来测试p2p连接。
以下是我的应用程序正在运行的代码以供参考:
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button btnOnOff, btnDiscover, btnSend;
ListView listView;
TextView read_msg_box, connectionStatus;
EditText writeMsg;
//For programmatically turning wifi on and off:
WifiManager wifiManager;
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter = new IntentFilter();
//For keeping track of available clients to connect to
List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
String[] deviceNameArray;
WifiP2pDevice[] deviceArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialWork();
//initializeButtonOnClickListeners();
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);
btnDiscover = (Button)findViewById(R.id.discover);
connectionStatus = (TextView) findViewById(R.id.connectionStatus);
// btnSend = (Button)findViewById(R.id.sendButton);
listView = (ListView)findViewById(R.id.peerListView);
// read_msg_box = (TextView) findViewById(R.id.readMsg);
// writeMsg = (EditText)findViewById(R.id.writeMsg);
wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
initializeOnClickListeners();
}
private void initializeOnClickListeners() {
//Set up functionality for when the discover button is clicked.
btnDiscover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "looking for a new peer", Toast.LENGTH_SHORT).show();
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
connectionStatus.setText("Discovery Started");
}
@Override
public void onFailure(int reason) {
connectionStatus.setText("Discovery Failed to start");
}
});
}
});
}
//Define the logic for when a new peer is discovered
WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
if(!peerList.getDeviceList().equals(peers)) {
//clear the peers
peers.clear();
//store device list
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<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
deviceNameArray);
listView.setAdapter(adapter);
}
if(peers.size() == 0) {
Toast.makeText(getApplicationContext(), "No Devices Found :(", Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
WifiP2pManager.PeerListListener myPeerListListener;
public WiFiDirectBroadcastReceiver(WifiP2pManager mManager, WifiP2pManager.Channel mChannel, MainActivity mActivity) {
this.mManager = mManager;
this.mChannel = mChannel;
this.mActivity = mActivity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("The following action was received: " + action);
if(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
//if wifi is enabled
if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Toast.makeText(context, "Wifi is ON", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Wifi is OFF", Toast.LENGTH_LONG).show();
}
} else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
Toast.makeText(context, "INSIDE THE P2P_PEERS_CHANGED_ACTION", Toast.LENGTH_LONG).show();
if(mManager != null) {
mManager.requestPeers(mChannel, mActivity.peerListListener);
Toast.makeText(context, "A new peer has been found!!!", Toast.LENGTH_LONG).show();
}
} else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
//do something
} else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
//do something
}
}
}
最佳答案
有时我不使用Android编写代码,但是我认为我的经验可能仍然有效,您可以尝试一下。
如果我错了,请指正我,因此,您开发了一个应用程序,使您的设备可以发现并相互连接,而无需访问手机的设置。对于S10,您已经安装并打开了该应用程序,但是除非您在设置中手动将其打开,否则无法通过“选项卡”找到它。
首先,请确保您的应用具有在使用应用时添加和请求的所有必需权限。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nsdchat"
...
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
只是为了确保您的应用程序具有该权限,请将权限添加到下面的代码中以获取相应的权限:
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CHANGE_WIFI_STATE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
}
...
其次,查看代码库版本。由于s10 +相当新,因此从Android 8.0开始,Android实际上增强了隐私和安全性。由于库的版本,您编写的代码可能已过时,并且某些API可能已弃用。