Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by registered user pooja ( 12 years ago )
package com.example.chat;
import java.util.List;
import com.example.chat.R;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private boolean isWifiP2pEnabled = false;
private BroadcastReceiver receiver = null;
private Button click;
//to access wifi states
private final IntentFilter intentFilter = new IntentFilter();
//for log purposes
public static final String TAG = "wifidirectdemo";
//wifi channel
Channel mChannel;
WifiP2pManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.click);
//WIFI P2P CODE - changes in the wifi states
// Indicates a change in the Wi-Fi P2P status.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
// Indicates a change in the list of available peers.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
// Indicates the state of Wi-Fi P2P connectivity has changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
// Indicates this device's details have changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
click.setOnClickListener(new OnClickListener()
{
//on click start the device discovery
public void onClick(View arg0)
{
// Intent intent = new Intent(context, Activity2.class);
// startActivity(intent);
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method
//simply display discovery successfully initiated!
Toast.makeText(getApplicationContext(), "discovery initiated!",
Toast.LENGTH_SHORT).show();
//after this is succesfully initiated all the control goes to the onrecieve method in the other file!
}
@Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
Toast.makeText(getApplicationContext(), "discovery failed",
Toast.LENGTH_SHORT).show();
}
});
}
}
);//end of onclick();
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
/******************************************************************************/
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
@Override
public void onResume() {
super.onResume();
receiver = new WifiDirectBroadCastReceiver(mManager, mChannel, this);
registerReceiver(receiver, intentFilter);
Toast.makeText(getApplicationContext(), "...registering...",
Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
public void updatedevicescreen(List peers)
{
ListView list;
list = (ListView)findViewById(R.id.devicelist);
String wifis[];
wifis = new String[peers.size()];
for(int i = 0; i < peers.size(); i++){
wifis[i] = ((peers.get(i)).toString());
}
list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,wifis));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Revise this Paste