PDA

View Full Version : مشکل در متصل شدن دو دستگاه با وای فای



afshinhd
یک شنبه 30 آذر 1393, 09:27 صبح
سلام...کدی که من نوشتم لیست دستگاه های اماده ی متصل شدن رو نشون نمیده..لطفا بگید مشکل کجاست؟؟
متد اخر قراره که این کارو انجام بده..به این صورت که اسمارو بریزه تو list و به وسیله ی list view اونو نشون بده

public class MainActivity extends Activity{
private final IntentFilter intentFilter = new IntentFilter();
private WifiP2pManager manager;
private Channel channel;
private boolean isWifiP2pEnabled = false;
private BroadcastReceiver receiver = null;
private List list = new ArrayList();
private Context a;


public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayAdapter adapter = new ArrayAdapter<>(this,
R.layout.afshin, list);
ListView listView = (ListView) findViewById(R.id.country_list);
listView.setAdapter(adapter);

intentFilter.addAction(WifiP2pManager.WIFI_P2P_STA TE_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEE RS_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_CON NECTION_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_THI S_DEVICE_CHANGED_ACTION);

manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);

}

@Override
public void onResume() {
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
WifiManager wifiManager = (WifiManager)getSystemService(Context .WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
unregisterReceiver(receiver);
}

@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;
}

if (id == R.id.atn_direct_enable ){
WifiManager wifiManager = (WifiManager)getSystemService(Context .WIFI_SERVICE);
if (manager != null && channel != null && wifiManager.setWifiEnabled(false)) {


wifiManager.setWifiEnabled(true);

return true;
}
if(wifiManager.setWifiEnabled(true)){
wifiManager.setWifiEnabled(false);
return true;
}

return true;
}
if (id == R.id.atn_direct_discover ){
if (!isWifiP2pEnabled) {
Toast.makeText(MainActivity.this, R.string.p2p_off_warning,
Toast.LENGTH_SHORT).show();
return true;
}
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Discovery Initiated",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reasonCode) {
Toast.makeText(MainActivity.this, "Discovery Failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});

return true;
}
return super.onOptionsItemSelected(item);
}
PeerListListener peerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {

// Out with the old, in with the new.
list.clear();
list.addAll(peerList.getDeviceList());


}
};

}

NeoFighT
یک شنبه 30 آذر 1393, 11:58 صبح
دوست عزیز الان حتی خود شما هم نمیتونید کد رو بخونید چه برسه به ما
وقتی میخواین کد بنویسید اوی همین ابزار های بالا سمت چپ روی Java کلیک کنید ، یک تگ ساخته میشه و بین این تگ کدتون رو بنویسید.:چشمک:

afshinhd
یک شنبه 30 آذر 1393, 14:20 عصر
public class MainActivity extends Activity{
private final IntentFilter intentFilter = new IntentFilter();
private WifiP2pManager manager;
private Channel channel;
private boolean isWifiP2pEnabled = false;
private BroadcastReceiver receiver = null;
private List list = new ArrayList();
private Context a;
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
private int b;


public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



intentFilter.addAction(WifiP2pManager.WIFI_P2P_STA TE_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEE RS_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_CON NECTION_CHANGED_ACTION);


intentFilter.addAction(WifiP2pManager.WIFI_P2P_THI S_DEVICE_CHANGED_ACTION);

manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);

}

@Override
public void onResume() {
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
registerReceiver(receiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
WifiManager wifiManager = (WifiManager)getSystemService(Context .WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
unregisterReceiver(receiver);
}

@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;
}

if (id == R.id.atn_direct_enable ){
WifiManager wifiManager = (WifiManager)getSystemService(Context .WIFI_SERVICE);
if (manager != null && channel != null && wifiManager.setWifiEnabled(false)) {


wifiManager.setWifiEnabled(true);

return true;
}
if(wifiManager.setWifiEnabled(true)){
wifiManager.setWifiEnabled(false);
return true;
}

return true;
}
if (id == R.id.atn_direct_discover ){
if (!isWifiP2pEnabled) {
Toast.makeText(MainActivity.this, R.string.p2p_off_warning,
Toast.LENGTH_SHORT).show();
return true;
}
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Discovery Initiated",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int reasonCode) {
Toast.makeText(MainActivity.this, "Discovery Failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});

return true;
}
if (id == R.id.atn_direct_peerlist){
PeerListListener peerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {

// Out with the old, in with the new.
list.clear();
list.addAll(peerList.getDeviceList());
ArrayAdapter adapter = new ArrayAdapter<>(a,
R.layout.afshin, list);
ListView listView = (ListView) findViewById(R.id.country_list);
listView.setAdapter(adapter);


}
};
}
return super.onOptionsItemSelected(item);
}


}

afshinhd
دوشنبه 01 دی 1393, 00:09 صبح
میخوام در منو گزینه peerlist رو که میزنم لیست دستگاه هارو نشون بده...کمک کنید لطفا

afshinhd
دوشنبه 01 دی 1393, 11:40 صبح
.................................................. ......