/** 发送扫描结果 */ private void sendWifiResult() { if (mScanListener != null) { if (preWifiListDatas != null && !preWifiListDatas.equals(listWifiDevices)) { preWifiListDatas.clear(); preWifiListDatas.addAll(listWifiDevices); mScanListener.onWifiResult(listWifiDevices); } else if (preWifiListDatas == null) { preWifiListDatas = new ArrayList<Device>(); preWifiListDatas.addAll(listWifiDevices); mScanListener.onWifiResult(listWifiDevices); } listWifiDevices.clear(); } }
private void sendResult() { if (mScanListener != null) { listDevices = fixListDevices(listDevices, listMulticasts); if (preListDatas != null && !preListDatas.equals(listDevices)) { preListDatas.clear(); preListDatas.addAll(listDevices); mScanListener.onResult(listDevices); } else if (preListDatas == null) { preListDatas = new ArrayList<Device>(); preListDatas.addAll(listDevices); mScanListener.onResult(listDevices); } if (satus != DeviceUtil.checkDevice(mWipicoClient.getDevice(), listDevices)) { // 检查是否掉线 satus = DeviceUtil.checkDevice(mWipicoClient.getDevice(), listDevices); mScanListener.deviceStatus(satus); } } }
@Override public void run() { // TODO Auto-generated method stub while (running) { if (isDebug) Log.i(TAG, "5s扫描设备>>>>>>>>>>>>" + Thread.currentThread()); receive = 50; // 最大接收数量 isSend = false; String recestr = null; try { listDevices.clear(); sendSocket = new DatagramSocket(); sendSocket.setBroadcast(true); sendSocket.setSoTimeout(3000); sendSocket.send(sendPacket); while (receive-- > 0) { sendSocket.receive(recePacket); recestr = new String(recedata, recePacket.getOffset(), recePacket.getLength()); if (recestr.startsWith(DISCOVERY_RESP)) { Device device = new Device(); device.setDeviceName(recestr.substring(9)); device.setDeviceIp(recePacket.getAddress().getHostAddress()); if (listDevices.indexOf(device) < 0) { listDevices.add(device); if (device.getDeviceIp().equals("10.8.8.1")) { isSend = true; if (isDebug) Log.i(TAG, "扫描到10.8.8.1" + listDevices + "|" + Thread.currentThread()); sendResult(); break; } } } // 每次接收完UDP数据后,重置长度。否则可能会导致下次收到数据包被截断。 recePacket.setLength(default_bufferSize); } } catch (SocketTimeoutException e) { if (isDebug) Log.i(TAG, "扫描3s超时" + listDevices + "|" + Thread.currentThread()); if (!isSend) { sendResult(); } } catch (SocketException e) { receive = 0; if (isDebug) Log.i(TAG, "SocketException>>>>>>" + Thread.currentThread()); if (mScanListener != null) { mScanListener.scanException(); } } catch (IOException e) { receive = 0; if (isDebug) Log.i(TAG, "SocketException>>>>>>" + Thread.currentThread()); if (mScanListener != null) { mScanListener.scanException(); } } sendSocket.close(); if (wipicoVersion >= 2) { // 版本2才进行SSID 扫描 List<ScanResult> listScanResults = WifiAdmin.getInstance(mContext).getScanResults(); if (listScanResults != null) { for (ScanResult bean : listScanResults) { if (DeviceUtil.isMatchSSID(DeviceUtil.ssidCorrect(bean.SSID))) { listWifiDevices.add( new Device(DeviceUtil.ssidCorrect(bean.SSID), bean.capabilities, null)); } } } sendWifiResult(); } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }