使用Android中的application / vnd.wfa.wsc创建NDEF WiFi记录 [英] Creating an NDEF WiFi record using application/vnd.wfa.wsc in Android

查看:520
本文介绍了使用Android中的application / vnd.wfa.wsc创建NDEF WiFi记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Android 5.0.0开始,您可以长时间点击WiFi连接并将该连接写入标签(写入NFC标签)。您可以在此处找到该操作的来源: WriteWifiConfigToNfcDialog.java 。采用WiFi连接并创建NDEF有效负载的相关线路似乎在此处:

As of Android 5.0.0 you can long tap on a WiFi connection and write that connection to a tag ("Write to NFC tag"). You can find the source for that operation here: WriteWifiConfigToNfcDialog.java. The relevant line that takes a WiFi connection and creates an NDEF payload appears to be here:

字符串wpsNfcConfigurationToken = mWifiManager.getWpsNfcConfigurationToken(mAccessPoint.networkId) ;

mWifiManager WifiManager的实例,但 getWpsNfcConfigurationToken 不是API的一部分。通过跟踪此方法,我们可以在此处找到它的提交:添加电话对于NFC WSC令牌创建,遗憾的是没有帮助。 这是我的调查已经用完的地方。编辑:
我发现了以下调用堆栈:

mWifiManager is an instance of WifiManager, however getWpsNfcConfigurationToken is not part of the API. By tracking down this method, we can find its commit here: Add calls for NFC WSC token creation which is unfortunately no help. This is where my investigation has run out. I've found out the following call stack:

WifiServiceImpl.java 调用 mWifiStateMachine.syncGetWpsNfcConfigurationToken(netId);

WifiStateMachine.java 调用 mWifiNative.getNfcWpsConfigurationToken(netId);

WifiNative.java 最后有方法

public String getNfcWpsConfigurationToken(int netId){
返回doStringCommand(WPS_NFC_CONFIG_TOKEN WPS+ netId);
}

然后调用

String result = doStringCommandNative(mInterfacePrefix + command);

其中 doStringCommandNative 生成一个系统调用(无法在任何地方找到此代码)。

where doStringCommandNative makes a system call (can't find the code for this anywhere).

现在调查结束的地方。

希望有人可以介入并向我展示一个创建 NdefRecord 的方法,该方法的类型为 application / vnd.wfa.wsc 给定SSID,密码,加密/验证类型。

Hoping someone can step in and show me a method that creates an NdefRecord that is of the type application/vnd.wfa.wsc given an SSID, Password, Encryption/Auth type.

我当然检查了实际应用程序/ vnd的字节数.wfa.wsc Android创建的记录,但用字节手动重新创建这个过程似乎可能非常不可靠,而且非常繁琐。

I've of course inspected the bytes of an actual application/vnd.wfa.wsc record created by Android but manually recreating this process with the bytes seems potentially very unreliable and is incredibly tedious.

推荐答案

答案在于Wi-Fi联盟Wi-Fi简单配置技术规范v2.0.5(可供下载此处)。 Android使用这种标准格式来配置WiFi网络,我错误地认为它是专有的。

The answer lies in the Wi-Fi Alliance "Wi-Fi Simple Configuration Technical Specification v2.0.5" (available for download here). Android makes use of this standard format for configuring WiFi networks, I wrongly assumed it was proprietary.

首先,我创建了一个NFC辅助类(恰当地命名为 NFCHelper.java )具有构造记录所需的所有字节常量。然后,我创建了一个hacky方法来创建所需的两个记录之一。这里的规范实际上相当无用,我所做的是检查了通过Android OS成功配置的许多标签有效负载。最后,您需要有一种机制来预先设置切换选择记录(NFC WKT Hs)(请参阅​​WiFi规范第90页)。我相信这条记录告诉Android在以下令牌中注册网络。

Firstly, I created an NFC helper class (aptly named NFCHelper.java) which has all the byte constants needed to construct the record. Then, I created a hacky method for creating one of the two records required. The spec is actually fairly useless here, what I did was examined a number of payloads of tags that had been successfully configured via the Android OS. Finally, you need to have a mechanism to prepend a "Handover Select Record (NFC WKT Hs)" (see page 90 of WiFi spec). I believe this record "tells" Android to register the network in the following token.

如何创建移交记录:

ndefRecords = new NdefRecord[2];
byte[] version = new byte[] { (0x1 << 4) | (0x2)};
ndefRecords[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_HANDOVER_REQUEST, new byte[0], version);
// and then obviously add the record you create with the method below.

创建配置令牌的方法:

private NdefRecord createWifiRecord(String[] data) {
    String ssid = data[0];
    String password = data[1];
    String auth = data[2];
    String crypt = data[3];
    byte[] authByte = getAuthBytes(auth);
    byte[] cryptByte = getCryptBytes(crypt);
    byte[] ssidByte = ssid.getBytes();
    byte[] passwordByte = password.getBytes();
    byte[] ssidLength = {(byte)((int)Math.floor(ssid.length()/256)), (byte)(ssid.length()%256)};
    byte[] passwordLength = {(byte)((int)Math.floor(password.length()/256)), (byte)(password.length()%256)};
    byte[] cred = {0x00, 0x36};
    byte[] idx = {0x00, 0x01, 0x01};
    byte[] mac = {0x00, 0x06};
    byte[] keypad = {0x00, 0x0B};

    byte[] payload = concat(NFCHelper.CREDENTIAL, cred,
            NFCHelper.NETWORK_IDX, idx,
            NFCHelper.NETWORK_NAME, ssidLength, ssidByte,
            NFCHelper.AUTH_TYPE, NFCHelper.AUTH_WPA_PERSONAL, authByte,
            NFCHelper.CRYPT_TYPE, NFCHelper.CRYPT_WEP, NFCHelper.CRYPT_AES_TKIP,
            NFCHelper.NETWORK_KEY, passwordLength, passwordByte);
           // NFCHelper.MAC_ADDRESS, mac);
    return NdefRecord.createMime(NFC_TOKEN_MIME_TYPE, payload);
} 

许可证和要点此处。你可以在网上任何地方找到 concat 方法的实现,或者只编写自己的方法。

License and gist here. You can find an implementation of the concat method anywhere on the net, or just write your own.

注意:这是一个非常糟糕的实现(您可能会注意到)。我在测试中发现了AES和AES / TKIP字节,它在Android 5下使用不同的加密/验证方法为各种网络工作。*
请随意更改函数原型,String数组只是和我正在做的事情很好地合作。

Note: this is a fairly hacky implementation (as you may notice). I am including the AES and AES/TKIP bytes as I found in testing it worked for a variety of networks using different encryption/auth methods under Android 5.* Please feel free to change the function prototype, the String array just worked nicely with what I was doing.

使用上面第一个片段中创建的两个记录,然后你应该将它传递给 NdefMessage 并写下来到你的标签。

Using the two records created in the first snippet above, you should then pass that into an NdefMessage and write it to your tag.

不久的一天,我将要写一篇关于图形和内容的更好/更强大的解决方案,所以我会更新这个答案。

One day soon I'm going to do a write up and a far better/robust soln with graphics and stuff too, so I'll update this answer then.

这篇关于使用Android中的application / vnd.wfa.wsc创建NDEF WiFi记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