本文介绍了检查使用Android应用Miracast的兼容设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我如何搜索Miracast的兼容设备在android系统(可使用WiFi直)?

How can I search for Miracast compatible devices (may be using WiFi Direct) in android?

我也是刚刚才知道,DISPLAYMANAGER并在presentation和放大器的Andr​​oid 4.2的帮助presentation类; Miracast的。但有什么办法,我可以检查其他设备兼容的Miracast /搜索Miracast的沉沦?

I just got to know that DisplayManager and Presentation class in Android 4.2 help in presentation & miracast. But is there any way I can check if the other device is Miracast compatible / search Miracast sink?

由于结果
Smitha

Thanks
Smitha

推荐答案

Android框架源$ C ​​$ CS展示如何搜索Miracast的终端设备。

android framework source codes shows how to search miracast sink devices.

基本上使用WiFi Direct设备搜索API,
discoverPeers - > requestPeers - > isWifiDisplay&安培; isPrimarySinkDeviceType

basically using WiFi Direct devices search API,discoverPeers -> requestPeers -> isWifiDisplay & isPrimarySinkDeviceType

private static boolean isWifiDisplay(WifiP2pDevice device) {
    return device.wfdInfo != null
           && device.wfdInfo.isWfdEnabled()
           && isPrimarySinkDeviceType(device.wfdInfo.getDeviceType());
}

private static boolean isPrimarySinkDeviceType(int deviceType) {
    return deviceType == WifiP2pWfdInfo.PRIMARY_SINK
           || deviceType == WifiP2pWfdInfo.SOURCE_OR_PRIMARY_SINK;
}



WifiP2pWfdInfo定义了四种类型的设备,


WifiP2pWfdInfo defined four device types,

public static final int WFD_SOURCE              = 0;
public static final int PRIMARY_SINK            = 1;
public static final int SECONDARY_SINK          = 2;
public static final int SOURCE_OR_PRIMARY_SINK  = 3;

的http s://android.googlesource.com/platform/frameworks/base/+/master/wifi/java/android/net/wifi/p2p/WifiP2pWfdInfo.java

http s://android.googlesource.com/platform/frameworks/base/+/master/wifi/java/android/net/wifi/p2p/WifiP2pWfdInfo.java



如果你不熟悉的Wi-Fi直连,也许我的应用程序是使用Wi-Fi直连的API是有用的。


if you don't familiar with wifi direct, maybe my app is useful for using wifi direct api.

HTTP小号://github.com/kensuke/WiFiDirectTestApp

http s://github.com/kensuke/WiFiDirectTestApp

地址:2014年2月24日 - 解析的toString输出字符串的认识Miracast的设备源,汇

ADD: 2014/02/24 - parse toString output String for recognize Miracast devices Source or Sink

的Wi-Fi Direct的API requestPeers()回调至onPeersAvailable(),这个方法的参数WifiP2pDeviceList有附近P2P设备是WifiP2pDevice实例的列表,WifiP2pDevice有一个设备的Wi-Fi Direct的信息,包括Miracast的信息(WFD .. )和设备到可以使用WifiP2pDevice.toString()方法。

Wi-Fi Direct API requestPeers() callback to onPeersAvailable(), this method's parameter WifiP2pDeviceList has near by P2p devices that is a list of WifiP2pDevice instances, WifiP2pDevice has one device Wi-fi Direct information including Miracast information(wfd..) and device into can get uses WifiP2pDevice.toString() method.

搜索WFD DeviceInfo:XXX的字符串,XXX是一个数值,蒙面×03(见WifiP2pWfdInfo.java),蒙面后,我们得到0-3。该值定义源或汇,见WifiP2pWfdInfo.java常量)。

Search "WFD DeviceInfo: XXX" String, XXX is a numerical value, masked "0x03" (see WifiP2pWfdInfo.java), after masked we get 0-3. That value defined SOURCE or SINK, see WifiP2pWfdInfo.java constants).

private static final int WFD_SOURCE = 0;
private static final int PRIMARY_SINK = 1;
private static final int SECONDARY_SINK = 2;
private static final int SOURCE_OR_PRIMARY_SINK = 3;

此的很本分的使用可以在非root权限的设备,一般的应用程序的方式。

This very duty way can using on non rooted device, general app.

WifiP2pDevice.toString()的样品返回的值

