Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.?

like image 497
kzs Avatar asked Jul 15 '13 04:07

kzs


1 Answers

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        }
like image 152
Jun Avatar answered Oct 08 '22 16:10

Jun