WifiP2pUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.epson.mobilephone.common.wifidirect;
  2. import android.app.Service;
  3. import android.content.Context;
  4. import android.net.wifi.p2p.WifiP2pDevice;
  5. import android.net.wifi.p2p.WifiP2pGroup;
  6. import android.net.wifi.p2p.WifiP2pManager;
  7. import android.os.Build;
  8. import android.os.HandlerThread;
  9. import java.net.NetworkInterface;
  10. import java.net.SocketException;
  11. import java.util.Locale;
  12. /**
  13. * 局域网wifi管理工具类
  14. */
  15. public class WifiP2pUtils {
  16. private static final String TAG = "WifiP2pUtils";
  17. private static final int TIMEOUT_CONNECTIONINFO = 5000;
  18. private static WifiP2pUtils instance;
  19. private volatile WifiP2pGroup groupInfo = null;
  20. private HandlerThread handlerThread = null;
  21. private volatile Object lock = new Object();
  22. private Context mAppContext = null;
  23. private WifiP2pManager mWiFiP2PManager = null;
  24. private WifiP2pManager.Channel p2pChannnel = null;
  25. private volatile STATUS status = STATUS.IDLE;
  26. enum STATUS {
  27. IDLE,
  28. REQUEST_GROUPINFO
  29. }
  30. public static class ConnectionInfo {
  31. boolean isGroupOwnerThisDevice;
  32. public String p2PMacAdder;
  33. public String printerName;
  34. public ConnectionInfo() {
  35. }
  36. }
  37. /**
  38. * 新建实例
  39. *
  40. * @param context 上下文
  41. * @return
  42. */
  43. public static WifiP2pUtils getInstance(Context context) {
  44. WifiP2pUtils wifiP2pUtils = instance;
  45. if (wifiP2pUtils != null) {
  46. return wifiP2pUtils;
  47. }
  48. instance = new WifiP2pUtils(context);
  49. return instance;
  50. }
  51. private WifiP2pUtils(Context context) {
  52. if (Build.VERSION.SDK_INT >= 14) {
  53. mAppContext = context.getApplicationContext();
  54. handlerThread = new HandlerThread("Thread for WiFip2p");
  55. handlerThread.start();
  56. mWiFiP2PManager = (WifiP2pManager) mAppContext.getSystemService(Service.WIFI_P2P_SERVICE);
  57. WifiP2pManager wifiP2pManager = mWiFiP2PManager;
  58. if (wifiP2pManager != null) {
  59. p2pChannnel = wifiP2pManager.initialize(mAppContext, handlerThread.getLooper(), (WifiP2pManager.ChannelListener) null);
  60. if (p2pChannnel == null) {
  61. mWiFiP2PManager = null;
  62. }
  63. }
  64. }
  65. }
  66. public boolean isConnectedWiFiP2P() {
  67. return getConnectionInfo() != null;
  68. }
  69. public ConnectionInfo getConnectionInfo() {
  70. WifiP2pDevice p2PPrinterInfo;
  71. WifiP2pGroup p2POwnerInfo = getP2POwnerInfo();
  72. if (p2POwnerInfo == null || (p2PPrinterInfo = getP2PPrinterInfo(p2POwnerInfo)) == null) {
  73. return null;
  74. }
  75. ConnectionInfo connectionInfo = new ConnectionInfo();
  76. connectionInfo.isGroupOwnerThisDevice = p2POwnerInfo.isGroupOwner();
  77. connectionInfo.p2PMacAdder = p2PPrinterInfo.deviceAddress;
  78. connectionInfo.printerName = p2PPrinterInfo.deviceName;
  79. return connectionInfo;
  80. }
  81. public WifiP2pDevice getP2PPrinterInfo(WifiP2pGroup wifiP2pGroup) {
  82. if (wifiP2pGroup == null) {
  83. return null;
  84. }
  85. if (!wifiP2pGroup.isGroupOwner() && wifiP2pGroup.getOwner() != null && PrimaryDeviceType.isPrinter(wifiP2pGroup.getOwner().primaryDeviceType)) {
  86. return wifiP2pGroup.getOwner();
  87. }
  88. for (WifiP2pDevice next : wifiP2pGroup.getClientList()) {
  89. if (PrimaryDeviceType.isPrinter(next.primaryDeviceType)) {
  90. return next;
  91. }
  92. }
  93. return null;
  94. }
  95. public NetworkInterface getP2PInterfaceInfo() {
  96. WifiP2pGroup p2POwnerInfo = getP2POwnerInfo();
  97. if (p2POwnerInfo == null) {
  98. return null;
  99. }
  100. try {
  101. return NetworkInterface.getByName(p2POwnerInfo.getInterface());
  102. } catch (SocketException e) {
  103. e.printStackTrace();
  104. return null;
  105. }
  106. }
  107. private WifiP2pGroup getP2POwnerInfo() {
  108. if (mWiFiP2PManager == null) {
  109. return null;
  110. }
  111. groupInfo = null;
  112. synchronized (lock) {
  113. status = STATUS.REQUEST_GROUPINFO;
  114. mWiFiP2PManager.requestGroupInfo(p2pChannnel, new WifiP2pManager.GroupInfoListener() {
  115. public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
  116. WifiP2pGroup unused = groupInfo = wifiP2pGroup;
  117. if (groupInfo != null) {
  118. EPLog.d(WifiP2pUtils.TAG, "Received EXTRA_WIFI_P2P_GROUP = " + groupInfo.toString());
  119. } else {
  120. EPLog.d(WifiP2pUtils.TAG, "Received EXTRA_WIFI_P2P_GROUP = null");
  121. }
  122. resumeThread();
  123. }
  124. });
  125. try {
  126. lock.wait(5000);
  127. } catch (InterruptedException e) {
  128. e.printStackTrace();
  129. }
  130. status = STATUS.IDLE;
  131. }
  132. return groupInfo;
  133. }
  134. private void resumeThread() {
  135. synchronized (lock) {
  136. try {
  137. lock.notifyAll();
  138. } catch (IllegalMonitorStateException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. }
  143. public void disconnect() {
  144. WifiP2pManager wifiP2pManager = mWiFiP2PManager;
  145. if (wifiP2pManager != null) {
  146. wifiP2pManager.removeGroup(p2pChannnel, new WifiP2pManager.ActionListener() {
  147. public void onSuccess() {
  148. EPLog.d(WifiP2pUtils.TAG, "Success removeGroup");
  149. }
  150. public void onFailure(int i) {
  151. EPLog.d(WifiP2pUtils.TAG, "Failed removeGroup");
  152. }
  153. });
  154. }
  155. }
  156. public static class PrimaryDeviceType {
  157. private static final String DEMIRITA_WIFI_OUI = "0050F204";
  158. public static final int P2P_CATEGORY_PRINTER = 3;
  159. public static final int P2P_SUBCATEGORY_MFP = 5;
  160. public static final int P2P_SUBCATEGORY_PRINTER = 1;
  161. private int categoryId = 0;
  162. private int subCategoryId = 0;
  163. public static boolean isPrinter(String str) {
  164. PrimaryDeviceType parsePrimaryDeviceType = parsePrimaryDeviceType(str);
  165. if (parsePrimaryDeviceType == null || 3 != parsePrimaryDeviceType.categoryId) {
  166. return false;
  167. }
  168. int i = parsePrimaryDeviceType.subCategoryId;
  169. if (1 == i || 5 == i) {
  170. return true;
  171. }
  172. return false;
  173. }
  174. private static PrimaryDeviceType parsePrimaryDeviceType(String str) {
  175. PrimaryDeviceType primaryDeviceType = new PrimaryDeviceType();
  176. primaryDeviceType.categoryId = 0;
  177. primaryDeviceType.subCategoryId = 0;
  178. if (str == null || str.isEmpty()) {
  179. return primaryDeviceType;
  180. }
  181. str.toUpperCase(Locale.ENGLISH);
  182. String replaceAll = str.replaceAll("[^(0-9)^(A-Z)]", "");
  183. int indexOf = replaceAll.indexOf(DEMIRITA_WIFI_OUI);
  184. if (indexOf < 0) {
  185. return primaryDeviceType;
  186. }
  187. String substring = replaceAll.substring(0, indexOf);
  188. String substring2 = replaceAll.substring(indexOf + 8);
  189. try {
  190. primaryDeviceType.categoryId = Integer.parseInt(substring, 16);
  191. primaryDeviceType.subCategoryId = Integer.parseInt(substring2, 16);
  192. } catch (NumberFormatException e) {
  193. e.printStackTrace();
  194. }
  195. return primaryDeviceType;
  196. }
  197. }
  198. }