• 大小: 0.28M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-23
  • 语言: Java
  • 标签: Android  文件  网络  p2p  d  

资源简介

public static final String TAG = "wifidirectdemo";
private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;

/**
* @param isWifiP2pEnabled
*            the isWifiP2pEnabled to set
*/
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled)
{
this.isWifiP2pEnabled = isWifiP2pEnabled;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// add necessary intent values to be matched.

intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
intentFilter
.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
intentFilter
.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);
}

/** register the BroadcastReceiver with the intent values to be matched */
@Override
public void onResume()
{
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
}

@Override
public void onPause()
{
super.onPause();
unregisterReceiver(receiver);
}

/**
* Remove all peers and clear all fields. This is called on
* BroadcastReceiver receiving a state change event.
*/
public void resetData()
{
DeviceListFragment fragmentList = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
DeviceDetailFragment fragmentDetails = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
if (fragmentList != null)
{
fragmentList.clearPeers();
}
if (fragmentDetails != null)
{
fragmentDetails.resetViews();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_items, menu);
return true;
}

/*
* (non-Javadoc)

* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.atn_direct_enable:
if (manager != null && channel != null)
{

// Since this is the system wireless settings activity, it's
// not going to send us a result. We will be notified by
// WiFiDeviceBroadcastReceiver instead.

startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
} else
{
Log.e(TAG, "channel or manager is null");
}
return true;

case R.id.atn_direct_discover:
if (!isWifiP2pEnabled)
{
Toast.makeText(WiFiDirectActivity.this,
R.string.p2p_off_warning, Toast.LENGTH_SHORT).show();
return true;
}
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.onInitiateDiscovery();
manager.discoverPeers(channel, new WifiP2pManager.ActionListener()
{

@Override
public void onSuccess()
{
Toast.makeText(WiFiDirectActivity.this,
"Discovery Initiated", Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(int reasonCode)
{
Toast.makeText(WiFiDirectActivity.this,
"Discovery Failed : " reasonCode,
Toast.LENGTH_SHORT).show();
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public void showDetails(WifiP2pDevice device)
{
DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
fragment.showDetails(device);

}

@Override
public void connect(WifiP2pConfig config)
{
manager.connect(channel, config, new ActionListener()
{

@Override
public void onSuccess()
{
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
}

@Override
public void onFailure(int reason)
{
Toast.makeText(WiFiDirectActivity.this,
"Connect failed. Retry.", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void disconnect()
{
final DeviceDetailFragment fragment = (DeviceDetailFragment) getFragmentManager()
.findFragmentById(R.id.frag_detail);
fragment.resetViews();
manager.removeGroup(channel, new ActionListener()
{

@Override
public void onFailure(int reasonCode)
{
Log.d(TAG, "Disconnect failed. Reason :" reasonCode);

}

@Override
public void onSuccess()
{
fragment.getView().setVisibility(View.GONE);
}

});
}

@Override
public void onChannelDisconnected()
{
// we will try once more
if (manager != null && !retryChannel)
{
Toast.makeText(this, "Channel lost. Trying again",
Toast.LENGTH_LONG).show();
resetData();
retryChannel = true;
manager.initialize(this, getMainLooper(), this);
} else
{
Toast.makeText(
this,
"Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
Toast.LENGTH_LONG).show();
}
}

@Override
public void cancelDisconnect()
{

/*
* A cancel abort request by user. Disconnect i.e. removeGroup if
* already connected. Else, request WifiP2pManager to abort the ongoing
* request
*/
if (manager != null)
{
final DeviceListFragment fragment = (DeviceListFragment) getFragmentManager()
.findFragmentById(R.id.frag_list);
if (fragment.getDevice() == null
|| fragment.getDevice().status == WifiP2pDevice.CONNECTED)
{
disconnect();
} else if (fragment.getDevice().status == WifiP2pDevice.AVAILABLE
|| fragment.getDevice().status == WifiP2pDevice.INVITED)
{

manager.cancelConnect(channel, new ActionListener()
{

@Override
public void onSuccess()
{
Toast.makeText(WiFiDirectActivity.this,
"Aborting connection", Toast.LENGTH_SHORT)
.show();
}

@Override
public void onFailure(int reasonCode)
{
Toast.makeText(
WiFiDirectActivity.this,
"Connect abort request failed. Reason Code: "
reasonCode, Toast.LENGTH_SHORT)
.show();
}
});
}
}

}

资源截图

代码片段和文件信息

/** Automatically generated file. DO NOT MODIFY */
package com.example.android.wifidirect;

public final class BuildConfig {
    public final static boolean DEBUG = true;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        364  2013-01-06 15:14  WiFiDirectServiceDiscovery\.classpath

     文件        864  2013-01-06 15:14  WiFiDirectServiceDiscovery\.project

     文件        177  2013-01-06 15:14  WiFiDirectServiceDiscovery\.settings\org.eclipse.jdt.core.prefs

     文件       1461  2013-01-04 13:09  WiFiDirectServiceDiscovery\AndroidManifest.xml

     文件       1461  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\AndroidManifest.xml

     文件        389  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\BuildConfig.class

     文件       2510  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\ChatManager.class

     文件       1873  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\ClientSocketHandler.class

     文件       2013  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\GroupOwnerSocketHandler.class

     文件        406  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$attr.class

     文件        473  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$drawable.class

     文件        559  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$id.class

     文件        533  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$layout.class

     文件        567  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$string.class

     文件        495  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R$style.class

     文件        755  2013-01-06 15:19  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\R.class

     文件       1914  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiChatFragment$1.class

     文件       2594  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiChatFragment$ChatMessageAdapter.class

     文件        324  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiChatFragment$MessageTarget.class

     文件       3216  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiChatFragment.class

     文件       2528  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiDirectBroadcastReceiver.class

     文件        392  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiDirectServicesList$DeviceClickListener.class

     文件       3113  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiDirectServicesList$WiFiDevicesAdapter.class

     文件       2679  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiDirectServicesList.class

     文件        525  2013-01-06 15:14  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiP2pService.class

     文件       1340  2013-01-06 15:45  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiServiceDiscoveryActivity$1.class

     文件       1173  2013-01-06 15:45  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiServiceDiscoveryActivity$2.class

     文件       2696  2013-01-06 15:45  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiServiceDiscoveryActivity$3.class

     文件       1995  2013-01-06 15:45  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiServiceDiscoveryActivity$4.class

     文件       1186  2013-01-06 15:45  WiFiDirectServiceDiscovery\bin\classes\com\example\android\wifidirect\discovery\WiFiServiceDiscoveryActivity$5.class

............此处省略168个文件信息

评论

共有 条评论