Skip to content

Scan devices

You can scan for Bluetooth Low Energy (BLE) (BLEScanner), Wi-Fi (WifiScanner) or NFC (NFCScanner) capable Taps. Each scanner implements the IDeviceScanner class.

If you need a ready-to-use Tap picker, your can either use the ScanActivity or ScanDeviceFragment class. See UI Components page for how to use it.

Depending on the smartphone that the device application is running on, NFC, BLE or Wi-Fi may not be available. Use function IDeviceScanner to check if the scanner is available.

Do not forget to request permission!

Wi-Fi Scanner

class YourActivity extends Activity {
    @Nullable
    WifiScanner scanner;

    @Override
    public void onCreate(){
        scanner = new WifiScanner(context);
        scanner.setOnDeviceDiscoveredCallback(new IOnDeviceDiscovered<ScanResult>() {
            @Override
            public void onDeviceDiscovered(ScanResult scanResult) {
                // You could instantiate a device from this info
                // IoTizeDevice device = IoTizeDevice.fromProtocol(new WIFIProtocol(scanResult));
            }
        });

    }

    @Override
    public void onResume(){
        scanner.start();
    }

    @Override
    public void onPause(){
        scanner.stop();
    }
}

Bluetooth Low Energy Scanner

class YourActivity extends Activity {
    @Nullable
    BluetoothScanner scanner;

    @Override
    public void onCreate(){
        scanner = new BluetoothScanner(context);
        scanner.setOnDeviceDiscoveredCallback(new IOnDeviceDiscovered<BluetoothDevice>() {
            @Override
            public void onDeviceDiscovered(BluetoothDevice device) {
                // You could instantiate a device from this info
                // IoTizeDevice device = IoTizeDevice.fromProtocol(BLEProtocol.fromDevice(YourActivity.this, device));
            }
        });

    }

    @Override
    public void onResume(){
        scanner.start();
    }

    @Override
    public void onPause(){
        scanner.stop();
    }
}

NFC Scanner

To enable NFC you need to edit your Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.iotize.android.exampleapp">

    <!-- Add permission -->
    <uses-permission android:name="android.permission.NFC" />

    <!-- Add intent filter -->
    <activity
        android:name=".MyActivity">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <!-- <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="application/com.iotize.android.communicationapp" /> -->
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</manifest>

Then in your activity, create an NFCScanner instance.

class MyActivity extends Activity {

    @Nullable
    NFCScanner scanner;

    @Override
    public void onCreate(){
        scanner = new NFCScanner(context);
        scanner.setOnDeviceDiscoveredCallback(new IOnDeviceDiscovered<Intent>() {
            @Override
            public void onDeviceDiscovered(Intent intent) {
                Log.d(TAG, "NFC device discovered");
                NFCIntentParser nfcIntentParser = new NFCIntentParser(intent);
                Tag nfcTag = nfcIntentParser.getTag();

                // You could instantiate a device from this info
                // IoTizeDevice device = IoTizeDevice.fromProtocol(NFCProtocol.fromTag(nfcTag));
            }
        });

    }

    /**
     * IMPORTANT

     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "New intent: " + intent);

        String action = intent.getAction();
        if (action != null && (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED) || action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED))) {
            mNFCScanner.onNewIntent(intent);
        }
    }

    @Override
    public void onResume(){
        scanner.start();
    }

    @Override
    public void onPause(){
        scanner.stop();
    }
}