-1

I want get SSID, Wifi-password from user, connect Applicable WiFi on Android Things

I reference Connecting Raspberry Pi running Android Things to wifi

first my wifi connect command

adb shell am startservice \
-n com.google.wifisetup/.WifiSetupService \
-a WifiSetupService.Connect \
-e ssid <Network_SSID> \
-e passphrase <Network_Passcode>

On ADB

first I write this command on terminal it is success work.

but fail work on source.

try {
    String[] command = {"am startservice -n com.google.wifisetup/.WifiSetupService -a WifiSetupService.Connect -e ssid AndroidHotspot7899 -e passphrase 1111"};
    Process connProcess = Runtime.getRuntime().exec(command);
} catch (IOException e) {
    e.printStackTrace();
}

Error log

 W/System.err: java.io.IOException: Cannot run program "am startservice -n com.google.wifisetup/.WifiSetupService -a WifiSetupService.Connect -e ssid AndroidHotspot7899 -e passphrase 78994245": error=2, No such file or directory
 W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
 W/System.err:     at java.lang.Runtime.exec(Runtime.java:692)
    at java.lang.Runtime.exec(Runtime.java:560)
 W/System.err:     at com.example.hyunwook.connect.MainActivity.lambda$onCreate$1$MainActivity(MainActivity.java:72)
 W/System.err:     at com.example.hyunwook.connect.MainActivity$$Lambda$1.onClick(Unknown Source:21)
 W/System.err:     at android.view.View.performClick(View.java:6294)
    at android.view.View$PerformClick.run(View.java:24770)
 W/System.err:     at android.os.Handler.handleCallback(Handler.java:790)
 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 W/System.err: Caused by: java.io.IOException: error=2, No such file or directory
 W/System.err:     at java.lang.UNIXProcess.forkAndExec(Native Method)
 W/System.err:     at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
 W/System.err:     at java.lang.ProcessImpl.start(ProcessImpl.java:128)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)

perhaps, Is it a problem to use it in a lambda expression?

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
hyunwookcho
  • 147
  • 1
  • 3
  • 16

1 Answers1

1

The problem is that am isn't in the $PATH for this app, and as such when you try to run it it fails.

Why are you doing this anyway? If you know the name of the service, launch it via intent. That's the way you're supposed to launch services in Android. I can't think of any valid reason you should be using a command to am inside of an app.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • because, I want within in app, get ssid, wifi password from user, To connect to Wi-Fi without accessing the default Wi-Fi settings window. – hyunwookcho Aug 28 '18 at 09:23
  • If you can do that via command line with am, then you can do that via intent with parameters as well. All am is going to do is create an intent and send it to that service. You should not be using am in an app. – Gabe Sechan Aug 28 '18 at 09:24
  • within in app, not use `am` command, how to use wifi connect? – hyunwookcho Aug 28 '18 at 09:31