Clearly you donot have permissions to open the device from user space. In the second case when you are running the executable from terminal, you are having permissions probably because you have donesu
before running the executable.
For your problem here, two things can be done.
1) Change the permissions of the node from terimnal.
Steps involved:
-
Open the terminal (
adb shell
) -
Do
su
(In order to do this your device must be rooted) -
Do
chmod 777 /dev/radio
in the terminal
Once this is done, your radio node is having proper permissions for the user to read and write. So you can now do open()
call and it will work.
2) Programmatically you can achieve this (assuming your device is rooted and su is running on your device) by calling the below function - changePerm()
. This is a small function I have written which will change the permissions of the device nodes or rather any system file that does not have user access. Once you have permissions, you can open it from user space. open() call will work properly after this.
void changePerm() { Process chperm; try { chperm=Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(chperm.getOutputStream()); os.writeBytes("chmod 777 /dev/radio\n"); os.flush(); os.writeBytes("exit\n"); os.flush(); chperm.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
I have tested this for other nodes. So it should also work for radio aswell. Let me know in case yo are facing any difficulty. Thanks