#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Question [B instead of byte[]


    Hi guys,

    There is a private method as the following:

    Code:
    private void android.telephony.SmsManager.sendRawPdu(byte[],byte[],android.app.PendingIntent,android.app.PendingIntent)
    And this code snippet DOES work well:

    Code:
    private void sendSMS(byte[] encodedAddress, byte[] encodedMessage) {
    Method mtd1 = SmsManager.class.getDeclaredMethod("sendRawPdu", byte[].class, byte[].class, PendingIntent.class, PendingIntent.class);
    mtd1.setAccessible(true);
    mtd1.invoke(SmsManager.getDefault(), encodedAddress, encodedMessage, null, null);
    }

    On another device, there is another signature for this method, as below:

    Code:
    private void android.telephony.SmsManager.sendRawPdu([B,[B,android.app.PendingIntent,android.app.PendingIntent)
    This one uses '[B' instead of 'byte[]'. I've tried the above code snippet but it does NOT work. Please help me to re-write the code.

    Thanks a lot.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    > This one uses '[B' instead of 'byte[]'. I've tried the above code snippet but it does NOT work. Please help me to re-write the code.
    Is that something you copied off a web page?

    Because it seems to me that it's far more likely that some HTML formatting got carried away and munged the code.
    [B by itself makes no syntactic sense.

    The first one works, and would work on any Android device.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    > This one uses '[B' instead of 'byte[]'. I've tried the above code snippet but it does NOT work. Please help me to re-write the code.
    Is that something you copied off a web page?

    Because it seems to me that it's far more likely that some HTML formatting got carried away and munged the code.
    [B by itself makes no syntactic sense.

    The first one works, and would work on any Android device.
    hi salem, thank you for your response.

    It is not a mispelling, [B is a class name of the byte array byte[]
    you can see the link here: stackoverflow.com/questions/4606864/what-kind-of-java-type-is-b

    The problem is that the sendRawPdu method does not exist on all Android devices, and the signature also are not the same. On some device it does NOT exist at all. On some device it exists with the signature #1, on other devices with the signature #2...

    With the signature #1, the above code work well. But it does not work with signature #2, although [B and byte[] are almost the "same".

    I dont know why and how to correct the code?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    Yes, the keyword being "JNI"

    The Java Native Interface uses coded strings ( like [B ) to allow the native implementation (C or C++) to correctly specify the APIs it implements, so the Java runtime knows what it is calling.

    As far as I know, it does not appear in Java code proper, and if it's screwing up, then it seems the JNI implementation is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    Yes, the keyword being "JNI"

    The Java Native Interface uses coded strings ( like [B ) to allow the native implementation (C or C++) to correctly specify the APIs it implements, so the Java runtime knows what it is calling.

    As far as I know, it does not appear in Java code proper, and if it's screwing up, then it seems the JNI implementation is broken.
    So what I can do with the 2-nd signature?

    How can I write the code?
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    I don't know.

    Perhaps you should post more information.

    Like for example, actual URLs of where the WHOLE code of what you found can be read by all (to establish the context).

    One line of code and "it doesn't work" is hardly the basis of a meaningful error report.

    All I can say is that case 1 works and case 2 doesn't (because it doesn't look like any Java I've seen before).
    I've just searched all of the Android source tree, and there's nothing like that in any of the standard Java code.

    The only place where you see the [B notation is in JNI interfaces - eg.
    Code:
    // ./frameworks/base/core/jni/android_os_Parcel.cpp
    
    static const JNINativeMethod gParcelMethods[] = {
    ...
        {"nativePushAllowFds",        "(IZ)Z", (void*)android_os_Parcel_pushAllowFds},
        {"nativeRestoreAllowFds",     "(IZ)V", (void*)android_os_Parcel_restoreAllowFds},
    
        {"nativeWriteByteArray",      "(I[BII)V", (void*)android_os_Parcel_writeNative},
    ...
    So the C++ function android_os_Parcel_writeNative expects to be able to extract from the Java call frame (amongst other things) a byte array, as signified by "[B" in the control string.

    > So what I can do with the 2-nd signature?
    Delete it and replace it with the first one which works perhaps.

    The second seems to be some random crap you found that could never work, so I fail to see the point of expending any more effort on it. You have something which works, go with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by salem
    I don't know.

    Perhaps you should post more information.

    Like for example, actual URLs of where the WHOLE code of what you found can be read by all (to establish the context).

    One line of code and "it doesn't work" is hardly the basis of a meaningful error report.

    All I can say is that case 1 works and case 2 doesn't (because it doesn't look like any Java I've seen before).
    I've just searched all of the Android source tree, and there's nothing like that in any of the standard Java code.

    The only place where you see the [B notation is in JNI interfaces - eg.
    Code:
    // ./frameworks/base/core/jni/android_os_Parcel.cpp
    
    static const JNINativeMethod gParcelMethods[] = {
    ...
        {"nativePushAllowFds",        "(IZ)Z", (void*)android_os_Parcel_pushAllowFds},
        {"nativeRestoreAllowFds",     "(IZ)V", (void*)android_os_Parcel_restoreAllowFds},
    
        {"nativeWriteByteArray",      "(I[BII)V", (void*)android_os_Parcel_writeNative},
    ...
    So the C++ function android_os_Parcel_writeNative expects to be able to extract from the Java call frame (amongst other things) a byte array, as signified by "[B" in the control string.

    > So what I can do with the 2-nd signature?
    Delete it and replace it with the first one which works perhaps.

    The second seems to be some random crap you found that could never work, so I fail to see the point of expending any more effort on it. You have something which works, go with it.
    Hi salem,

    The method is at the target (customer) device and I cannot delete or modify it.

    BTW the code I took from here:

    http://www.tutorialforandroid.com/2009/11/calling-private-methods-in-android.html

    However this code is for signature #1.

    On some other device the signature is as #2. And this code does not work anymore.

IMN logo majestic logo threadwatch logo seochat tools logo