java android p2p通信聊天源代码_Java类android.net.wifi.p2p.WifiP2pManager.PeerListListener的实例源码 - 编程字典...

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

log("wifi direct changed state", false);

// Determine if Wifi P2P mode is enabled or not, alert the Activity.

int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);

if (state != WifiP2pManager.WIFI_P2P_STATE_ENABLED) {

log("p2p not enabled", true);

}

} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

// log("peer list changed", false);

if (p2pManager != null) {

p2pManager.requestPeers(p2pChannel, new PeerListListener() {

@Override

public void onPeersAvailable(WifiP2pDeviceList peers) {

// log("peer list changed", false);

}

});

}

} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

// Respond to new connection or disconnections

log("connection changed", false);

if (p2pManager == null) {

return;

}

NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

if (networkInfo.isConnected()) {

log("connected", false);

// We are connected with the other device, request connection info to find group owner IP

p2pManager.requestConnectionInfo(p2pChannel, new ConnectionInfoListener() {

@Override

public void onConnectionInfoAvailable(WifiP2pInfo info) {

InetAddress groupOwnerAddress = info.groupOwnerAddress;

if (info.groupFormed && info.isGroupOwner) {

wdManager.connected(groupOwnerAddress.getHostAddress(), true);

log("[connected] group owner with ip = " + groupOwnerAddress.getHostAddress() + " (me)", true);

} else if (info.groupFormed) {

wdManager.connected(groupOwnerAddress.getHostAddress(), false);

log("[connected] group owner ip = " + groupOwnerAddress.getHostAddress(), true);

}

wdManager.setConnected(true);

}

});

} else {

// this is called also when the other peer refuses connection

wdManager.setConnected(false);

log("disconnected", true);

}

} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {

// This is called when the connectivity is lost

// TODO unregister here any registered expressions with the remote

wdManager.setConnected(false);

wdManager.resetPeers();

log("connectivity lost", true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值