Bluetooth Pairing

Last Updated on : 2024-09-03 07:08:00download

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

BLEActivator bleActivator = (BLEActivator) ActivatorService.activator(ActivatorMode.BLE);

Initialize device scanning IDiscovery

Parameters

Parameter Type Required Description
mode DiscoveryMode Yes The scanning mode.

Example

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

Device scanning parameters

Example

DiscoveryParams discoveryParams = new DiscoveryParams(new DiscoveryParams.Builder().setTimeout(600_000));
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);
iDiscovery.setListener(new IDiscoveryListener() {
            @Override
            public void didDiscover(@NonNull IDiscoveryDevice iDiscoveryDevice) {
                if (iDiscoveryDevice instanceof IBluetoothDevice){
                    ((IBluetoothDevice) iDiscoveryDevice).getAddress();
                    ((IBluetoothDevice) iDiscoveryDevice).getId();
                }

            }
        });

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.
deviceType Int No The type of the device.
uuid String Yes The UUID of the device.
productId String Yes The product ID of the device.
time long No The activation timeout, in milliseconds.
BLEActivatorParams params = new BLEActivatorParams.Builder()
                .setAddress("address")
                .setAssetId("assetId")
                .setDeviceType(deviceType)
                .setUuid("uuid")
                .setProductId("productId")
                .setTimeout(time)
                .build();
        bleActivator.setParams(params);

Register IActivatorListener to listen for pairing result

Example

bleActivator.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

bleActivator.start();

Stop pairing

Example

bleActivator.stop();

Destroy pairing instance

Example

bleActivator.destroy();