设备:ZTS1145

    deviceAddress:7A:E8:B6:F6:4D:74

   主要类型:10-0050F204-5

   第二类:空

    WPS:392

    grpcapab:0

    devcapab:33

   状态:3

    wfdInfo:WFD启用:trueWFD DeviceInfo:273

    WFD CtrlPort:7236

    WFD MaxThroughput:10

Sample of WifiP2pDevice.toString() returned value
"Device: ZTS1145
   deviceAddress: 7a:e8:b6:f6:4d:74
   primary type: 10-0050F204-5
   secondary type: null
   wps: 392
   grpcapab: 0
   devcapab: 33
   status: 3
   wfdInfo: WFD enabled: trueWFD DeviceInfo: 273
   WFD CtrlPort: 7236
   WFD MaxThroughput: 10"

// callback method of requestPeers();
public void onPeersAvailable(WifiP2pDeviceList peers) {
    List<WifiP2pDevice> devs = new ArrayList<WifiP2pDevice>(peers.getDeviceList());
    for (int i = 0; i < devs.size(); i++) {
        WifiP2pDevice dev = devs.get(i);
        boolean src = isWifiDisplaySource(dev);
        boolean sink = isWifiDisplaySink(dev);
        Log.d(TAG, dev.deviceName + " isSource[" + src + "] isSink[" + sink + "]");
    }
}

private static final int WFD_DISABLED = -1;
private static final int WFD_SOURCE = 0;
private static final int PRIMARY_SINK = 1;
private static final int SECONDARY_SINK = 2;
private static final int SOURCE_OR_PRIMARY_SINK = 3;

private static final int DEVICE_TYPE = 0x3;

private int getWFDDeviceInfoFromString(String devStr) {
    if (devStr == null) {
        return WFD_DISABLED;
    }

    boolean wfd = false;
    for (String line : devStr.split("\n")) {
        // start self parsing...
        // TODO: sprintf parse is more easy

        // search "WFD enabled: [true|false]"
        if (!line.matches(".*WFD enabled:.*")) {
            continue;
        }

        String[] tokens = line.split(":");
        int toks = tokens.length;
        for (int i = 0; i < toks - 1; i++) {
            if (!tokens[i].contains("WFD enabled")) {
                continue;
            }

            String tok = tokens[i + 1].replaceAll("\\s", ""); // delete white space
            if (tok.startsWith("true")) {
                wfd = true;
                break;
            }
            // why didn't use .equals() instead of .contains() and .startsWith() ? because
            // 1) "wfdInfo: WFD enabled: trueWFD DeviceInfo: 273"          // inputed string
            // 2) "(wfdInfo):( WFD enabled):( trueWFD DeviceInfo):( 273)"  // : splited string
            // 3) "( trueWFD DeviceInfo)" => "trueWFD DeviceInfo"          // white space deleted
        }
    }
    if (!wfd) {
        return WFD_DISABLED;
    }

    for (String line : devStr.split("\n")) {
        // search "WFD DeviceInfo: \d+"
        if (!line.matches(".*WFD DeviceInfo:.*")) {
            continue;
        }

        String[] tokens = line.split(":");
        int toks = tokens.length;
        for (int i = 0; i < toks - 1; i++) {
            if (!tokens[i].contains("WFD DeviceInfo")) {
                continue;
            }

            String tok = tokens[i + 1].replaceAll("\\s", "");
            int deviceInfo = Integer.parseInt(tok);
            Log.d(TAG, "line[" + line + "] DeviceInfo[" + deviceInfo + "] masked[" + (deviceInfo & DEVICE_TYPE) + "]");
            return deviceInfo;
        }
    }

    return WFD_DISABLED;
}

private boolean isWifiDisplaySource(WifiP2pDevice dev) {
    if (dev == null) {
        return false;
    }

    int deviceInfo = getWFDDeviceInfoFromString(dev.toString());
    if (deviceInfo == WFD_DISABLED) {
        return false;
    }

    int deviceType = deviceInfo & DEVICE_TYPE; // masked
    return deviceType == WFD_SOURCE || deviceType == SOURCE_OR_PRIMARY_SINK;
}

private boolean isWifiDisplaySink(WifiP2pDevice dev) {
    if (dev == null) {
        return false;
    }

    int deviceInfo = getWFDDeviceInfoFromString(dev.toString());
    if (deviceInfo == WFD_DISABLED) {
        return false;
    }

    int deviceType = deviceInfo & DEVICE_TYPE; // masked
    return deviceType == PRIMARY_SINK || deviceType == SOURCE_OR_PRIMARY_SINK;
}

这篇关于检查使用Android应用Miracast的兼容设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 18:27