#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学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》]( )收录