Alternative Network

Last Updated on : 2024-07-15 09:45:16download

The alternative network feature provides the following capabilities:

  • Check support for alternative networks
  • Get the device’s current network information
  • Get the list of alternative networks
  • Update the list of alternative networks
  • Check support for automatic network switching
  • Check support for updating the alternative network list
  • Connect the device to a new Wi-Fi network other than the alternative networks
  • Connect the device to an alternative Wi-Fi network

Prerequisites

Get the management class for alternative networks

val manager = DeviceBusinessDataManager.getInstance().getDeviceNetSetManager(deviceId)

Check support for alternative networks

val isSupport = manager?.isSupportBackupNetwork()

Implement the alternative network service as shown below only when the device supports it.

Step 1: Get the current network information

Get information about the Wi-Fi network the device is connected to.

manager?.getDeviceCurrentNetInfo(object : IThingDataCallback<CurrentWifiInfoBean?> {
    override fun onSuccess(result: CurrentWifiInfoBean?) {

    }

    override fun onError(errorCode: String?, errorMessage: String?) {

    }

})

Step 2: Get the list of alternative networks

Get the list of alternative Wi-Fi networks.

manager?.getDeviceBackupWiFiList(object : IThingDataCallback<BackupWifiListInfo?> {
    override fun onSuccess(result: BackupWifiListInfo?) {

    }

    override fun onError(errorCode: String?, errorMessage: String?) {

    }

})

Step 3: Update the list of alternative networks

Updating the list of alternative networks applies only when the device supports this feature. When the maximum number of alternative networks is reached, no additional networks can be added. You can check this with the maxNum value returned in step 1.

Check support for updating the alternative network list

manager?.canUpdateDeviceBackupWiFiList(bean)

Update the alternative network list

To add an alternative Wi-Fi network, assemble the SSID, password, and hash value of the Wi-Fi network into a BackupWifiBean and then invoke the method updateDeviceBackupWiFiList.

// Assemble data
val backupWifiBean = BackupWifiBean()
backupWifiBean.ssid = ssid
backupWifiBean.passwd = pwd
backupWifiBean.hash =
    SHA256Utils.getBase64Hash(viewModel.deviceBean?.getLocalKey() + ssid + pwd)
backupWifiBeans.add(backupWifiBean)
//  Update the list
manager?.updateDeviceBackupWiFiList(backupWifiList,
    object : IThingDataCallback<BackupWifiResultBean?> {
        override fun onSuccess(result: BackupWifiResultBean?) {

        }

        override fun onError(errorCode: String?, errorMessage: String?) {

        }

    })

Step 4: Switch the device’s network automatically

Switching the device’s network automatically applies only when the device supports this feature.

Check support for automatic network switching

canSwitchDeviceWiFi.value = manager?.canSwitchDeviceWiFi(bean)

Switch the device’s network

Switch the device’s network to a new Wi-Fi network or an alternative one.

Switch to a new Wi-Fi network

manager?.switchToNewWifi(ssid, pwd, object : IThingDataCallback<SwitchWifiResultBean?> {
    override fun onSuccess(result: SwitchWifiResultBean?) {

    }

    override fun onError(errorCode: String?, errorMessage: String?) {

    }

})

Switch to an alternative Wi-Fi network

manager?.switchToBackupWifi(hash, object : IThingDataCallback<SwitchWifiResultBean?> {
    override fun onSuccess(result: SwitchWifiResultBean?) {

    }

    override fun onError(errorCode: String?, errorMessage: String?) {

    }

})

Demo

This topic describes the service implementation in the sequence of read and create operations. In real-world projects, you can implement them as needed. For more information, see the Alternative Network Module in the BizBundle SDK Demo.