Bug 1522 - Getting a Byte[] from a Java.Lang.Object fails using JNIEnv.GetArray<Byte>(intptr)
Summary: Getting a Byte[] from a Java.Lang.Object fails using JNIEnv.GetArray<Byte>(in...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: Macintosh Mac OS
: --- enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-10-16 17:26 UTC by Chris Hardy [MSFT]
Modified: 2011-11-04 10:14 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Chris Hardy [MSFT] 2011-10-16 17:26:19 UTC
Using 
var bytes = JNIEnv.GetArray<Byte>(castedPdus[i].Handle); 
fails with this error - https://gist.github.com/ae1d7a1723a9d1376c74 - where castedPdus is a object[].

where the workaround of

var bytes = new Byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
JNIEnv.CopyArray(castedPdus[i].Handle, bytes);

is successful.

The following is a sample Broadcast Receiver to hit this error:

https://gist.github.com/61dbd32792c20cca72c0
Comment 1 Chris Hardy [MSFT] 2011-11-02 11:27:09 UTC
To get the broadcast to run,

Telnet into the emulator;

telnet localhost 5554

then send SMS commands like the following;

sms send 123456789 my message goes here

I hope this helps,

ChrisNTR
Comment 2 Jonathan Pryor 2011-11-02 11:33:04 UTC
`ddms` can also be used, by selecting the device in the left tre view, selecting the Emulator Control tab in the right pane, and using the Telephony Actions section.
Comment 3 Jonathan Pryor 2011-11-02 11:36:58 UTC
The app also needs the android.permission.RECEIVE_SMS permission in order to receive the SMS.
Comment 4 Jonathan Pryor 2011-11-03 10:04:50 UTC
The "Fails" line in the gist is fixed in 46fe19e0, allowing this to work:

    var bytes = JNIEnv.GetArray<Byte>(castedPdus[i].Handle);
Comment 5 Jonathan Pryor 2011-11-04 10:14:45 UTC
Fixed in 3cde304a, allowing OnReceive() to make great use of LINQ (while being somewhat similar to the Java code):

    public override void OnReceive(Context context, Intent intent)
    {
      if (intent.Action == INTENT_ACTION) {
        Bundle bundle = intent.Extras;

        if (bundle != null) {
          var pdus = (Java.Lang.Object[]) bundle.Get ("pdus");

          var msgs = pdus.Select (pdu => SmsMessage.CreateFromPdu ((byte[]) pdu)).ToArray ();
          var str  = string.Join ("\n",
              msgs.Select (msg => "SMS from: " + msg.OriginatingAddress + ": " +
                msg.MessageBody.ToString ()));

          Toast.MakeText(context, str, ToastLength.Short).Show();
        }
      }
    }