以编程方式将Android应用程序连接到ESP8266

时间:2017-12-31 20:57:33

标签: android esp8266

我们正在尝试将我们的ESP8266连接到安卓版本6+的设备上的Android应用程序,该应用程序是以编程方式连接到esp8266访问点,使用http请求发送和接收一些命令,该应用程序与android工作正常版本低于6,某些使用Android 6及以上版本的手机上的应用程序工作正常,其他手机根本不工作(也就是说没有连接到esp)。该应用程序正确配置网络,但手机不能正常运行。以编程方式连接到它但是如果你在设置中从wifi中选择它而没有要求密钥(这意味着网络配置正确),它会连接到它。 编辑:当您删除其他可用的已配置网络时,通常无法工作的电话可以正常工作,从而只留下ESP配置的网络连接到

public class NewAppWidget extends AppWidgetProvider {

    static int ids;
    private static AppWidgetManager appWidgetManager1;
    static SharedPreferences credentials;
    static RemoteViews rv;

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            NewAppWidgetConfigureActivity.deleteTitlePref(context, appWidgetIds[i]);
        }
    }

    @Override
    public void onEnabled(Context context) {
        Log.i("Scenario","onEnabled");
        // Enter relevant functionality for when the first widget is created
        ids=0;
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
        ids-=1;
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        Log.i("lastLog","updateAppWidget");
        ids=appWidgetId;
        appWidgetManager1=appWidgetManager;


        rv = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

/*   do stuff 
*/
     appWidgetManager.updateAppWidget(ids, rv);


    }

    public static PendingIntent getSelfPendingIntent(Context context, String command){
        Log.i("lastLog","getSelfPendingIntent");
        Intent actionPerformed = new Intent(context, NewAppWidget.class);
        actionPerformed.setAction(command);
        actionPerformed.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return PendingIntent.getBroadcast(context, 0, actionPerformed, 0);
    }
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.i("lastLog", "onUpdate");
        final int N = appWidgetIds.length;

        for (int i = 0; i < N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.i("lastLog","onReceive: "+intent.getAction().toString());
        String passKey=intent.getAction().toString() + context.getSharedPreferences("PassKey", 0);

        credentials = context.getSharedPreferences("MainStorage", 0);
        rv= new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

        if (intent.getAction().toString().equals("OPEN")){
            opened=true;
        }
        else{
            opened=false;
        }
//      updating the widget's layout drawing
        if (appWidgetManager1!=null)
        {updateAppWidget(context,appWidgetManager1, ids);}
//       end of layout drawing update

        wifi_action(context, intent);

        super.onReceive(context, intent);
    }
    public void wifi_action(final Context context, Intent intent){

        //       start sending the HTTP REQUEST
        if (intent.getAction().toString().equals("somthing") || intent.getAction().toString().equals("somtthingelse")){
            Log.i("lastLog","1");
            final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            AsyncHttpClient client = new AsyncHttpClient();

            String storedSSID=credentials.getString("SSID","");
            String storedNetworkPass=credentials.getString("NetworkPassword","");
            String storedPassKey=credentials.getString("PassKey","");
            String storedUrl=credentials.getString("URL","")+storedPassKey+intent.getAction().toString();
            Log.i("lastLog","2");
//            Log.i("SSID",storedSSID);
//            Log.i("Network Password",storedNetworkPass);
//            Log.i("Action PassKey",storedPassKey);
//            Log.i("URL",storedUrl);

            if (wifiManager.isWifiEnabled()){
                wifiManager.setWifiEnabled(false);
                while(wifiManager.isWifiEnabled());
            }
            if (!wifiManager.isWifiEnabled()){
                wifiManager.setWifiEnabled(true);
                while (!wifiManager.isWifiEnabled());
            }
            Log.i("lastLog","3");
            if (wifiManager.isWifiEnabled()){
                WifiConfiguration configuration= new WifiConfiguration();
                configuration.SSID="\"" + "SSID" + "\"";
                configuration.preSharedKey="\""+ "somepass"  + "\"";
                wifiManager.addNetwork(configuration);

                List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();

                Log.i("lastLog","4");
                for (WifiConfiguration i : list) {
                    Log.i("lastLog",i.SSID);
                    if (i.SSID != null && i.SSID.equals("\"" + "SSID" + "\"")) {
                        Log.i("lastLog", "Network Found, #"+i);
                        wifiManager.disconnect();
                        wifiManager.enableNetwork(i.networkId, true);
                        wifiManager.reconnect();
                        Log.i("lastLog", "supposed to be connected by now");
                        client.get(context, "http://somelink", null, new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                super.onSuccess(statusCode, headers, response);
                                wifiManager.setWifiEnabled(false);
                                for (int i = 1; i < ids; i++) {
                                    updateAppWidget(context, appWidgetManager1, ids);
                                    Log.i("lastLog", "Command Communicated Successfully!");
                                }
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                                super.onFailure(statusCode, headers, responseString, throwable);
                                Log.i("lastLog", "Command Communicated Failed");
                            }
                        });
                        break;
                    }
                }

            }

        }

        if  (intent.getAction().equals("SETTINGS")){
            Intent startSettings = new Intent(context, MainActivity.class);
            startSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(startSettings);
        }
    }
}

0 个答案:

没有答案