Wi-Fi and Bluetooth Combo Pairing

Last Updated on : 2024-09-03 07:51:38download

Preparation

  • System requirements: Android 4.3 or later. Smart Industry App SDK works with Android 6.0 and later.

  • Manifest permissions:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
    <!-- Android 12 added -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    
  • Bluetooth permission precheck: It is performed before Bluetooth scanning and connection. Otherwise, the app cannot use Bluetooth as expected.

    • Before scanning and connection, the app checks if it is allowed to access location services.
    • Check if the phone’s Bluetooth is turned on.

      Smart Industry App SDK does not provide the logic for precheck. You can implement it on your own.

Initialize IActivator

Parameters

Parameter Type Required Description
mode ActivatorMode Yes The pairing mode.

Example

BLEWIFIActivator blewifiActivator = (BLEWIFIActivator) ActivatorService.activator(ActivatorMode.BLE_WIFI);

Initialize device scanning IDiscovery

Parameters

Parameter Type Required Description
mode DiscoveryMode Yes The scanning mode.

Example

IDiscovery iDiscovery = ActivatorService.discovery(DiscoveryMode.BLE_WIFI);

Device scanning parameters

Example

DiscoveryParams.Builder builder = new DiscoveryParams.Builder();
builder.setTimeout(600_000);
DiscoveryParams discoveryParams = new DiscoveryParams(builder);
iDiscovery.setParams(discoveryParams);

Register IDiscoveryListener to listen for scanning result

Parameters

IBluetoothDevice data model

Parameter Type Description
getId Function Get the ID of the Bluetooth device.
getName Function Get the name of the Bluetooth device.
getData Function Get the device data.
getConfigType Function Get the configuration type.
  • config_type_single: Bluetooth LE device
  • config_type_wifi: Bluetooth and Wi-Fi combo device
getAddress Function Get the Bluetooth address of the device.
getDeviceType Function Get the type of the device.
getUUID Function Get the UUID of the device.
getMAC Function Get the MAC address of the device.
getProductId Function Get the product ID for the device.
isBind Function Determine if the device has been bound.
getFlag Function
  • bit0: indicates whether a combo device supports the 5 GHz Wi-Fi network.
  • bit2: indicates whether a shared device is used.
  • bit3: indicates whether to support pairing over Bluetooth due to unavailable Wi-Fi connections.
  • bit4: indicates whether QR code scanning is required to pair the device.

getDeviceType indicates the type of device to be paired.

Value of getDeviceType getConfigType Device type
200 config_type_single Bluetooth device
300 config_type_single Bluetooth device
301 config_type_wifi Wi-Fi and Bluetooth combo device
304 config_type_wifi Wi-Fi and Bluetooth combo device that supports pairing over Bluetooth if a Wi-Fi connection is unavailable
400 config_type_single Bluetooth device
401 config_type_wifi Wi-Fi and Bluetooth combo device
404 config_type_wifi Wi-Fi and Bluetooth combo device that supports pairing over Bluetooth if a Wi-Fi connection is unavailable

Example

 IDiscovery iDiscovery = ActivatorService.discovery(DiscoveryMode.BLE_WIFI);
        iDiscovery.setListener(new IDiscoveryListener() {
            @Override
            public void didDiscover(@NonNull IDiscoveryDevice iDiscoveryDevice) {
                if (iDiscoveryDevice instanceof IBluetoothDevice){
                    ((IBluetoothDevice) iDiscoveryDevice).getAddress();
                    ((IBluetoothDevice) iDiscoveryDevice).getId();
                    ((IBluetoothDevice) iDiscoveryDevice).getDeviceType();
                }

            }
        });

Device scanning

// Start scanning.
iDiscovery.startDiscovery();
// Stop scanning.
iDiscovery.stopDiscovery();

Initialize pairing parameters

Get the parameters from the result of the scanning listener.

Parameters

Parameter Type Required Description
address String Yes The address of the Bluetooth device.
assetId String Yes The ID of the specified asset or space.
token String Yes The token of the device.
pwd String No The password of the Wi-Fi network.
uuid String Yes The UUID of the device.
ssid String No The SSID of the Wi-Fi network.
mac String No The MAC address of the device.
time long No The activation timeout, in milliseconds.
phase1Timeout long No The timeout for device activation and pairing in the cloud, default value: 60000, in milliseconds.
onlyConnectBle Boolean No Indicates whether to connect and activate the device over Bluetooth only, default value: false.
BLEWIFIActivatorParams params = new BLEWIFIActivatorParams.Builder()
                .setAddress("address")
                .setAssetId("assetId")
                .setToken("token")
                .setPwd("pwd")
                .setUuid("uuid")
                .setSsid("ssid")
                .setMac("mac")
                .setTimeout(time)
                .build();
blewifiActivator.setParams(params);

Register IActivatorListener to listen for pairing result

Example

blewifiActivator.setListener(new IActivatorListener() {
            @Override
            public void onSuccess(@Nullable IDevice iDevice) {
                Log.d(TAG, "onSuccess: ");

            }

            @Override
            public void onError(@NonNull String s, @NonNull String s1) {
                Log.d(TAG, "onError: ");

            }
        });
// Added enhanced Activator listener, maintaining compatibility with the previous version of the listener.
blewifiActivator.setListener(new IExtMultiModeActivatorListener() {
            @Override
            public void onActivatorStatePauseCallback(@NonNull String uuid, int configStage, int status) {

            }

            @Override
            public void onSuccess(@Nullable IDevice iDevice) {

            }

            @Override
            public void onError(@NonNull String s, @NonNull String s1) {

            }
        });

Description of onActivatorStatePauseCallback:

Pairing stage configStage Description Pairing status
0 Activation of pairing
  • 0: The pairing information is returned successfully.
  • 1: The pairing information is incorrect.
1 Delegate activation
  • 0: The delegate information is returned successfully.
  • 1: The pairing information is incorrect.
2 Connection to the network
  • 0: Connected to the router successfully.
  • 1: Incorrect information about connection to the network.
  • 2: Failed to find a router.
  • 3: Incorrect Wi-Fi password.
  • 4: Failed to connect to the router.
  • 5: Failed to get the DHCP-assigned IP address.
3 Activation stage
  • 1: Incorrect activation information.
  • 6: Failed to connect the device to the cloud.
  • 7: Failed to get the URL.
  • 8: The request to activate the device failed.
  • 9: Activated successfully.

Start pairing

This method starts device pairing.

Example

blewifiActivator.start();

Stop pairing

Example

blewifiActivator.stop();

Destroy pairing instance

Example

blewifiActivator.destroy();