android wifi AsyncChannel(WifiManager和WifiP2pManager)

AynscChannel的讲解

[Android]AsyncChannel介绍-CSDN博客

WifiP2pManager里的channel的使用理解

WifiP2pManager.java

java 复制代码
public void createGroup(Channel c, ActionListener listener) {
   checkChannel(c);
   c.mAsyncChannel.sendMessage(CREATE_GROUP, WifiP2pGroup.NETWORK_ID_PERSISTENT,
   c.putListener(listener));
}
这里面的c变量是Channel, c.putListener按理说Channel里有putListener方法

一开始以为Channel是android 里的工具类接口,结果发现WifiP2pManager里有这个Channel的定义

里面还有个P2pHandle,解析各种信息,调用不同的回调,可以看出来这儿不止有ActionListener回调,还有P2pStateListener、ConnectionInfoListener等

java 复制代码
class WifiP2pManager {
public static class Channel implements AutoCloseable {
     public Channel(Context context, Looper looper, ChannelListener l, Binder binder,WifiP2pManager p2pManager) {
         mAsyncChannel = new AsyncChannel();
         mHandler = new P2pHandler(looper);
     }

     class P2pHandler extends Handler {
        public void handleMessage(Message message) {
            case DISCOVER_PEERS_FAILED:
            case STOP_DISCOVERY_FAILED:
            case DISCOVER_SERVICES_FAILED:
            case CONNECT_FAILED:
            case CANCEL_CONNECT_FAILED:
            case CREATE_GROUP_FAILED:
            case REMOVE_GROUP_FAILED:
                ((ActionListener) listener).onFailure(message.arg1);
                break;
            case CREATE_GROUP_SUCCEEDED:
            case REMOVE_GROUP_SUCCEEDED:
                 ((ActionListener) listener).onSuccess();

            case RESPONSE_P2P_STATE:
                  ((P2pStateListener) listener)
                                    .onP2pStateAvailable(message.arg1);
            case RESPONSE_CONNECTION_INFO:
                  ((ConnectionInfoListener)listener)
                        .onConnectionInfoAvailable(wifiP2pInfo);
        }
     }
     private int putListener(Object listener) {
            int key;
            key = mListenerKey++;
            mListenerMap.put(key, listener);
            }
            return key;
     }
}
}