How to write an Android application to do sysfs read/write.?

android, android-ndk, apk, linux-device-driver, linux-kernel

Solution

Firstly, make sure you have the permission to that sysfs node(Usually you don't, if you are developing a user app).

Secondly, I would say usually you don't have to talk to sysfs node directly from Android app.Below app there are Android Framework and HAL levels that did all the abstraction for you.

Since not sure about what you are going to do, here is an example I fetched from Android LightsService which directly talk to sysfs node, which might be helpful to you.

216        private static final String FLASHLIGHT_FILE = "/sys/class/leds/spotlight/brightness";
...
236        try {
237                FileOutputStream fos = new FileOutputStream(FLASHLIGHT_FILE);
238                byte[] bytes = new byte[2];
239                bytes[0] = (byte)(on ? '1' : '0');
240                bytes[1] = '\n';
241                fos.write(bytes);
242                fos.close();
243            } catch (Exception e) {
244                // fail silently
245            }
246        }

Problem

I want to write an Android application with UI Button Read/Write that does `sysfs read` or `sysfs write`. I found the below example code for java.io.RandomAccessFile. ``` package com.tutorialspoint; import java.io.*; public class RandomAccessFileDemo { public static void main(String[] args) { try { // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // set the file pointer at 0 position raf.seek(0); // read the first byte and print it System.out.println("" + raf.read()); // set the file pointer at 4rth position raf.seek(4); // read the first byte and print it System.out.println("" + raf.read()); } catch (IOException ex) { ex.printStackTrace(); } } } ``` Can someone please tell me how to build this code using Android sdk.?

Original source