I'm trying to execute the following method to spoof the MAC address of an android device. The method runs every time the user clicks a button:
public void changeMac(View v) throws IOException {
editText = (EditText)findViewById(R.id.newMacText);
String mac = editText.getText().toString();
Log.d("my log", "" + mac);
String[] cmds = {"ip link set wlan0 address " + mac};
Log.d("my log", "in method");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(p.getInputStream()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
Log.d("my log", "new mac " + Utils.getMACAddress("wlan0"));
Log.d("my log", result.toString());
}
Now when the user clicks the button again, the app crashes and I get "broken pipe" error.
android.system.ErrnoException: write failed: EPIPE (Broken pipe)
and also an illegal state exception. What can I do to make this error stop happening and let the method be called multiple times?
When you execute the command
os.writeBytes("exit\n");
this ends your su session. The su process ends itself and the pipe your are using for writing commands to the su shell gets broken.Therefore if you want to execute another command you have to restart a new su session or do not close the old one calling
exit
.Anyway I would not start the su process in your onCreate() method. SuperUser permissions should be acquired only when needed and not on app start.