2

My goal is to use libnl in an Android Application to fetch a list of beacon frames received in the latest network scan and print (display) the associated metadata such as AP SSID, AP BSSID etc...

Following basic tutorials and guidelines, the approach seems to be:

  1. Cross compile libnl for all supported Android ABIs since libnl comes with its own ./configure and Makefile
  2. Import the compiled archives in my Native C/C++ code as PREBUILT_STATIC_LIBRARY or a PREBUILT_SHARED_LIBRARY
  3. Use libnl methods in native code

Dev Environment and Configuration:

  • IDE: Android Studio 3.1.2
  • NDK Version: Android NDK revision 17b
  • External Tools: Gradle, CMake
  • MinSDK Version: API 14
  • Targeted SDK Version: API 21
  • Library Source: Libnl 3.2.25 - link
  • OS: Ubuntu 18.04 LTS

  1. Cross Compiling:
    I have been able to successfully cross compile libnl for all the ABIs (armeabi-v7a, arm64-v8a, x86, x86_64) using the NDK Standalone Toolchains method documented here. This gives me .a compiled files for the library.

  2. Importing the Libraries and Compiling:
    This was done closely following the code samples from Google (github) - specifically the "hello-libs" example.

NOTE: I infer that step 1 and 2 are done correctly because the library gets linked successfully with my C/C++ source code when I compile and run the app. Further, I am able to call libnl methods from my C/C++ source code. I get no UnsatisfiedLinkError or Undefined Reference to ....

  1. Using libnl methods as in Linux
    I have debugged the issue to the statement using the libnl method genl_ctrl_resolve to get driverId for nl80211 driver. This returns error code -12 meaning "Object not found."

    ...
    struct nl_sock *socket = init_socket();
    driver_id = genl_ctrl_resolve(socket, "nl80211");
    ...
    

My specific doubts are:

  1. Is my approach correct? If not, any help or insight is appreciated.
  2. Is there any alternative approach?
  3. Does my application require root access to get the driver id?
  4. Any good resources for learning more about Native Android Development.

Any help is appreciated. Thank you in anticipation.


PS..

  • I have gone through the Android Developer's Website
  • I have been referring Android NDK Beginner's Guide by Sylvain Ratabouil and Pro Android Apps Performance Optimization by Herve´ Guihot
  • Also tried building the project from scratch using Eclipse (ADT) and older SDK (r24.4.1), NDK (r10e)
Gursimran Singh
  • 301
  • 2
  • 5

2 Answers2

1

If libnl requires read access to /dev or /proc you can forget about accessing it from the NDK on unrooted devices.

Google has been clamping down Android from a couple of releases, and as of Android 7, all read access outside the APK install directory are strictly forbidden, as applications are required to read files indirectly via SAF.

https://developer.android.com/about/versions/nougat/android-7.0-changes#permfilesys

https://developer.android.com/guide/topics/providers/document-provider

A better way would be to use the Framework network interfaces via JNI.

Paulo Pinto
  • 632
  • 4
  • 10
  • 1
    Thanks for your reply. A brief research shows that "network interfaces" framework is not able to access frame layer information (where beacon frames exist) but is more concerned with the network layer. I shall update more after digging deeper into this. – Gursimran Singh Jul 25 '18 at 16:29
0

libnl talks to the kernel through a socket. You are asking the kernel to give you access to a driver interface for your wifi. At this point it would be relevant to know which kernel your device is using and which specific driver it's using for wifi. On Android these are manufacturer specific, so you cannot rely on specific details based on API level.

Based on the details you provide my best guess is: 1. The kernel is understanding your query 2. The driver does not have a nl80211 interface. You could try to listen for wireless extension messages?

All in all, it might work on some devices and not others, regardless of API level.

Mihai Timar
  • 1,141
  • 13
  • 21