Device Pairing UI BizBundle

Last Updated on : 2023-05-22 06:38:26download

Smart Life Device Pairing UI BizBundle is used to implement pairing guidance and smart device activation. It applies to multiple common types of smart devices, such as Wi-Fi devices, Zigbee devices, Bluetooth devices, and devices that support QR code scanning like GPRS & NB-IoT devices. The UI BizBundle provides the service logic and UI encapsulation to guide pairing and activate smart devices over different protocols.

Feature overview

Wi-Fi device pairing

Wi-Fi smart devices can be paired and connected to cloud services in two modes: Wi-Fi Easy Connect (EZ) mode and access point (AP) or hotspot mode. IP cameras (IPCs) can be paired after QR code scanning.

Term Description
Wi-Fi EZ mode Also known as the EZ mode. This pairing mode is implemented through the following steps:
  1. The app encapsulates the pairing data into the specified section of an IEEE 802.11 packet and sends the packet to the network.
  2. The Wi-Fi module of a smart device runs in promiscuous mode and listens for and captures all packets over the network.
  3. The Wi-Fi mode parses the packets that carry the pairing data from the app into the data format specified by the protocol.
AP mode Also known as the hotspot mode. A mobile phone acts as a station (STA) and connects to the hotspot of a smart device. Then, both devices are paired to establish a socket connection between them and exchange data through the specified ports.
QR code mode An IPC scans the QR code on the app to get the pairing data.

Zigbee device pairing

Zigbee gateways and sub-devices can be paired.

Term Description
Zigbee gateway The device that integrates the coordinator with Wi-Fi features on a Zigbee network. The gateway is responsible to formulate the Zigbee network and store data.
Zigbee sub-device A router or a terminal on a Zigbee network. Each Zigbee sub-device is responsible to forward data, or respond to terminal control commands.

Bluetooth device pairing

Tuya provides the following Bluetooth solutions.

Term Description
Bluetooth Low Energy (LE) A peer-to-peer connection is created between a Bluetooth or Bluetooth LE device and a mobile phone.
Bluetooth mesh Enable many-to-many (m:m) device communications over a mesh network released by Bluetooth Special Interest Group (SIG).
Tuya mesh Tuya’s proprietary technology that enables Bluetooth devices to communicate over a mesh network.
Combo devices The devices that support Bluetooth and Wi-Fi combo can be paired over either Bluetooth or Wi-Fi.

QR code pairing

A smart device that supports this pairing mode is connected to Tuya’s cloud services immediately after power on. The app can be used to scan the QR code on the device to implement cloud-based device activation and binding. The QR code must comply with Tuya’s QR code rules. For more information, contact Tuya’s account manager.

Term Description
GPRS devices Smart devices that apply GPRS technologies to connect to the network and access cloud services.
NB-IoT devices Smart devices that apply NB-IoT technologies.

Pairing by auto discovery

Tuya provides efficient pairing features on top of Tuya’s generic pairing technologies for smart devices.

Integrate with the UI BizBundle

Add the components of the Device Pairing UI BizBundle to the Podfile and run the command pod update.

source 'https://github.com/tuya/tuya-private-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do
  # Adds the Device Pairing UI BizBundle.
  pod 'TuyaSmartActivatorBizBundle'
end

To implement pairing of Wi-Fi devices, you must get the name of the current Wi-Fi network after the location permissions are enabled in your project. To achieve this purpose, add the following permission declaration to info.plist of your project, create the CLLocationManager sample, and then call the method requestWhenInUseAuthorization:

NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

To implement QR code scanning, the permissions on access to cameras of mobile phones are required. You can add the following permission declaration in info.plist:

NSCameraUsageDescription

Customize settings

Bluetooth device pairing

The Device Pairing UI BizBundle supports device pairing over protocols such as Wi-Fi and Bluetooth. Bluetooth pairing is optional.

  • If your app does not require Bluetooth pairing, you only need to set the needBle property in ty_custom_config.json to false.

  • If your app requires Bluetooth pairing, you must first add the following Bluetooth permission declaration to info.plist of your project, set the needBle property in ty_custom_config.json to true, and then add the following dependencies to your project:

    System permissions

    NSBluetoothAlwaysUsageDescription
    NSBluetoothPeripheralUsageDescription
    

    Settings

    {
    	"config": {
    		"appId": 123,
    		"tyAppKey": "xxxxxxxxxxxx",
    		"appScheme": "tuyaSmart",
    		"hotspotPrefixs": ["SmartLife"],
    		"needBle": true // Sets the value to `true` to support pairing of Bluetooth devices.
    	},
    "colors":{
    		"themeColor": "#FF5A28",
    	}
    }
    

    Dependencies

    pod 'TYBLEInterfaceImpl'
    pod 'TYBLEMeshInterfaceImpl'
    pod 'TYBLEHomeManager'
    pod 'TuyaSmartBLEKit'
    pod 'TuyaSmartBLEMeshKit'
    

