2

如何在 android 中搜索 Miracast 兼容设备(可能使用 WiFi Direct)?

我刚刚知道 Android 4.2 中的 DisplayManager 和 Presentation 类有助于演示和 Miracast。但是有什么方法可以检查其他设备是否与 Miracast 兼容/搜索 Miracast 接收器?

谢谢史密莎
_

4

1 回答 1

2

android 框架源代码展示了如何搜索 miracast sink 设备。

基本上使用 WiFi Direct 设备搜索 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;
}

https://github.com/kensuke/How-to-Miracast-on-AOSP/wiki/wfd_scan


WifiP2pWfdInfo 定义了四种设备类型,

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


如果您不熟悉 wifi direct,也许我的应用程序对于使用 wifi direct api 很有用。

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


添加:2014/02/24 - 解析 toString 输出字符串以识别 Miracast 设备源或接收器

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

搜索“WFD DeviceInfo:XXX”字符串,XXX为数值,掩码为“0x03”(见WifiP2pWfdInfo.java),掩码后得到0-3。该值定义了 SOURCE 或 SINK,请参阅 WifiP2pWfdInfo.java 常量)。

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设备、通用应用程序上使用。

WifiP2pDevice.toString() 返回值示例
“设备:ZTS1145 设备
   地址: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"

// 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;
}
于 2013-08-29T07:32:33.650 回答