Android Open Source - android_opengles Wifi Direct3 P Activity






From Project

Back to project page android_opengles.

License

The source code is released under:

MIT License

If you think the Android project android_opengles listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nfg.wifidirect3p;
//w ww .jav a2 s .  c o m
import android.app.Activity;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pDevice;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.nfg.sdk.NFGame;
import com.nfg.sdk.NFGame.NFGameNotifyListener;
import com.nfg.sdk.NFGameServer;

public class WifiDirect3PActivity extends Activity implements NFGameNotifyListener, OnClickListener {

  private NFGame mNFGame;
  private NFGameServer mNFGameServer;

  private TextView mTextView;
  private Button mButton1;
  private Button mButton2;
  private Button mButton3;
  private Button mButton4;
  private Button mButton5;
  private Button mButton6;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTextView = (TextView) findViewById(R.id.text1);
    mButton1 = (Button) findViewById(R.id.button1);
    mButton2 = (Button) findViewById(R.id.button2);
    mButton3 = (Button) findViewById(R.id.button3);
    mButton4 = (Button) findViewById(R.id.button4);
    mButton5 = (Button) findViewById(R.id.button5);
    mButton6 = (Button) findViewById(R.id.button6);

    mButton1.setOnClickListener(this);
    mButton2.setOnClickListener(this);
    mButton3.setOnClickListener(this);
    mButton4.setOnClickListener(this);
    mButton5.setOnClickListener(this);
    mButton6.setOnClickListener(this);

