Detecting the network status of the device in Android system


Preface

In embedded Linux development, sometimes our program needs to detect the networking status of the device. The following introduces a method to detect the networking status of the device by detecting the corresponding files under sysfs.


1. Command line reading

/sys/class is exported by the kernel at runtime in order to expose the hierarchical relationship of the hardware through the file system.
We can detect the corresponding files to get the status of the network connection in real time.

Detect the connection status of the wireless network and detect files:

/sys/class/net/wlan0/operstate

Insert image description here

Detect the connection status of the wired network and detect files:

/sys/class/net/eth0/operstate

Insert image description here
Read through the command line. Returning down indicates that the device network is disconnected. Returning up indicates that the device network is connected.

2. Application testing

Test code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>    
#include <sys/socket.h>       // bind
#include <sys/ioctl.h>
#include <linux/if_packet.h>  
#include <netinet/in.h>      // struct sockaddr_in
#include <netinet/in.h>      // for function htons

typedef enum _net_conn_status
{
    
    
    NET_DISCONNECT = -1,
    NET_CONNECT = 1
}net_conn_status_e;

static int check_net_status(void)
{
    
    
    int ret = 0;

    static char cmd_out[128] = {
    
    0};
    FILE *fp = popen("cat /sys/class/net/wlan0/operstate", "r");
    if(fp == NULL)
    {
    
    
        printf("popen error\n");
        return -1;
    }

    bzero(cmd_out, sizeof(cmd_out));
    fscanf(fp, "%s", cmd_out);
    pclose(fp);
    // printf("cmd_out = %s\n", cmd_out);
    if (0 == strcmp((char*)cmd_out, "down"))
    {
    
    
        // printf("net disconnect\n");
        return -1;
    }
    else if (0 == strcmp((char*)cmd_out, "up"))
    {
    
    
        // printf("net connect\n");
        return 1;
    }else{
    
    }

    return ret;
}

int main(int argc, char **argv)
{
    
    
    while (1)
    {
    
    
        if (NET_DISCONNECT == check_net_status())
        {
    
    
            printf("net disconnect====================\n");
        }
        else if (NET_CONNECT == check_net_status())
        {
    
    
            printf("net connect====================\n");
        }
        usleep(10 * 1000);
    }

    return 0;
}

Makefile:

CROSS_COMPILE = sdk/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC            = ${
    
    CROSS_COMPILE}gcc
AR            = ${
    
    CROSS_COMPILE}ar
ARFLAGS       = rcs
TARGET        := app

SOURCES       = $(wildcard $(TARGET).c)
OBJS          = $(patsubst %.c, %.o, $(SOURCES))

all:${
    
    TARGET}

#execute bin in elf
${
    
    TARGET}:$(OBJS)
	@mkdir -p build
	$(CC) $(OBJS) -o build/$(TARGET) -static

#compile
%.o:%.c
	$(CC) $(INCLUDE) $(DEBUGS) ${
    
    CFLAGS} -c $< -o $@

.PHONY:clean
clean:
	 @echo "remove app and objs files ..."
	 rm -rf $(OBJS) $(target) build *.o


After compilation, push the generated application app to the Android device.
Insert image description here
Test:
Insert image description here

This method can detect changes in network status in relatively real-time.


Guess you like

Origin blog.csdn.net/chenkanuo/article/details/134379510