WifiManager startScan () не дает результатов в Flutter - PullRequest
0 голосов
/ 22 мая 2019

Поскольку фактическая библиотека Wi-Fi Flutter не работает надежно, я пытался использовать рабочую библиотеку от Android в пользовательской части Android Flutter. Хотя он работал нормально в Android, он не работал во Flutter. Поскольку я не могу отладить код библиотеки, я скопировал код в свой проект флаттера. Я также запустил проект Android с тем же кодом, чтобы сравнить разницу в отладке. Отладив код, я обнаружил, что mWifiManager.startScan () возвращает true в Android и false в Flutter. Поскольку это системный вызов, я не могу отлаживать дальше. Код:

private final WifiStateCallback mWifiStateCallback = new WifiStateCallback() {
    @Override
    public void onWifiEnabled() {
        wifiLog("WIFI ENABLED...");
        unregisterReceiver(mContext, mWifiStateReceiver);
        of(mWifiStateListener).ifPresent(stateListener -> stateListener.isSuccess(true));

        if (mScanResultsListener != null || mPassword != null) {
            wifiLog("START SCANNING....");
            if (mWifiManager.startScan())
                registerReceiver(mContext, mWifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            else {
                of(mScanResultsListener).ifPresent(resultsListener -> resultsListener.onScanResults(new ArrayList<>()));
                of(mConnectionWpsListener).ifPresent(wpsListener -> wpsListener.isSuccessful(false));
                mWifiConnectionCallback.errorConnect();
                wifiLog("ERROR COULDN'T SCAN");
            }
        }
    }
};

AndroidManifest в Android:

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

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

AndroidManifest во флаттере:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="teco.kit.edu.smartaq_wlan">

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- for Android 6 and above -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="smartaq_wlan"
        android:icon="@mipmap/ic_launcher">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

build.gradle в Android:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.publishVersionCode
        versionName rootProject.ext.publishVersionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        encoding "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-beta02', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation "androidx.annotation:annotation:1.0.0"
    implementation 'com.thanosfisherman.elvis:elvis:2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
}

build.gradle во флаттере:

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "teco.kit.edu.smartaq_wlan"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
...