    mNFGame = new NFGame(this, this);
    mNFGame.init();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mNFGame.deinit();
    stopServer();
  }

  @Override
  public void onNFGameNotify(NFGame game) {
    StringBuffer sb = new StringBuffer();

    sb.append("P2P Enable: ");
    sb.append(game.isWifiP2pEnable());
    sb.append("\n");

    sb.append("P2P Discoverying: ");
    sb.append(game.isWifiP2pDiscoverying());
    sb.append("\n");

    sb.append("Me: ");
    if (game.getMe() != null) {
      sb.append(game.getMe().deviceName);
      sb.append(":");
      sb.append(statusAbbr(game.getMe().status));
    }
    sb.append("\n");

    sb.append("Peers: \n");
    int availablePeerCount = 0;
    int connectedPeerCount = 0;
    for (int i = 0; i < game.getPeers().size(); i++) {
      WifiP2pDevice device = game.getPeers().get(i);
      sb.append("  ");
      sb.append(i + 1);
      sb.append(". ");
      sb.append(device.deviceName);
      sb.append(":");
      sb.append(statusAbbr(device.status));
      sb.append("\n");
      if (device.status == WifiP2pDevice.AVAILABLE) {
        availablePeerCount++;
      } else if (device.status == WifiP2pDevice.CONNECTED) {
        connectedPeerCount++;
      }
    }

    sb.append("Service Peers: \n");
    for (int i = 0; i < game.getServicePeers().size(); i++) {
      WifiP2pDevice device = game.getServicePeers().get(i);
      sb.append("  ");
      sb.append(i + 1);
      sb.append(". ");
      sb.append(device.deviceName);
      sb.append(":");
      sb.append(statusAbbr(device.status));
      sb.append("\n");
    }

    sb.append("Connection info: \n");
    boolean groupFormed = game.getWifiP2pInfo() != null && game.getWifiP2pInfo().groupFormed;
    boolean isGroupOwner = game.getWifiP2pInfo() != null && game.getWifiP2pInfo().isGroupOwner;
    if (groupFormed) {
      sb.append("  isGO: ");
      sb.append(game.getWifiP2pInfo().isGroupOwner);
      sb.append("\n");

      sb.append("  ip: ");
      sb.append(game.getWifiP2pInfo().groupOwnerAddress.getHostAddress());
      sb.append("\n");
    }

    sb.append("Group info: \n");
    if (game.getWifiP2pGroup() != null) {
      sb.append("  ssid: ");
      sb.append(game.getWifiP2pGroup().getNetworkName());
      sb.append("\n");

      sb.append("  pass: ");
      sb.append(game.getWifiP2pGroup().getPassphrase());
      sb.append("\n");

      sb.append("  clients: ");
      sb.append(game.getWifiP2pGroup().getClientList().size());
      sb.append("\n");
    }

    sb.append("Network ID: ");
    sb.append(game.getNetId());
    sb.append("\n");

    mTextView.setText(sb.toString());

    mButton1.setEnabled(!groupFormed);
    mButton2.setEnabled(availablePeerCount >= 1);
    mButton3.setEnabled(!game.isWifiP2pDiscoverying());
    mButton6.setEnabled(groupFormed);

    if (isGroupOwner) {
      startServer();
    }
  }

  @Override
  public void onClick(View view) {
    if (view == mButton1) {
      mNFGame.createGroup();
    } else if (view == mButton2) {
      for (int i = 0; i < mNFGame.getPeers().size(); i++) {
        WifiP2pDevice peer = mNFGame.getPeers().get(i);
        if (peer.status == WifiP2pDevice.AVAILABLE) {
          mNFGame.connect(peer.deviceAddress, true);
          break;
        }
      }
    } else if (view == mButton3) {
      mNFGame.discoverPeers();
    } else if (view == mButton4) {
      startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    } else if (view == mButton5) {
      mNFGame.reset();
      stopServer();
    } else if (view == mButton6) {
      Intent intent = new Intent(this, ChatActivity.class);
      intent.putExtra(ChatActivity.EXTRA_ADDRESS, mNFGame.getWifiP2pInfo().groupOwnerAddress.getHostAddress());
      startActivity(intent);
    }
  }

  private void startServer() {
    if (mNFGameServer == null) {
      mNFGameServer = new NFGameServer();
      mNFGameServer.start();
    }
  }

  private void stopServer() {
    if (mNFGameServer != null) {
      try {
        mNFGameServer.stop(0);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      mNFGameServer = null;
    }
  }

  private String statusAbbr(int status) {
    if (status == WifiP2pDevice.AVAILABLE) {
      return "A";
    } else if (status == WifiP2pDevice.CONNECTED) {
      return "C";
    } else if (status == WifiP2pDevice.FAILED) {
      return "F";
    } else if (status == WifiP2pDevice.INVITED) {
      return "I";
    } else if (status == WifiP2pDevice.UNAVAILABLE) {
      return "U";
    } else {
      return String.valueOf(status);
    }
  }

}




Java Source Code List

