#include

#include

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "freertos/event_groups.h"

#include "esp_system.h"

#include "esp_wifi.h"

#include "esp_event_loop.h"

#include "esp_log.h"

#include "nvs_flash.h"

#include "lwip/err.h"

#include "lwip/sockets.h"

#include "lwip/sys.h"

#include

#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID

#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD

#define PORT CONFIG_EXAMPLE_PORT

/ FreeRTOS event group to signal when we are connected & ready to make a request /

static EventGroupHandle_t wifi_event_group;

const int IPV4_GOTIP_BIT = BIT0;

static const char *TAG = "UDP";

static esp_err_t event_handler(void ctx, system_event_t event)

{

switch (event->event_id) {

case SYSTEM_EVENT_STA_START:

esp_wifi_connect();

ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");

break;

case SYSTEM_EVENT_STA_CONNECTED:

break;

case SYSTEM_EVENT_STA_GOT_IP:

xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);

ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");

break;

case SYSTEM_EVENT_STA_DISCONNECTED:

/ This is a workaround as ESP32 WiFi libs dont currently auto-reassociate. /

esp_wifi_connect();

xEventGroupClearBits(wifi_event_group, IPV4_GOTIP_BIT);

break;

default:

break;

}

return ESP_OK

《Android學習筆記總結+最新移動架構視頻+大廠安卓面試真題+項目實戰源碼講義》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整內容開源分享

;

}

static void initialise_wifi(void)

{

tcpip_adapter_init();

wifi_event_group = xEventGroupCreate();

ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();

ESP_ERROR_CHECK( esp_wifi_init(&cfg) );

ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );

wifi_config_t wifi_config = {

.sta = {

.ssid = EXAMPLE_WIFI_SSID,

.password = EXAMPLE_WIFI_PASS,

},

};

ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);

ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );

ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );

ESP_ERROR_CHECK( esp_wifi_start() );

}

static void wait_for_ip()

{

uint32_t bits = IPV4_GOTIP_BIT;

ESP_LOGI(TAG, "Waiting for AP connection...");

xEventGroupWaitBits(wifi_event_group, bits, false, true, portMAX_DELAY);

ESP_LOGI(TAG, "Connected to AP");

}

static void udp_server_task(void *pvParameters)

{

char rx_buffer[4000];

char addr_str[128];

int addr_family;

int ip_protocol;

while (1) {

struct sockaddr_in destAddr;

destAddr.sin_addr.s_addr = htonl(INADDR_ANY);

destAddr.sin_family = AF_INET;

destAddr.sin_port = htons(PORT);

addr_family = AF_INET;

ip_protocol = IPPROTO_IP;

inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);

int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);

if (sock < 0) {

ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket created");

int err = bind(sock, (struct sockaddr *)&destAddr, sizeof(destAddr));

if (err < 0) {

ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);

}

ESP_LOGI(TAG, "Socket binded");

while (1) {

ESP_LOGI(TAG, "Waiting for data");

struct sockaddr_in sourceAddr;

socklen_t socklen = sizeof(sourceAddr);

int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&sourceAddr, &socklen);

// Error occured during receiving

if (len < 0) {

ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);

break;

}

// Data received

else {

// Get the senders ip address as string

inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);

rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...

總結

這次面試問的還是還是有難度的,要求當場寫代碼并且運行,也是很考察面試者寫代碼
因為Android知識體系比較龐大和復雜的,涉及到計算機知識領域的方方面面。在這里我和身邊一些朋友特意整理了一份快速進階為Android高級工程師的系統且全面的學習資料。涵蓋了Android初級——Android高級架構師進階必備的一些學習技能。

附上:我們之前因為秋招收集的二十套一二線互聯網公司Android面試真題(含BAT、小米、華為、美團、滴滴)和我自己整理Android復習筆記(包含Android基礎知識點、Android擴展知識點、Android源碼解析、設計模式匯總、Gradle知識點、常見算法題匯總。)

里面包含不同方向的自學編程路線、面試題集合/面經、及系列技術文章等,資源持續更新中…

本文已被[CODING開源項目:《Android學習筆記總結+移動架構視頻+大廠面試真題+項目實戰源碼》]( )收錄