我之前沒有應用程序開發經驗,現在我應該在android studio中創建一個應用程序,該應用程序通過WiFi-direct連接到傳感器。我已經閱讀了WiFi-direct,並試圖遵循指南https://developer.android.com/guide/topics/connectivity/wifip2p.html和https://stuff.mit.edu/afs/sipb/project/android/docs/training/connect-devices-wirelessly/wifi-direct.html,但是當使用他們的代碼時,我仍然不知道如何前進。我也看了幾個演示應用程序的WiFi直接。如何創建一個可直接啓動WiFi的按鈕
我的代碼目前看起來像這樣。
廣播reciever:
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)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
mActivity.setIsWifiP2pEnabled(true);
} else {
mActivity.setIsWifiP2pEnabled(false);
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
private boolean isWifiP2pEnabled = false;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
}
/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
super.onResume();
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
}
指南中接下來的部分是我應該嘗試發現同伴。我應該在哪裏實現這個代碼?
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}
@Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
}
});
我的最終產品應該是一個按鈕,當我按它時,我應該搜索同行並能夠連接到一個。傳感器將直接通過WiFi啓動。有誰知道我在哪裏可以找到更多關於如何進行的信息或有關我需要哪些類的提示?
謝謝!
謝謝!我是否應該在OnRecieve方法中請求對等列表,或者創建一個像「public void DiscoverPeers()」之類的單獨方法? – user7772159
是的,在「OnReceive」裏面的註釋裏說requestPeers - > else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){ //調用WifiP2pManager.requestPeers()得到當前對等列表 } – JAD