How to write an android application to read / write sysfs?

I want to write an Android app with a read / write button for UI buttons that does sysfs read or sysfs write .

I found the below code example 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 tell me how to create this code using Android sdk.?

+7
android linux-kernel android-ndk apk linux-device-driver
source share
1 answer

First, make sure you have permission for this sysfs node (usually you do not do this if you are developing a custom application).

Secondly, I would say that usually you do not need to talk to sysfs node directly from the Android app.Below application. There are Android Framework and HAL levels that have done all the abstraction for you.

Since I'm not sure what you are going to do, here is an example that I got from Android LightsService that talks directly to sysfs node, which may be useful for 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 } 
+5
source share

All Articles