资源简介
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();
}
});
}
}
}
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.xm
文件 1461 2013-01-06 15:14 WiFiDirectServiceDiscovery\bin\AndroidManifest.xm
文件 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$st
文件 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个文件信息
- 上一篇:简单微信支付和银联支付
- 下一篇:Fragment切换
相关资源
- android 数字以动画的形式动态增长 源
- 西玛拉雅FM调用
- android 仿微信主界面UI(不附代码逻辑
- android 点击两次返回键退出
- android崩溃日志保存SDCARD-本地阅读-并
- 雅圆字体 otf艺术字体
- android 拍照摄像 回放代码
- android 自定义进度加载条Demo
- android 即时通讯 IM(MobileIMSDK)
- android 详情页导航栏颜色渐变
- android 拖动浏览大图片(类似地图拖动
- Android_AlertDialog代码
- android 评分自定义控件 以及
- android 白天 夜间模式切换
- android grid 分页(美团九宫格导航)
-
android 解析xm
l - android分类标题框架demo(SmartTabLayout)
- Glide加载网络图片
- android 端视频拍摄(带进度条)
- Android 2秒内按两次back键退出应用
- 一个基于RSS的新闻应用
- android 息屏亮屏 (呼吸效果)
- 精仿微信程序android源码
- 《kotlin-for-android-developers-zh》(kotli
- android bluetooth 搜尋距離
- Android应用源码追加的6中图片特效处理
- 完美解决android_gif_drawable_demo
- android 调整屏幕亮度 (无需root)
- android通过ajax上传文件到php服务器
- android 联动滚轮(选择控件)
评论
共有 条评论