Connecting withWi-Fi Direct
Wi-FiDirect™APIs允许应用连接周围的设备,而不用通过网络或者热点,点对点直接连接。应用可以快速的发现周围设备并与之交互,而有效作用距离大于蓝牙。
以下内容关于如何通过Wi-Fi Direct™发现和连接周围设备:
第一步:权限问题 Set Up ApplicationPermissions
在manifest里添加三个权限:
CHANGE_WIFI_STATE,
ACCESS_WIFI_STATE,
INTERNET 。
Wi-FiDirect™不需要网络,但是需要java socket,而java socket需要INTERNET 权限。
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nsdchat"
...
<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"/>
...
第二步:新建广播接收器和点对点管理器
SetUp a Broadcast Receiver and Peer-to-PeerManager
使用Wi-FiDirect™,需要监听(broadcast intents)广播intent,当有特定的事件发生时,你的应用就会觉察到。
在app里实例化一个IntentFilter对象出来,将其设置为监听以下内容:
WIFI_P2P_STATE_CHANGED_ACTION
Indicateswhether Wi-Fi Peer-To-Peer (P2P) is enabled----P2P功能是否打开
WIFI_P2P_PEERS_CHANGED_ACTION
Indicates thatthe available peer list has changed.----可连接peer改变
WIFI_P2P_CONNECTION_CHANGED_ACTION
Indicatesthe state of Wi-Fi P2P connectivity has changed.----P2P连接状态改变
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
Indicatesthis device's configuration details havechanged.----设备设置改变
private finalIntentFilterintentFilter = new IntentFilter();
...
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Indicates a change in the Wi-Fi Peer-to-Peerstatus.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
//Indicates a change in the list of availablepeers.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
//Indicates the state of Wi-Fi P2P connectivity haschanged.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
//Indicates this device's details havechanged.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
...
}
在onCreate()方法最后,取得一个WifiP2pManager实例,调用它的initialize()方法,这个方法返回一个WifiP2pManager.Channel对象,这个后面用来连接app和Wi-FiDirect Framework。
@Override
Channel mChannel;
public void onCreate(Bundle savedInstanceState){
....
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);//取得对象
mChannel = mManager.initialize(this, getMainLooper(), null);
}
创建一个新的 BroadcastReceiver类,用来监听系统Wi-Fi P2P的变化状态,在onReceive()方法里,增加condition条件来处理各种P2P状态的改变。
@Override
public void onReceive(Context context, Intent intent){
String action =intent.getAction();//获取接收的intent里的action
//以下进行判断,对各种不同的P2P状态改变,执行不同的代码
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)){
// 检查 WifiDirect模式是否开启
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);
if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
activity.setIsWifiP2pEnabled(true); //开启则...
} else {
activity.setIsWifiP2pEnabled(false); //未开启则...
}
}
elseif (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
// The peer list has changed! We should probablydo something about that.
}
elseif(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
// Connection state changed! We should probablydo something about that.
}
else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)){
DeviceListFragment fragment = (DeviceListFragment)activity.getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.updateThisDevice((WifiP2pDevice)intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
}
}
最后,当你 主activity激活以后,添加代码在onResume()方法里注册 intent filter 和 broadcast receiver。当你的activity paused的时候,在onPause()方法里对它们解除注册。
@Override
public void onResume() {
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(mManager, mChannel,this);
registerReceiver(receiver,intentFilter);
}
@Override
public voidonPause() {
super.onPause();//调用父类方法
unregisterReceiver(receiver);//添加新的内容,解除注册
}
第三步:开始peer发现Initiate Peer Discovery
调用discoverPeers()方法,来发现周围的设备。这个方法需要2个参数:
1.WifiP2pManager.Channel
2.WifiP2pManager.ActionListener 的一个新的实现
第一个参数是一个对象,第二个参数是内部类对象
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener(){
@Override
public void onSuccess() {
// Code for when the discovery initiation is successful goeshere.
// No services have actually been discovered yet, so thismethod
// can often be left blank. Code for peerdiscovery 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.
}
});
这只是启动peerdiscovery,方法discoverPeers()开始了发现线程,然后马上返回。如果peerdiscovery线程成功启动,系统就会注意到。发现一直持续直到建立连接或者建立一个P2P group。
第四步:获取peer 列表 Fetch the List ofPeers
首先实现WifiP2pManager.PeerListListener接口,这个接口提供了发现的peer的信息。
private List peers =new ArrayList();//装peer的list
...
private PeerListListenerpeerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList){
// Out with the old, in with the new.
peers.clear();//清空list,刷新
peers.addAll(peerList.getDeviceList());
// If an AdapterView is backed by this data, notify it
// of the change. For instance, if you have aListView of available
// peers, trigger an update.
((WiFiPeerListAdapter)getListAdapter()).notifyDataSetChanged();
//如果peers.size()=0,则没有发现peer
if (peers.size() == 0) {
Log.d(WiFiDirectActivity.TAG, "No devices found");
return;
}
}
}
修改broadcastreceiver的onReceive()方法,当接收到一个action包含WIFI_P2P_PEERS_CHANGED_ACTION的Intent的时候,调用requestPeers()方法,
需要把监听器传入接收器,方法之一就是把listener传入broadcastreceiver构造器。
public void onReceive(Context context, Intent intent){
...
else if(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
// Request available peers from the wifi p2p manager. This isan
// asynchronous call and the calling activity is notified witha
// callback on PeerListListener.onPeersAvailable()
if (mManager != null) {
mManager.requestPeers(mChannel, peerListener);
}
Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
}...
}
这样的话,一个intent的action是WIFI_P2P_PEERS_CHANGED_ACTION 的时候,就会触发更新peer list的请求。
第五步:连接peer:Connectto a Peer
要连接peer,就要new一个新的WifiP2pConfig对象,并把想连接的peer的信息写入,最后调用 connect()方法。
@Override
public voidconnect() {
// Picking the first device found on the network.
WifiP2pDevice device = peers.get(0);
WifiP2pConfigconfig = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
mManager.connect(mChannel, config, new ActionListener(){
@Override
public void onSuccess() {
// WiFiDirectBroadcastReceiver will notify us. Ignore fornow.
}
@Override
public void onFailure(int reason) {
Toast.makeText(WiFiDirectActivity.this, "Connect failed.Retry.",
Toast.LENGTH_SHORT).show();
}
});
}
WifiP2pManager.ActionListener只是负责通知peerinitiation是否成功,要监听P2P状态改变的话,就要实现WifiP2pManager.ConnectionInfoListener,它的onConnectionInfoAvailable()回调方法会在P2P状态改变时做出反应。在多个设备需要连接同一个设备的情况下,有一个设备会被指定为“groupowner“。
@Override
public voidonConnectionInfoAvailable(finalWifiP2pInfo info) {
// InetAddress from WifiP2pInfostruct.
InetAddress groupOwnerAddress =info.groupOwnerAddress.getHostAddress());
// After the group negotiation, we can determinethe group owner.
if (info.groupFormed&& info.isGroupOwner) {
// Dowhatever tasks are specific to the group owner.
// Onecommon case is creating a server thread and accepting
//incoming connections.
} else if (info.groupFormed) {
// Theother device acts as the client. In this case,
// you'llwant to create a client thread that connects to thegroup
//owner.
}
}
返回到broadcastreceiver里的onReceive()方法,修改下,接收到WIFI_P2P_CONNECTION_CHANGED_ACTION这个intent的时候,调用requestConnectionInfo()方法。
..
} else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){
if (mManager == null){
return;
}
NetworkInfo networkInfo = (NetworkInfo)intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if(networkInfo.isConnected()) {
// We are connected with theother device, request connection
// info to find group ownerIP
mManager.requestConnectionInfo(mChannel,connectionListener);
}
...