Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending JSON as extra data in an android broadcast via ADB gets incorrectly formatted

I'm trying to send JSON data as extra on an Android broadcast which I send via ADB to the device. But it looks like the data which gets to the device is not as expected.

What I execute:

adb shell am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver -e "notify" '{"debug": false, "title": "Application update!"}'

What I expect as extra data:

{"debug": false, "title": "Application update!"}

What I get as extra data:

"debug": false

If I send {"debug": false "title": "Application update!"} as extra data then i get {"debug": false "title": "Application update!"} as extra data (noting the missing comma). So I assume it has something to do with Brace Expansion of my bash, but turning it off does not solve the problem and escaping the braces or the comma is not working.

What am I doing wrong?

like image 624
bela.waeckerlig Avatar asked Jan 14 '15 09:01

bela.waeckerlig


2 Answers

Your workaround was a huge help!

It indicates that the problem is in quoting through two shells (host plus Android). Shell quoting is a horrible tarpit but there's serious extra weirdness here.

After experimenting with "echo" commands, I found that adb shell's argument must be quoted as a single argument to the local shell and the JSON payload must be quoted as a single argument to the Android shell.

Here's a general solution (and it doesn't require sprinkling \-quoting of ", !, {, and } chars in the JSON text):

adb shell "am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver -e notify '"'{"debug": false, "title": "Application update!"}'"'"

Pattern: adb shell "am broadcast ... '"'JSON_TEXT'"'"

The inner pair of ' marks quotes the JSON_TEXT locally while the outer pair quotes it remotely. That outer pair is in turn "-quoted.

like image 173
Jerry101 Avatar answered Nov 19 '22 19:11

Jerry101


ok, I just found a solution. I first have to enter the devices shell via adb shell and then execute am broadcast -a com.test.android.ACTION_TEST_FEATURE -n com.test.android/.receivers.TestsReceiver --es "notify" '{"debug": false, "title": "Application update!"}'

like image 32
bela.waeckerlig Avatar answered Nov 19 '22 21:11

bela.waeckerlig