开发者

Not able to write to sys/class/uwb/scan file through JNI using android application

开发者 https://www.devze.com 2023-04-10 11:59 出处:网络
define SCAN_FILE /sys/class/uwb_rc/uwb0/scan My aim is to write to a SCAN_FILE in android as in " echo 9 0 >/sys/class/uwb_rc/uwb0/scan "

define SCAN_FILE /sys/class/uwb_rc/uwb0/scan

My aim is to write to a SCAN_FILE in android as in

  " echo 9 0 >  /sys/class/uwb_rc/uwb0/scan "

I want to implement this using android application and file operati开发者_如何转开发ons with JNI integration

My code is as shown below :-

package com.samsung.w;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class write extends Activity {
    
    private static final String TAG = "test";
 
    TextView tv_status;
    /** Called when the activity is first created. */

    private Object myClickHandler;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e(TAG, "entered after main and waiting ...");
       getAccessPerm("/sys/class/uwb_rc/uwb0/scan");        
    } 

      
     
    private void getAccessPerm(String path)
    {
        
        try {
    
            String command = String.format("echo \"chmod 777 %s\" | su\nexit\n", path);
            Process sh_process = Runtime.getRuntime().exec("sh");               
            
            OutputStream writer = sh_process.getOutputStream();
            writer.write(command.getBytes("ASCII")); 
            BufferedReader stdError = new BufferedReader(new InputStreamReader(
                                                    sh_process.getErrorStream()));
            
            String err_line = null;
            if((err_line = stdError.readLine()) != null) {
                
                Log.e(TAG, "getAccessPerm: chmod cmd failed");
                System.out.println(err_line);
                while ((err_line = stdError.readLine()) != null) {
                    System.out.println(err_line);
                } 
            }   
        }
        catch (Exception e) {
     
            Log.e(TAG, "getAccessPerm: exception");
            e.printStackTrace();   
        }      
    }
    
    
    
    public void myClickHandler(View view) {
        switch (view.getId()) {
        case R.id.scan_button: 
        TextView textview = (TextView)findViewById(R.id.tv_title);
            textview.setText(scaninit());
            break;   
        }
    }   
public native String  scaninit(); 


    
    static {
       System.loadLibrary("write");
    }
}

And the corresponding JNI file is

    jstring Java_com_samsung_w_write_scaninit(JNIEnv* env, jobject thiz) {
    FILE* file = fopen("/sys/class/uwb_rc/uwb0/scan", "rw");
    char *mystring[20];
    if (file != NULL) {
        fputs("9 0", file);
        fgets(mystring, 4, file);
        fclose(file);

        return (*env)->NewStringUTF(env, mystring);
    }

    return (*env)->NewStringUTF(env, "file opening failed!");
}

And Android Manifest file is like this

    <?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.samsung.w"
 android:versionCode="1"
 android:versionName="1.0">
 <uses-sdk android:minSdkVersion="7" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> **
 <uses-permission android:name="android.permission.FACTORY_TEST" /> **
 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity
   android:name=".write"
   android:label="@string/app_name">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>

 </application>
</manifest> 

**-tried these two thinking is it related to permission issue but no use

Default value in the SCAN_FILE on cat:-

   #cat /sys/class/uwb_rc/uwb0/scan

   3 0 

Result of typin in adb shell

 #echo 9 0 > | cat sys/class/uwb_rc/uwb0/scan
      9 0

but when i use the android application i am not able to see this change

  #cat /sys/class/uwb_rc/uwb0/scan
     3 0   (default value on booting)

Someone Kindly help me please


e could solve this thanks to Bhushan for helping me out

The problem is in the kernel source code DEVICE attribute the permission has to be written to

as

DEVICE_ATTR(scan, 0777, uwb_rc_scan_show, uwb_rc_scan_store);

instead of

DEVICE_ATTR(scan, S_IRUGO | S_IWUSR, uwb_rc_scan_show, uwb_rc_scan_store);

then in the C code fopen shoudl be in r+ mode as in

FILE* file = fopen("/sys/class/uwb_rc/uwb0/scan", "r+");

This forum is awesome :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号