java - how to solve discovering other device via Wi-Fi (android API)? -
java - how to solve discovering other device via Wi-Fi (android API)? -
recently follow steps given developer.android.com
but seems have few luck on there. tried find available peers nearby, set 'em arraylist seems no luck. didnt anything.
my actual 2 devices android 4.1 , 4.2 since wifi direct based on api level 14 android 4.0+ think real device not problem.
the concept utilize using 1 activity , 1 broadcast receiver.
please take @ code, did set wrongly or forgot?
chat1activity.java
package com.example.androtut; import java.util.arraylist; import others.mybroadcastreceiver; import android.app.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intentfilter; import android.net.wifi.p2p.wifip2pdevicelist; import android.net.wifi.p2p.wifip2pmanager; import android.net.wifi.p2p.wifip2pmanager.channel; import android.net.wifi.p2p.wifip2pmanager.peerlistlistener; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.toast; public class chat1activity extends activity { private final intentfilter mintentfilter = new intentfilter(); private channel mchannel; private wifip2pmanager mmanager; private broadcastreceiver mreceiver; private arraylist peers = new arraylist(); private peerlistlistener mypeerlistlistener = new peerlistlistener() { @override public void onpeersavailable(wifip2pdevicelist peerlist) { // out old, in new. peers.clear(); peers.addall(peerlist.getdevicelist()); // if adapterview backed data, notify // of change. instance, if have listview of available // peers, trigger update. if (peers.size() == 0) { toast.maketext(getapplicationcontext(),"nothing found!", toast.length_long).show(); return; } else { toast.maketext(getapplicationcontext(),"we've found " + peers.size() + " users", toast.length_long).show(); } } }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_chat1); //toast.maketext(getapplicationcontext(), "wifi okay", toast.length_long).show(); prepareintentfilter(); button btn_startchat = (button) findviewbyid(r.id.btn_startchat); // listening news feed button click btn_startchat.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // launching news feed screen detectpeers(); } }); mmanager = (wifip2pmanager) getsystemservice(context.wifi_p2p_service); mchannel = mmanager.initialize(this, getmainlooper(), null); } private void detectpeers(){ mmanager.discoverpeers(mchannel, new wifip2pmanager.actionlistener() { @override public void onsuccess() { // code when discovery initiation successful goes here. // no services have been discovered yet, method // can left blank. code peer discovery goes in // onreceive method, detailed below. } @override public void onfailure(int reasoncode) { // code when discovery initiation fails goes here. // alert user went wrong. } }); } @override protected void onresume() { super.onresume(); mreceiver = new mybroadcastreceiver(mmanager, mchannel, this, mypeerlistlistener); registerreceiver(mreceiver, mintentfilter); } /* unregister broadcast receiver */ @override protected void onpause() { super.onpause(); unregisterreceiver(mreceiver); } private void prepareintentfilter() { mintentfilter.addaction(wifip2pmanager.wifi_p2p_state_changed_action); // indicates alter in list of available peers. mintentfilter.addaction(wifip2pmanager.wifi_p2p_peers_changed_action); // indicates state of wi-fi p2p connectivity has changed. mintentfilter.addaction(wifip2pmanager.wifi_p2p_connection_changed_action); // indicates device's details have changed. mintentfilter.addaction(wifip2pmanager.wifi_p2p_this_device_changed_action); } }
and mybroadcastreceiver.java
package others; import java.util.arraylist; import com.example.androtut.chat1activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.net.wifi.p2p.wifip2pdevice; import android.net.wifi.p2p.wifip2pdevicelist; import android.net.wifi.p2p.wifip2pmanager; import android.net.wifi.p2p.wifip2pmanager.channel; import android.net.wifi.p2p.wifip2pmanager.peerlistlistener; import android.widget.toast; public class mybroadcastreceiver extends broadcastreceiver { private wifip2pmanager mmanager; private channel mchannel; private chat1activity mactivity; private peerlistlistener mypeerlistlistener; public mybroadcastreceiver(wifip2pmanager manager, channel channel, chat1activity activity, peerlistlistener obpeerlist) { super(); this.mmanager = manager; this.mchannel = channel; this.mactivity = activity; this.mypeerlistlistener = obpeerlist; } @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if (wifip2pmanager.wifi_p2p_state_changed_action.equals(action)) { // check see if wi-fi enabled , notify appropriate activity } else if (wifip2pmanager.wifi_p2p_peers_changed_action.equals(action)) { // phone call wifip2pmanager.requestpeers() list of current peers if (mmanager != null) { mmanager.requestpeers(mchannel, mypeerlistlistener); } } else if (wifip2pmanager.wifi_p2p_connection_changed_action .equals(action)) { // respond new connection or disconnections } else if (wifip2pmanager.wifi_p2p_this_device_changed_action .equals(action)) { // respond device's wifi state changing } if (wifip2pmanager.wifi_p2p_state_changed_action.equals(action)) { int state = intent.getintextra(wifip2pmanager.extra_wifi_state, -1); if (state == wifip2pmanager.wifi_p2p_state_enabled) { // wifi p2p enabled toast.maketext(mactivity.getapplicationcontext(),"wifi okay", toast.length_long).show(); } else { // wi-fi p2p not enabled toast.maketext(mactivity.getapplicationcontext(), "wifi not okay", toast.length_long).show(); } } } }
i guess did't add together peerlistlistener activity. won't reply.
you can seek implement peerlistlistener in activity
public class chat1activity extends activity implements wifip2pmanager.peerlistlistener
and override onpeeravailable method in activity
@override public void onpeersavailable(wifip2pdevicelist peers) { //put code here }
and alter requestpeers method in receiver
if (mmanager != null) { mmanager.requestpeers(mchannel, mactivity); }
hope help!!
java android eclipse wifi broadcast
Comments
Post a Comment