AP Mode

Last Updated on : 2023-06-01 06:19:39download

This topic describes the access point (AP) or hotspot mode to pair devices. It is a connection capability for pairing over Wi-Fi. After the mobile phone is connected to the Wi-Fi hotspot of the device to be paired, the app is paired and communicates with the device over Wi-Fi. This pairing mode features a high success rate and excellent reliability, and supports routers that can process data using both 2.4 GHz and 5 GHz frequencies. Users must manually change the Wi-Fi band on their mobile phones to enable pairing.

Pairing process

Process of pairing in AP mode

AP Mode

Initialize pairing parameters

API description

ActivatorBuilder builder = new ActivatorBuilder()
		.setContext(context)
		.setSsid(ssid)
		.setPassword(password)
		.setActivatorModel(ActivatorModelEnum.TY_AP)
		.setTimeOut(timeout)
		.setToken(token)
		.setListener(new IThingSmartActivatorListener() {

				@Override
				public void onError(String errorCode, String errorMsg) {

				}

				@Override
				public void onActiveSuccess(DeviceBean devResp) {

				}

				@Override
				public void onStep(String step, Object data) {

				}
			}
		));

Parameters

Parameter Description
token The pairing token.
context The context to be set in activity.
ssid The name of the Wi-Fi network to which a paired device is connected.
password The password of the Wi-Fi network to which a paired device is connected.
activatorModel The pairing mode. For example, the value is set to ActivatorModelEnum.TY_AP to represent the AP mode.
timeout The timeout value of a pairing task. Default value: 100. Unit: seconds.

Get a token

Before the AP pairing process, the SDK must get a pairing token from the cloud in the networked state. The token is valid for 10 minutes and expires immediately after the device is paired. A new token must be generated if the device needs to be paired again.

API description

ThingHomeSdk.getActivatorInstance().getActivatorToken(homeId,
		new IThingActivatorGetToken() {

			@Override
			public void onSuccess(String token) {

			}

			@Override
			public void onFailure(String s, String s1) {

			}
		});

Parameters

Parameter Description
homeId The home ID. For more information, see Home Management.

Call the pairing method

IThingActivator mThingActivator = ThingHomeSdk.getActivatorInstance().newActivator(builder);
// Starts pairing.
mThingActivator.start();
// Stops pairing.
mThingActivator.stop();
// Exits the page and destroys certain caches and listeners.
mThingActivator.onDestroy();

Pairing optimization process

In order to improve the distribution network and reduce the reduction, sdk user user distribution network success rate distribution network success rate distribution network distribution network distribution network distribution network distribution network success rate success rate A set of nets New new new process distribution network distribution network.

Corresponding device firmware: TuyaOS 3.6.1, 3.7.0 and later versions

AP Mode

Get security configuration list

 ThingHomeSdk.getActivatorInstance().getDeviceSecurityConfigs(new IThingDataCallback<String>() {
            @Override
            public void onSuccess(String result) {
                securityConfig = result;
                //get success
                
            }

            @Override
            public void onError(String errorCode, String errorMessage) {
               
            }
        });

API description

ThingApActivatorBuilder builder = new ThingApActivatorBuilder().setContext(this);

Parameters

Parameter Description
context The context to be set in activity.

example

IThingActivator mThingActivator = ThingHomeSdk.getActivatorInstance().newOptimizedActivator(builder);

Query the WIFI list from the device

The SDK will first query the status of the device at this time. If it is in an abnormal state, such as a state that the SDK cannot recognize, it will not query the WIFI list.

ApQueryBuilder queryBuilder = new ApQueryBuilder.Builder().setContext(context).setTimeout(queryParam.timeout == null ? 0 : queryParam.timeout * 1000).build();
mThingActivator.queryDeviceConfigState(queryBuilder, new IThingResultCallback<List<WiFiInfoBean>>() {
        @Override
        public void onSuccess(List<WiFiInfoBean> result) {
            //get wifi list success  
        }

        @Override
        public void onError(String errorCode, String errorMessage) {
            //get wifi list fail
        }
    });

Parameters

ApQueryBuilder

Parameter Description
context The context to be set in activity
imeout The timeout value of a pairing task. Unit: ms

WiFiInfoBean

Parameter Description
ssid The wifi name scanned by the device
rssi wifi signal
sec wifi protocol value
The mapping relationship is as follows
0 -> WAAM_OPEN
1 -> WAAM_WEP
2 -> WAAM_WPA_PSK
3 -> WAAM_WPA2_PSK
4 -> WAAM_WPA_WPA2_PSK
5 -> WAAM_WPA_WPA3_SAE
6 -> WAAM_UNKNOWN
after get wifi list
If the user chooses to specify wifi, the password entered by the user can be judged in advance according to the protocol
For example, if 2<=sec<=5, the length of the password is not less than 8 digits to reduce the error rate.

Start Pairing

The wifi list can be found according to the query, allowing users to choose

ApActivatorBuilder builder = new ApActivatorBuilder.Builder()
        .setSsid("ssid")
        .setPwd("password")
        .setTimeout(timeout)
        .setToken(token)
        .setSecurityConfig(securityConfig)
        .setListener(new IThingSmartActivatorListener() {
            @Override
            public void onError(String errorCode, String errorMsg) {
                //pair fail
                //errorCode See the introduction at the end of the article

			}
            
            @Override
            public void onActiveSuccess(DeviceBean devResp) {
                //pair success
			}

            @Override
            public void onStep(String step, Object data) {
            }
        }).build();
mThingActivator.startActivator(builder);

Parameters

ApActivatorBuilder

Parameter Description
ssid The name of the Wi-Fi network to which a paired device is connected.
password The password of the Wi-Fi network to which a paired device is connected.
token The pairing token.
timeout The timeout value of a pairing task. Unit: ms.
securityConfig get the security configuration list (optional) mainly to configure the security level information for the device.

Recover Pairing

Generally, when the password is wrong, you can use the recovery configuration network to re-input the wifi name and password to restore the configuration network
.
During the device pair configuration process, the SDK will automatically connect to the AP hotspot within the specified time. If connected, the SDK will internally obtain the device status and report it to the business side. (Only valid for new device firmware).

ApHandlerBean bean = new ApHandlerBean.Builder()
               .setSsid("ssid")
               .setPassword("password")
               .build();
mThingActivator.resumeAPConfigWifi(bean);

Parameters

ApHandlerBean

Parameter Description
ssid The name of the Wi-Fi network to which a paired device is connected.
password The password of the Wi-Fi network to which a paired device is connected.

停止配网

After the distribution network is stopped, or when resources are destroyed, it is necessary to manually call the end

mThingActivator.stopActivator();

Error Codes

The error code is strongly associated with the device connection, and the device information error is obtained from the device

Error code Description
207201 Failed to establish a connection channel with the device.
207206 Query the status of the device, and the device returns an unrecognizable status, that is, an abnormal status.
207207 The device does not support the function of obtaining the wifi list.
207209 After the device receives the distribution network information, the distribution network information is wrong.
207210 After the device receives the distribution network information, it cannot find the router.
207211 After the device receives the configuration information, the password is wrong.
207212 After the device receives the configuration information, it fails to connect to the router.
207213 After the device receives the configuration information, the DHCP fails.
207214 Error in activation of device-to-cloud information.
207215 Failed to connect the device to the cloud.
207216 Failed to activate the interface on the device.
207218 The request to the device’s cloud-based interface failed.
207219 The device is activated, and the connection with cloud iot-dns fails.
207220 pairing timeout.
207222 Failed to obtain device wifi list information.