当前位置:首页 » 《随便一记》 » 正文

Android 投屏 发现_海天鹰

4 人参与  2021年11月10日 07:43  分类 : 《随便一记》  评论

点击全文阅读


Qt:投屏的发现和发送_海天鹰-CSDN博客 做好一部分,就忍不住做安卓版,入门难,找对资料才是王道。

SSDP协议的Android实现以及使用 - yxwkaifa - 博客园

SSDP他用了4个类,看得我头晕眼花,改一改,一个都不要!

重点:

MulticastSocket 的设置和发收。

package com.hty.dlna;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class MainActivity extends Activity {

    TextView textView;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());
        button = findViewById(R.id.button);
        button.setOnClickListener(new MyOnClickListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, "投屏");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int item_id = item.getItemId();
        switch (item_id) {
            case 0:
                textView.setText("");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        discover();
                    }
                }).start();
                break;
        }
        return true;
    }

    class MyOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    textView.setText("");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            discover();
                        }
                    }).start();
                    break;
            }
        }
    }

    void discover() {
        String s = "M-SEARCH * HTTP/1.1\r\n"
                + "HOST: 239.255.255.250:1900\r\n"
                + "MAN: \"ssdp:discover\"\r\n"
                + "ST: urn:schemas-upnp-org:service:AVTransport:1\r\n" //投屏
                + "MX: 3\r\n"
                + "\r\n";

        try  {
            MulticastSocket socket = new MulticastSocket(5800);
            InetAddress address = InetAddress.getByName("239.255.255.250");
            socket.joinGroup(address);
            DatagramPacket DP = new DatagramPacket(s.getBytes(), s.length(), address, 1900);
            socket.send(DP);
            Message message = new Message();
            message.what = 0;
            message.obj = "发送:\n" + s;
            handler.sendMessage(message);

            while (true) {
                byte[] buf = new byte[1024];
                DP = new DatagramPacket(buf, buf.length);
                socket.receive(DP);
                String IP = DP.getAddress().toString();
                int port = DP.getPort();
                s = new String(DP.getData());
                message = new Message();
                message.what = 0;
                message.obj = "接收:" + IP + ":" + port + "\n" + s;
                handler.sendMessage(message);
            }
        } catch (Exception e) {
            Message message = new Message();
            message.what = 0;
            message.obj = e.toString();
            handler.sendMessage(message);
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    String s = (String) msg.obj;
                    textView.append(s);
                    break;
            }
        }
    };

}

接下来要解析XML,获取服务,推送网址。


点击全文阅读


本文链接:http://zhangshiyu.com/post/30708.html

天鹰  发送  才是  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1