Skip to content

Commit

Permalink
add log
Browse files Browse the repository at this point in the history
  • Loading branch information
ihrthk committed Feb 3, 2015
1 parent 00acd27 commit 35487aa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-21.0.2" level="project" />
<orderEntry type="module" module-name="library" exported="" />
</component>
</module>
Expand Down
9 changes: 1 addition & 8 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/distributions" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
Expand All @@ -82,17 +80,12 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-21.0.2" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.2" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.2" level="project" />
<orderEntry type="library" exported="" name="android-wifi-connecter" level="project" />
<orderEntry type="library" exported="" name="android-wifi-connecter-1.0" level="project" />
</component>
</module>

Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class WiFiConnecter {

// Combo scans can take 5-6s to complete
private static final int WIFI_RESCAN_INTERVAL_MS = 5 * 1000;

private static final int MAX_TRY_COUNT = 3;
private static final String TAG = WiFiConnecter.class.getSimpleName();

private Context mContext;
private WifiManager mWifiManager;

private final IntentFilter mFilter;
private final BroadcastReceiver mReceiver;
private BroadcastReceiver mReceiver;
private final Scanner mScanner;
private ActionListener mListener;
private String mSsid;
Expand All @@ -53,15 +53,7 @@ public WiFiConnecter(Context context) {
mFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
handleEvent(context, intent);
}
};

context.registerReceiver(mReceiver, mFilter);
isRegistered = true;
mScanner = new Scanner();
}

Expand All @@ -78,6 +70,17 @@ public void connect(String ssid, String password, ActionListener listener) {
this.mSsid = ssid;
this.mPassword = password;

isRegistered = true;
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
handleEvent(context, intent);
}
};

Log.d(TAG, "registerReceiver:" + mReceiver.toString());
mContext.registerReceiver(mReceiver, mFilter);

if (listener != null) {
listener.onStarted(ssid);
}
Expand All @@ -92,15 +95,17 @@ private void handleEvent(Context context, Intent intent) {
//TODO zls:Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 }
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction()) && isActiveScan) {
isActiveScan = false;

List<ScanResult> results = mWifiManager.getScanResults();
Log.d("WiFiConnecter", "results:" + results.size());
Log.d(TAG, "scan results:" + results.toString());
for (ScanResult result : results) {
if (mSsid.equals(result.SSID)) {
// mScanner.pause();
Log.d(TAG, "find result:" + result);
//https://code.google.com/p/android-wifi-connecter/
// Loger.d("result.SSID=" +result.SSID+" newNetwork:"+newNetwork);
if (!connect(context, result, mPassword)) {
boolean connectResult = connect(context, result, mPassword);
Log.d(TAG, "connect result:" + connectResult);
if (!connectResult) {
if (mListener != null) {
mListener.onFailure();
mListener.onFinished(false);
Expand All @@ -119,7 +124,7 @@ private void handleEvent(Context context, Intent intent) {
if (mInfo.isConnected() && mWifiInfo != null && mWifiInfo.getSSID() != null &&
//nexus 5 have quote,sumsang don't have quote
(mWifiInfo.getSSID().equals(mSsid) ||
mWifiInfo.getSSID().equals(Wifi.convertToQuotedString(mSsid)))) {
mWifiInfo.getSSID().equals(Wifi.convertToQuotedString(mSsid)))) {
if (mListener != null) {
// Loger.d("success");
mListener.onSuccess(mWifiInfo);
Expand All @@ -146,6 +151,7 @@ public boolean connect(Context context, ScanResult mScanResult, String mPassword
WifiConfiguration config = Wifi.getWifiConfiguration(mWifiManager, mScanResult, security);

if (config == null) {
Log.d(TAG, "start connect new network");
connResult = newNetWork(context, mScanResult, mPassword);
} else {
final boolean isCurrentNetwork_ConfigurationStatus = config.status == WifiConfiguration.Status.CURRENT;
Expand All @@ -156,6 +162,7 @@ public boolean connect(Context context, ScanResult mScanResult, String mPassword
if (isCurrentNetwork_ConfigurationStatus || isCurrentNetwork_WifiInfo) {
connResult = true;
} else {
Log.d(TAG, "start connect configure network");
connResult = configuredNetwork(context, config, security, mPassword);
}
}
Expand Down Expand Up @@ -212,6 +219,7 @@ private boolean configuredNetwork(Context context, WifiConfiguration config, Str

public void onPause() {
if (isRegistered) {
Log.d(TAG, "unregisterReceiver:" + mReceiver.toString());
mContext.unregisterReceiver(mReceiver);
isRegistered = false;
}
Expand Down Expand Up @@ -249,6 +257,7 @@ void pause() {

@Override
public void handleMessage(Message message) {
Log.d(TAG, "mRetry:" + mRetry);
if (mRetry < MAX_TRY_COUNT) {
mRetry++;
isActiveScan = true;
Expand All @@ -258,6 +267,7 @@ public void handleMessage(Message message) {
}
//TODO startScan return false?
boolean startScan = mWifiManager.startScan();
Log.d(TAG, "startScan:" + startScan);
// exe scan fail(bind mechanism)
if (!startScan) {
if (mListener != null) {
Expand Down

0 comments on commit 35487aa

Please sign in to comment.