Connection Management

Soft-AP Connection

By clicking the ‘SOFT AP’ button, the sample application starts the ConnectionActivity class and checks whether the application is connected. The sample application starts device scanning by clicking the ‘START SCAN’ button. From the checkBleScanPermissionAndStartScan() method, the sample application checks required permissions, enables Wi-Fi and Bluetooth, requests the permissions if not granted and then starts device scanning by calling the startScanDevice() method which calls the ConnectionManager’s StartScanFriends() method to scan for Friends devices. Scanned devices are listed below the ‘START SCAN’ button. The following figure shows an example of device scan result:

 

android_image012.png 

 

By clicking the ‘STOP SCAN’ button, the sample application stops scanning devices by calling the stopScanDevice() method which calls the ConnectionManager’s StopScanFriends() method.

 

If any device name in the scanned list is selected, the sample application starts Wi-Fi connection with the chosen device by calling the ConnectionActivity’s connect() method which calls the ConnectionManager’s connect() method. If it’s the first time connecting to the selected camera device, the following pop-up is presented to ask for Wi-Fi password. The sample application saves the password information and uses this information in later connection attempts without challenging for Wi-Fi password.

 

android_image013.png android_image014.png

 

If the connection is successfully established, the application returns to the main UI and all the buttons get enabled.

 

The sample application returns to the Main screen when disconnected. If any download job is in progress when the connection is disconnected, the download job needs to be cancelled. The deleteUnfinishedDownloadFile() method of CameraFileListViewActivity deletes the file being downloaded. 

Over-AP Connection

The OverAPActivity class is responsible for Over-AP connection management. See the following figure for its layout:

 

android_overap_connection_ui.png 

 

Fill in the SSID and Password fields with plain texts and click ‘CONNECT OVERAP’ button. The connectOverAP() method is called and it handles Base 64 Encoding of parameters and then the sample application calls connect() method of ConnectionManager class to make Over-AP connection with the specified AP information.

 

private void connectOverAP() {
    String ssid_string = ssid.getText().toString();
    String ssid_passwd_string = passwd.getText().toString();
    OctopusManager.get(mContext).connect(ssid_string, ssid_passwd_string, 
ConnectionManager.OVER_AP);
    OctopusManager.get(mContext).registerListener(mConnectionCallback);
    mProgressDialog = ProgressDialog.show(mContext, "", "Processing...", 
true, false);
}

 

The connectOverAP() method also registers a Listeners.OverAP listener to handle Over-AP connection processes. Unregister the listener if the connection is disconnected or any error has occurred.

 

Listeners.OverAP mConnectionCallback = new Listeners.OverAP() {

    @Override
    public String asCallerId() {
        ... 
    }

    @Override
    public void onConnected(Bundle host) {
        ... 
    }

    @Override
    public void onDisconnected() {
        ... 
        OctopusManager.get(mContext).unregisterListener(mConnectionCallback);
    }

    @Override
    public void onResponse(Listeners.Type type, int code) {
        if (type == Listeners.Type.state) {
            if (code == ConnectionManager.Result.NSD_DISCOVERY_STARTED) {
// NSD Discovery Started
            } else if (code == ConnectionManager.Result.CONNECTED) {
                // Connected 
            }
        } else {
            // error 
            ...
OctopusManager.get(mContext).unregisterListener(mConnectionCallback);
        }
    }
};

 

The connection mode can be switched to Soft-AP mode. Click the ‘CONNECT SOFT AP’ button which calls the connectSoftAP() method. enableFriendWifiAP() is called to enable the Soft-AP mode of the camera device.

 

private void connectSoftAP(){
    FriendsCameraSharedPreference sharedPreference = 
new FriendsCameraSharedPreference(mContext);
    String prevBtAddr =
sharedPreference.getPreference(FriendsCameraSharedPreference.BT_ADDR);

    OctopusManager.getInstance(mContext).
getConnectionManager().enableFriendWifiAP(prevBtAddr);
}

 

The result of Soft-AP ON request is notified to the Listeners.SoftAP callback registered in the MainActivity. From onResponse(), the Soft-AP mode of the camera has successfully been enabled if ConnectionManager.Result.FRIEND_AP_ENABLED code is returned. Then call connect() method to connect to the camera in Soft-AP mode.

 

Listeners.SoftAP mSoftAPConnectionListener = new Listeners.SoftAP() {

    @Override
    public void onResponse(Listeners.Type type, int code) {
        if (type == Listeners.Type.state) {
            if (code == ConnectionManager.Result.WIFI_SCANNED) {
                ... 
            } else if (code == ConnectionManager.Result.FRIEND_AP_ENABLED) {
                if (...) {
                    FriendsCameraSharedPreference sharedPreference = 
new FriendsCameraSharedPreference(mContext);
                    String prevSsid = sharedPreference.getPreference(
FriendsCameraSharedPreference.SSID);
                    String prevPw = sharedPreference.getPreference(
FriendsCameraSharedPreference.PASSWORD);

                    OctopusManager.getInstance(mContext).getConnectionManager().
connect(prevSsid, prevPw, ConnectionManager.SOFT_AP);
                }
            }
            return;
        }else{
            ...
        }

    }
};
Navigation