From 3447e2c56096f24e8b32b36804bd1dba8dd69940 Mon Sep 17 00:00:00 2001 From: Gyeonghwan Hong Date: Sun, 5 Nov 2017 22:06:47 +0900 Subject: [PATCH] Fix Wi-fi direct discovery problem --- .../view/SensorViewerActivity.java | 3 +- .../WifiDirectDeviceController.java | 83 ++++++++++++++----- .../ant/cmfw/service/CommChannelService.java | 6 +- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/Ant-Manager/src/com/ant/ant_manager/view/SensorViewerActivity.java b/Ant-Manager/src/com/ant/ant_manager/view/SensorViewerActivity.java index 3af2a187..a6a8b5d4 100644 --- a/Ant-Manager/src/com/ant/ant_manager/view/SensorViewerActivity.java +++ b/Ant-Manager/src/com/ant/ant_manager/view/SensorViewerActivity.java @@ -235,8 +235,7 @@ class PrivateControllerBroadcastReceiver extends ANTControllerBroadcastReceiver this.setOnReceivedSensorDataListener(new OnReceivedDataFromTarget() { @Override public void onReceivedDataFromTarget(String listenerName, String data) { - Log.d(TAG, "Message coming for " + listenerName + ": " + data + " " + - listenerName.compareTo("sensorviewer") + "!!!"); + Log.d(TAG, "Message coming for " + listenerName + ": " + data); if (listenerName.compareToIgnoreCase("sensorviewer") == 0) { onSensorViewerMessage(data); } diff --git a/Ant-Manager/src/com/ant/cmfw/devicecontrollers/wifidirect/WifiDirectDeviceController.java b/Ant-Manager/src/com/ant/cmfw/devicecontrollers/wifidirect/WifiDirectDeviceController.java index 731a2cb1..9cb36522 100644 --- a/Ant-Manager/src/com/ant/cmfw/devicecontrollers/wifidirect/WifiDirectDeviceController.java +++ b/Ant-Manager/src/com/ant/cmfw/devicecontrollers/wifidirect/WifiDirectDeviceController.java @@ -24,6 +24,8 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.NetworkInfo; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; import android.net.wifi.WpsInfo; import android.net.wifi.p2p.WifiP2pConfig; import android.net.wifi.p2p.WifiP2pDevice; @@ -32,6 +34,7 @@ import android.util.Log; import java.util.ArrayList; +import java.util.List; public class WifiDirectDeviceController { static private String TAG = "WFDController"; @@ -84,14 +87,45 @@ private class ConnectProcedure { private Integer mConnectingResultListenerLock = 0; private boolean mIsConnecting = false; + private boolean mIsWifiDirectMode = true; + public void start(WifiDirectConnectingResultListener connectingResultListener) { this.mConnectingResultListener = connectingResultListener; - this.discoverPeers(); + if (mIsWifiDirectMode) { + this.discoverPeers(); + } else { + this.connectWiFiAP(); + } + } + + public void connectWiFiAP() { + String networkSSID = "DIRECT-CS-ANT"; + String networkPass = "12345670"; + + WifiConfiguration wifiConfig = new WifiConfiguration(); + wifiConfig.SSID = "\"" + networkSSID + "\""; + wifiConfig.preSharedKey = "\"" + networkPass + "\""; + + WifiManager wifiManager = (WifiManager) mService.getApplicationContext() + .getSystemService(Context.WIFI_SERVICE); + wifiManager.addNetwork(wifiConfig); + + List foundConfigs = wifiManager.getConfiguredNetworks(); + for (WifiConfiguration foundConfig : foundConfigs) { + if (foundConfig.SSID != null & foundConfig.SSID.equals("\"" + networkSSID + "\"")) { + wifiManager.disconnect(); + wifiManager.enableNetwork(foundConfig.networkId, true); + wifiManager.reconnect(); + this.onSuccess(); + break; + } + } + this.onFail(); } + // Step 1. Discover peers private void discoverPeers() { - // Step 1. Discover peers IntentFilter wifiP2PIntentFilter; wifiP2PIntentFilter = new IntentFilter(); wifiP2PIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); @@ -122,8 +156,8 @@ public void onReceive(Context context, Intent intent) { } } + // Step 2. Request peer list private void onPeerChanged() { - // Step 2. Request peer list Log.d(TAG, "Request peer list"); mWifiP2pManager.requestPeers(mWifiP2pManagerChannel, new WifiDirectPeerListListener()); } @@ -135,9 +169,10 @@ public void onPeersAvailable(WifiP2pDeviceList peerDeviceList) { } } + // Step 3. Check the given peer list private void onPeerListReceived(WifiP2pDeviceList peerDeviceList) { - // Prevent duplicated connection tries - if(this.mIsConnecting) { + // Step 3-1. Drop duplicated connection tries + if (this.mIsConnecting) { return; } else { Log.d(TAG, "Peer List Received"); @@ -151,16 +186,15 @@ private void onPeerListReceived(WifiP2pDeviceList peerDeviceList) { return; } - // Step 3. Check if there is peer device that we want + // Step 3-2. Check if there is peer device that we want for (WifiP2pDevice peerDevice : peerDeviceList.getDeviceList()) { if (peerDevice.deviceName.compareTo(mWifiDirectName) == 0) { - Log.d(TAG, "Found Wi-fi Direct device: " + mWifiDirectName + " / " + - peerDevice.deviceAddress); + Log.d(TAG, "Found Wi-fi peer device: name=" + mWifiDirectName + " / address=" + + peerDevice.deviceAddress + " / status=" + peerDevice.status); if (peerDevice.status == WifiP2pDevice.AVAILABLE || peerDevice.status == WifiP2pDevice.CONNECTED || peerDevice.status == WifiP2pDevice.INVITED) { Log.d(TAG, "Connecting Wi-fi Direct device: " + mWifiDirectName); requestConnection(peerDevice); - mWifiP2pManager.stopPeerDiscovery(mWifiP2pManagerChannel, null); } else { Log.d(TAG, "Not yet initiated: " + mWifiDirectName + " / " + peerDevice .status); @@ -173,15 +207,14 @@ private void onPeerListReceived(WifiP2pDeviceList peerDeviceList) { this.mIsConnecting = false; } + // Step 4. Request for connecting to the peer device private void requestConnection(WifiP2pDevice peerDevice) { - final WifiP2pDevice kPeerDevice = peerDevice; - // Step 4. Request for connecting to the peer device WifiP2pConfig wifiP2pConfig = new WifiP2pConfig(); - wifiP2pConfig.deviceAddress = kPeerDevice.deviceAddress; - if (kPeerDevice.wpsPbcSupported()) { + wifiP2pConfig.deviceAddress = peerDevice.deviceAddress; + if (peerDevice.wpsPbcSupported()) { wifiP2pConfig.wps.setup = WpsInfo.PBC; Log.d(TAG, "WPS: PBC"); - } else if (kPeerDevice.wpsKeypadSupported()) { + } else if (peerDevice.wpsKeypadSupported()) { wifiP2pConfig.wps.setup = WpsInfo.KEYPAD; wifiP2pConfig.wps.pin = "12345670"; Log.d(TAG, "WPS:KeyPad"); @@ -190,7 +223,10 @@ private void requestConnection(WifiP2pDevice peerDevice) { Log.d(TAG, "WPS:Display"); } - Log.d(TAG, "Request to connect Wi-fi connection"); + // Start to watch Bluetooth device's status + mState.startToWatchDeviceState(); + + Log.d(TAG, "Request to connect Wi-fi Direct connection"); mWifiP2pManager.connect(mWifiP2pManagerChannel, wifiP2pConfig, new WifiP2pManager .ActionListener() { @Override @@ -198,6 +234,7 @@ public void onSuccess() { new Thread() { @Override public void run() { + Log.d(TAG, "Wi-fi Direct Connection Success"); ConnectProcedure.this.onSuccess(); } }.start(); @@ -205,7 +242,7 @@ public void run() { @Override public void onFailure(int reason) { - onFail(); + Log.d(TAG, "Wi-fi Direct Connection Fail"); } }); } @@ -249,7 +286,7 @@ public void transitToConnected() { this.mIsConnected = true; // Start to watch Bluetooth device's status - this.startToWatchDeviceState(); + //this.startToWatchDeviceState(); // Notify Wi-fi direct device connection event to listeners for (WifiDirectDeviceStateListener listener : this.mDeviceStateListeners) { @@ -269,7 +306,7 @@ public void transitToDisconnected() { } } - private void startToWatchDeviceState() { + public void startToWatchDeviceState() { IntentFilter filter = new IntentFilter(); filter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); this.mWifiDirectDeviceStatusReceiver = new WifiDirectDeviceStatusReceiver(); @@ -291,9 +328,13 @@ public void onReceive(Context context, Intent intent) { if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.compareTo(action) == 0) { NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra (WifiP2pManager.EXTRA_NETWORK_INFO); - if (!networkInfo.isConnectedOrConnecting() || !networkInfo.isAvailable()) { - transitToDisconnected(); - } + Log.d(TAG, "Wi-fi Direct state change listener: Android-side state=" + + networkInfo.getState()); +// if (!networkInfo.isConnectedOrConnecting() || !networkInfo.isAvailable()) { +// Log.d(TAG, "Transit wi-fi direct state to disconnected: Android-side " + +// "state=" + networkInfo.getState()); +// transitToDisconnected(); +// } } } } diff --git a/Ant-Manager/src/com/ant/cmfw/service/CommChannelService.java b/Ant-Manager/src/com/ant/cmfw/service/CommChannelService.java index 02075a80..8f724d98 100644 --- a/Ant-Manager/src/com/ant/cmfw/service/CommChannelService.java +++ b/Ant-Manager/src/com/ant/cmfw/service/CommChannelService.java @@ -412,7 +412,7 @@ public void onConnectingWifiDirectDeviceFail() { private void openLargeDataPort() { // (Enable Largedata) Step 3. Open largedata port - int kSleepMillisecs = 500; + int kSleepMillisecs = 1000; int kMaxTries = 10; boolean isOpeningSuccess = false; int tries = 0; @@ -451,7 +451,7 @@ private void onSuccess() { } private void onFail() { - disableLargeDataMode(); + //disableLargeDataMode(); } } @@ -501,7 +501,7 @@ public void run() { Log.d(TAG, "No users detected for large data port over " + kThresholdMillisecs + " ms."); - disableLargeDataMode(); + //disableLargeDataMode(); } }