com.example.android.wifidirect.DeviceDetailFragment.java
com.example.android.wifidirect.DeviceListFragment.java
com.example.android.wifidirect.FileTransferService.java
com.example.android.wifidirect.WiFiDirectActivity.java
com.example.android.wifidirect.WiFiDirectBroadcastReceiver.java
com.example.android.wifidirect.discovery.ChatManager.java
com.example.android.wifidirect.discovery.ClientSocketHandler.java
com.example.android.wifidirect.discovery.GroupOwnerSocketHandler.java
com.example.android.wifidirect.discovery.WiFiChatFragment.java
com.example.android.wifidirect.discovery.WiFiDirectBroadcastReceiver.java
com.example.android.wifidirect.discovery.WiFiDirectServicesList.java
com.example.android.wifidirect.discovery.WiFiP2pService.java
com.example.android.wifidirect.discovery.WiFiServiceDiscoveryActivity.java
com.example.opengles.CubeRenderer.java
com.example.opengles.Cube.java
com.example.opengles.MainActivity.java
com.example.opengles.Planet.java
com.example.opengles.SolarSystemRenderer.java
com.example.opengles.SquareRenderer.java
com.example.opengles.Square.java
com.nfg.sdk.NFGameServer.java
com.nfg.sdk.NFGame.java
com.nfg.wifidirect3p.ChatActivity.java
com.nfg.wifidirect3p.WifiDirect3PActivity.java
fi.iki.elonen.HelloServer.java
fi.iki.elonen.HelloServer.java
fi.iki.elonen.IWebSocketFactory.java
fi.iki.elonen.InternalRewrite.java
fi.iki.elonen.InternalRewrite.java
fi.iki.elonen.NanoHTTPD.java
fi.iki.elonen.NanoHTTPD.java
fi.iki.elonen.NanoWebSocketServer.java
fi.iki.elonen.ServerRunner.java
fi.iki.elonen.ServerRunner.java
fi.iki.elonen.SimpleWebServer.java
fi.iki.elonen.SimpleWebServer.java
fi.iki.elonen.TempFilesServer.java
fi.iki.elonen.TempFilesServer.java
fi.iki.elonen.WebServerPluginInfo.java
fi.iki.elonen.WebServerPluginInfo.java
fi.iki.elonen.WebServerPlugin.java
fi.iki.elonen.WebServerPlugin.java
fi.iki.elonen.WebSocketException.java
fi.iki.elonen.WebSocketFrame.java
fi.iki.elonen.WebSocketResponseHandler.java
fi.iki.elonen.WebSocket.java
fi.iki.elonen.debug.DebugServer.java
fi.iki.elonen.debug.DebugServer.java
fi.iki.elonen.samples.echo.DebugWebSocketServer.java
fi.iki.elonen.samples.echo.DebugWebSocket.java
fi.iki.elonen.samples.echo.EchoSocketSample.java
org.java_websocket.AbstractWrappedByteChannel.java
org.java_websocket.SSLSocketChannel2.java
org.java_websocket.SocketChannelIOHelper.java
org.java_websocket.WebSocketAdapter.java
org.java_websocket.WebSocketFactory.java
org.java_websocket.WebSocketImpl.java
org.java_websocket.WebSocketListener.java
org.java_websocket.WebSocket.java
org.java_websocket.WrappedByteChannel.java
org.java_websocket.client.AbstractClientProxyChannel.java
org.java_websocket.client.WebSocketClient.java
org.java_websocket.drafts.Draft_10.java
org.java_websocket.drafts.Draft_17.java
org.java_websocket.drafts.Draft_75.java
org.java_websocket.drafts.Draft_76.java
org.java_websocket.drafts.Draft.java
org.java_websocket.exceptions.IncompleteHandshakeException.java
org.java_websocket.exceptions.InvalidDataException.java
org.java_websocket.exceptions.InvalidFrameException.java
org.java_websocket.exceptions.InvalidHandshakeException.java
org.java_websocket.exceptions.LimitExedeedException.java
org.java_websocket.exceptions.NotSendableException.java
org.java_websocket.exceptions.WebsocketNotConnectedException.java
org.java_websocket.framing.CloseFrameBuilder.java
org.java_websocket.framing.CloseFrame.java
org.java_websocket.framing.FrameBuilder.java
org.java_websocket.framing.FramedataImpl1.java
org.java_websocket.framing.Framedata.java
org.java_websocket.handshake.ClientHandshakeBuilder.java
org.java_websocket.handshake.ClientHandshake.java
org.java_websocket.handshake.HandshakeBuilder.java
org.java_websocket.handshake.HandshakeImpl1Client.java
org.java_websocket.handshake.HandshakeImpl1Server.java
org.java_websocket.handshake.HandshakedataImpl1.java
org.java_websocket.handshake.Handshakedata.java
org.java_websocket.handshake.ServerHandshakeBuilder.java
org.java_websocket.handshake.ServerHandshake.java
org.java_websocket.server.DefaultSSLWebSocketServerFactory.java
org.java_websocket.server.DefaultWebSocketServerFactory.java
org.java_websocket.server.WebSocketServer.java
org.java_websocket.util.Base64.java
org.java_websocket.util.Charsetfunctions.java