Hotspot name settings

  • By default, the hotspot name of each Powered by Tuya (PBT) device is prefixed with SmartLife.

  • If the hotspot prefix of the current smart device is modified, you must set the hotspotPrefixs property in ty_custom_config.json to the current hotspot prefix.

    {
    	"config": {
    		"appId": 123,
    		"tyAppKey": "xxxxxxxxxxxx",
    		"appScheme": "tuyaSmart",
    		"hotspotPrefixs": ["SL"], // The supported hotspot prefix is set to `SL`.
    		"needBle": true
    	},
    "colors":{
    		"themeColor": "#FF5A28",
    	}
    }
    

Implement service protocols

Provide services

The UI BizBundle relies on the implementation of the protocol TYActivatorProtocol to provide services. You can view the TYActivatorProtocol.h file in the TYModuleServices component.

#ifndef TYActivatorProtocol_h
#define TYActivatorProtocol_h

typedef NS_ENUM(NSUInteger, TYActivatorCompletionNode) {
    TYActivatorCompletionNodeNormal
};

@class TuyaSmartHome;

@protocol TYActivatorProtocol <NSObject>

/**
 * Start config
 * Goto device config list view
 */
- (void)gotoCategoryViewController;

/**
 *  Obtain device information after each device connection
 *  @param node completion node, default TYActivatorCompletionNodeNormal
 *  @param custionJump default false, set true for process not need to jump to de device panel
 */
- (void)activatorCompletion:(TYActivatorCompletionNode)node customJump:(BOOL)customJump completionBlock:(void (^)(NSArray * _Nullable deviceList))callback;

@end
#endif /* TYActivatorProtocol_h */

To customize the returned list of categories or products for pairing, implement the protocol methods provided by TYActivatorExternalExtensionProtocol.

TYActivatorExternalExtensionProtocol

/**
 *  Back action form category View Controller
 *  Need to implement when additional operations are needed
 */
- (BOOL)categoryViewControllerCustomBackAction;

Depend on services

The UI BizBundle depends on the method provided by the protocol TYSmartHomeDataProtocol to get the current home details required for pairing. To call the UI BizBundle, you must implement this dependent protocol.

/**
 Returns the current home. If the current user does not have a home, `nil` is returned.

 @return TuyaSmartHome
 */
- (TuyaSmartHome *)getCurrentHome;

Usage instruction

Things to note

  1. Before the call of any API method, make sure that the user has logged in to the app.

  2. Before the UI BizBundle is used, the getCurrentHome method provided by the protocol TYSmartHomeDataProtocol must be implemented first.

    ObjC:

    #import <TuyaSmartBizCore/TuyaSmartBizCore.h>
    #import <TYModuleServices/TYSmartHomeDataProtocol.h>
    
    - (void)initCurrentHome {
    	// Registers the protocols to be implemented.
    	[[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYSmartHomeDataProtocol) withInstance:self];
    }
    
    // Implements the protocol method.
    - (TuyaSmartHome *)getCurrentHome {
    	TuyaSmartHome *home = [TuyaSmartHome homeWithHomeId:@"Current home ID"];
    	return home;
    }
    

    Swift:

    import TuyaSmartDeviceKit
    
    class TYActivatorTest: NSObject,TYSmartHomeDataProtocol{
    
    	func test() {
    		TuyaSmartBizCore.sharedInstance().registerService(TYSmartHomeDataProtocol.self, withInstance: self)
    	}
    
    	func getCurrentHome() -> TuyaSmartHome! {
    		let home = TuyaSmartHome.init(homeId: 111)
    		return home
    	}
    
    }
    

Enter the pairing mode

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYActivatorProtocol.h>

- (void)gotoDeviceConfig {
    id<TYActivatorProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYActivatorProtocol)];
    [impl gotoCategoryViewController];

    // Returns the pairing result.
    [impl activatorCompletion:TYActivatorCompletionNodeNormal customJump:NO completionBlock:^(NSArray * _Nullable deviceList) {
        NSLog(@"deviceList: %@",deviceList);
    }];
}

Swift:

let impl = TuyaSmartBizCore.sharedInstance().service(of: TYActivatorProtocol.self) as? TYActivatorProtocol
impl?.gotoCategoryViewController()

impl?.activatorCompletion(TYActivatorCompletionNodeNormal, customJump: false, completionBlock: { (evIdList:[Any]?) in
            print(devIdList ?? [])
        })

Get custom categories for pairing

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYActivatorExternalExtensionProtocol.h>

- (void)initCurrentHome {
    // Registers the protocol to be implemented.
    [[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYActivatorExternalExtensionProtocol) withInstance:self];
}

// Implements the protocol method.
- (BOOL)categoryViewControllerCustomBackAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
    return YES;
}

Swift:

class TYActivatorTest: NSObject,TYActivatorExternalExtensionProtocol{

    func test() {
 TuyaSmartBizCore.sharedInstance().registerService(TYActivatorExternalExtensionProtocol.self, withInstance: self)
    }

    func categoryViewControllerCustomBackAction() -> Bool {
        self.navigationController?.popToRootViewController(animated: true)
        return true;
    }

}