Android 投屏 发现
Posted 海天鹰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 投屏 发现相关的知识,希望对你有一定的参考价值。
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,获取服务,推送网址。
以上是关于Android 投屏 发现的主要内容,如果未能解决你的问题,请参考以下文章