Wi-Fi and Bluetooth Combo Pairing

Last Updated on : 2024-05-27 06:30:35download

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.

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 Yes The password of the Wi-Fi network.
uuid String Yes The UUID of the device.
ssid String Yes The SSID of the Wi-Fi network.
mac String No The MAC address of the device.
time long No The activation timeout, in milliseconds.
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: ");

            }
        });

Start pairing

This method starts device pairing.

Example

blewifiActivator.start();

Stop pairing

Example

blewifiActivator.stop();

Destroy pairing instance

Example

blewifiActivator.destroy();