void Start(String ssid, StarwispActivity c, String name, StarwispBuilder b) { Log.i("starwisp", "Network startup!"); m_CallbackName = name; m_Context = c; m_Builder = b; if (state == NetworkManager.State.IDLE) { Log.i("starwisp", "State is idle, launching scan"); wifi = (WifiManager) c.getSystemService(Context.WIFI_SERVICE); state = State.SCANNING; SSID = ssid; wifi.startScan(); receiver = new WiFiScanReceiver(SSID, this); c.registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); // todo - won't work from inside fragments m_Builder.DialogCallback(m_Context, m_Context.m_Name, m_CallbackName, "\"Scanning\""); } if (state == NetworkManager.State.CONNECTED) { Log.i("starwisp", "State is connected, callback raised"); m_Builder.DialogCallback(m_Context, m_Context.m_Name, m_CallbackName, "\"Connected\""); } }
private void ReadStream(ReqMsg m) { InputStream in = m.m_Stream; BufferedReader reader = null; try { if (m.m_Type.equals("download")) { ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); // this is storage overwritten on each iteration with bytes int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; // we need to know how may bytes were read to write them to the byteBuffer int len = 0; Toast.makeText(m_Context, "Starting download for " + m.m_CallbackName, Toast.LENGTH_LONG) .show(); while ((len = in.read(buffer)) != -1) { byteBuffer.write(buffer, 0, len); } Toast.makeText(m_Context, "Finished downloading " + m.m_CallbackName, Toast.LENGTH_LONG) .show(); m_Builder.SaveData(m.m_CallbackName, byteBuffer.toByteArray()); } else { // results in evaluating data read from via http - fix if used from net reader = new BufferedReader(new InputStreamReader(in)); String line = ""; String all = ""; while ((line = reader.readLine()) != null) { all += line + "\n"; } // Log.i("starwisp","got data for "+m.m_CallbackName+"["+all+"]"); synchronized (mLock) { m_Builder.DialogCallback(m_Context, m_Context.m_Name, m.m_CallbackName, all); } } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
void Connect() { Log.i("starwisp", "Attemping connect to " + SSID); List<WifiConfiguration> list = wifi.getConfiguredNetworks(); Boolean found = false; for (WifiConfiguration i : list) { if (i.SSID != null && i.SSID.equals("\"" + SSID + "\"")) { found = true; Log.i("starwisp", "Connecting (state=connected)"); state = State.CONNECTED; wifi.disconnect(); wifi.enableNetwork(i.networkId, true); wifi.reconnect(); Log.i("starwisp", "Connected"); try { Thread.sleep(2000); Log.i("starwisp", "trying post-connection callback"); m_Builder.DialogCallback(m_Context, m_Context.m_Name, m_CallbackName, "\"Connected\""); } catch (Exception e) { Log.i("starwisp", e.toString()); e.printStackTrace(); } break; } } if (!found) { Log.i("starwisp", "adding wifi config"); WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + SSID + "\""; // conf.wepKeys[0] = "\"" + networkPass + "\""; // conf.wepTxKeyIndex = 0; // conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifi.addNetwork(conf); } }