Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未调用Android P2P服务发现回调_Android_Android Networking_Wifi Direct_Dns Sd - Fatal编程技术网

未调用Android P2P服务发现回调

未调用Android P2P服务发现回调,android,android-networking,wifi-direct,dns-sd,Android,Android Networking,Wifi Direct,Dns Sd,我对安卓相当陌生,我正在开发一个可以通过WIFI直接交换数据的应用程序,我希望使用DNS服务发现而不是纯粹的P2P发现来获取更多有用的信息,如设备“昵称”等 到目前为止,我已经阅读了上的教程,并通读了文档 我的应用程序似乎成功注册了它的本地服务,并且总是调用来自discoverServices的onSuccess回调,但是从未收到来自其他设备(我使用2个设备)的txtServiceRecord 以下是我的完整代码: package com.caballero.marco.dnssd_test;

我对安卓相当陌生,我正在开发一个可以通过WIFI直接交换数据的应用程序,我希望使用DNS服务发现而不是纯粹的P2P发现来获取更多有用的信息,如设备“昵称”等

到目前为止,我已经阅读了上的教程,并通读了文档

我的应用程序似乎成功注册了它的本地服务,并且总是调用来自discoverServices的onSuccess回调,但是从未收到来自其他设备(我使用2个设备)的txtServiceRecord

以下是我的完整代码:

package com.caballero.marco.dnssd_test;

import android.graphics.Color;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener;
import android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;


import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends ActionBarActivity
{
    public static String SERVICE_NAME = "_marcotest";
    private int server_port = 0;
    private WifiP2pManager mManager;
    private Channel channel;
    private ServerSocket serverSocket;
    private TextView txtServiceStatus;
    private Button btnDiscover;
    final HashMap<String, String> buddies = new HashMap<String, String>();
    private ArrayAdapter<String> adapter;
    //private ActionListener serviceDiscoveryListener;
    private WifiP2pDnsSdServiceRequest serviceRequest;
    private DnsSdServiceResponseListener serviceResponseListener;
    private DnsSdTxtRecordListener txtRecordListener;
    private WifiP2pDnsSdServiceInfo serviceInfo;

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

        /* Load controls */
        txtServiceStatus = (TextView)findViewById(R.id.txtStatus);
        btnDiscover = (Button)findViewById(R.id.btnDiscover);
        btnDiscover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startServiceDiscovery();
            }
        });

        mManager = (WifiP2pManager)getSystemService(WIFI_P2P_SERVICE);
        channel = mManager.initialize(this, getMainLooper(), null);
        adapter = new ArrayAdapter<String>( this,
                                            android.R.layout.simple_list_item_1,
                                            new ArrayList<String>());
        /* Initialize Socket Server */
        try {
            initializeSocketServer();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        startRegistration();
        setupDiscoverServices();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void startRegistration() {
        //  Create a string map containing information about your service.
        Map record = new HashMap();
        record.put("listenport", String.valueOf(server_port));
        record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
        record.put("available", "visible");

        // Service information.  Pass it an instance name, service type
        // _protocol._transportlayer , and the map containing
        // information other devices will want once they connect to this one.
        serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_NAME, "._tcp", record);

        // Add the local service, sending the service info, network channel,
        // and listener that will be used to indicate success or failure of
        // the request.
        mManager.addLocalService(channel, serviceInfo, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                // Command successful! Code isn't necessarily needed here,
                // Unless you want to update the UI or add logging statements.
                SpannableString text = new SpannableString("Online");
                text.setSpan(new ForegroundColorSpan(Color.GREEN), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtServiceStatus.setText(text);
                Log.v("DnsSDTest", "Service added.");
            }

            @Override
            public void onFailure(int arg0)
            {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY
                SpannableString text = new SpannableString("Offline");
                text.setSpan(new ForegroundColorSpan(Color.RED), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtServiceStatus.setText(text);
            }
        });
    }

    private void initializeSocketServer() throws IOException
    {
        serverSocket = new ServerSocket(0);
        server_port = serverSocket.getLocalPort();
    }

    private void setupDiscoverServices()
    {
        txtRecordListener = new WifiP2pManager.DnsSdTxtRecordListener() {
            @Override
            public void onDnsSdTxtRecordAvailable(String fullDomainName, Map<String, String> txtRecordMap, WifiP2pDevice srcDevice)
            {
                Log.v("DnsSDTest", "DnsSdTxtRecord available -" + txtRecordMap.toString());
                buddies.put(srcDevice.deviceAddress, txtRecordMap.get("buddyname"));
            }
        };

        serviceResponseListener = new DnsSdServiceResponseListener() {
            @Override
            public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice srcDevice)
            {
                // Update the device name with the human-friendly version from
                // the DnsTxtRecord, assuming one arrived.
                srcDevice.deviceName = buddies
                        .containsKey(srcDevice.deviceAddress) ? buddies
                        .get(srcDevice.deviceAddress) : srcDevice.deviceName;

                // Add to the custom adapter defined specifically for showing
                // wifi devices.
                adapter.add(srcDevice.toString());
                adapter.notifyDataSetChanged();
                Log.v("DnsSDTest", "onBonjourServiceAvailable " + instanceName);
            }
        };

        mManager.setDnsSdResponseListeners(channel, serviceResponseListener, txtRecordListener);
        Log.v("DnsSDTest", "Added DNS SD response listeners.");
    }

    private void startServiceDiscovery()
    {
        serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
        mManager.addServiceRequest(channel, serviceRequest, new ActionListener() {
            @Override
            public void onSuccess() {
                Log.v("DnsSDTest", "Service Request added successfully!");
            }

            @Override
            public void onFailure(int reason) {
                Log.v("DnsSDTest", "Failed to add service request!");
            }
        });

        mManager.discoverServices(channel, new ActionListener() {
            @Override
            public void onSuccess() {
                Log.v("DnsSDTest", "Service discovery successfull!");
            }

            @Override
            public void onFailure(int reason) {
                Log.v("DnsSDTest", "Service discovery failed :(");
            }
        });
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        mManager.removeLocalService(channel, serviceInfo, new ActionListener() {
            @Override
            public void onSuccess() {
                Log.v("DnsSDTest", "Removed service");
                serviceInfo = null;
            }

            @Override
            public void onFailure(int reason) {
                Log.v("DnsSDTest", "Failed to remove service");
            }
        });
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if(serviceInfo == null)
        {
            startRegistration();
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();

        try {
            if(!serverSocket.isClosed())
                serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
包com.caballero.marco.dnssd_测试;
导入android.graphics.Color;
导入android.net.wifi.p2p.wifip2p设备;
导入android.net.wifi.p2p.WifiP2pManager;
导入android.net.wifi.p2p.WifiP2pManager.Channel;
导入android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener;
导入android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener;
导入android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo;
导入android.net.wifi.p2p.WifiP2pManager.ActionListener;
导入android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.text.SpannableString;
导入android.text.span;
导入android.text.style.ForegroundColorSpan;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.TextView;
导入java.io.IOException;
导入java.net.ServerSocket;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
公共类MainActivity扩展了ActionBarActivity
{
公共静态字符串服务_NAME=“_marcotest”;
专用int服务器_端口=0;
私有WifiP2pManager-mManager;
专用信道;
私有服务器套接字服务器套接字;
私有文本视图txtServiceStatus;
专用按钮btnDiscover;
final HashMap buddies=new HashMap();
专用阵列适配器;
//私有ActionListener服务DiscoveryListener;
私有WifiP2pDnsSdServiceRequest服务请求;
专用DnsSdServiceResponseListener服务ResponseListener;
私有DnsSdTxtRecordListener txtRecordListener;
私有WifiP2pDnsSdServiceInfo serviceInfo;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*负载控制*/
txtServiceStatus=(TextView)findViewById(R.id.txtStatus);
btnDiscover=(按钮)findViewById(R.id.btnDiscover);
btnDiscover.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
startServiceDiscovery();
}
});
mManager=(WifiP2pManager)getSystemService(WIFI\u P2P\u服务);
channel=mManager.initialize(this,getMainLooper(),null);
适配器=新阵列适配器(此,
android.R.layout.simple\u list\u item\u 1,
新的ArrayList());
/*初始化套接字服务器*/
试一试{
初始化CheckServer();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
startRegistration();
setupDiscoverServices();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
私人作废startRegistration(){
//创建包含服务信息的字符串映射。
映射记录=新的HashMap();
record.put(“listenport”,String.valueOf(server_port));
put(“buddyname”,“johndoe”+(int)(Math.random()*1000));
记录。放置(“可用”、“可见”);
//服务信息。向其传递实例名称、服务类型
//_协议._传输层,以及包含
//其他设备连接到此设备后所需的信息。
serviceInfo=WifiP2pDnsSdServiceInfo.newInstance(服务名称,“.\u tcp”,记录);
//添加本地服务,发送服务信息,网络通道,
//和监听器,用于指示
//请求。
mManager.addLocalService(通道、serviceInfo、新的WifiP2pManager.ActionListener(){
@凌驾
成功时的公共无效(){
//命令成功!此处不一定需要代码,
//除非您想更新UI或添加日志记录语句。
SpannableString text=新的SpannableString(“在线”);
text.setSpan(新的ForegroundColorSpan(Color.GREEN),0,text.length(),SPAN.SPAN_EXCLUSIVE_EXCLUSIVE);
txtServiceStatus.setText(文本);
Log.v(“DnsSDTest”,“添加服务”);
}
@凌驾
公共void onFailure(int arg0)
{
//命令失败。请检查是否存在不支持的P2P\u、错误或忙
SpannableString text=新的SpannableString(“脱机”);
text.setSpan(新的ForegroundColorSpan(Color.RED),0,text.length(),SPAN.SPAN_EXCLUSIVE_EXCLUSIVE);
txtServiceStatus
serviceInfo = WifiP2pDnsSdServiceInfo.newInstance(SERVICE_NAME, "._tcp", record);