Hi,
I am trying to port the Gingerbread CAN application in ICS with JNI calls.
While running my application in my target system its hanging. I am getting
the below error.
E/dalvikvm(662): JNI ERROR (app bug): attempt to use stale local reference
0x1
W/dalvikvm(662): JNI WARNING: invalid reference returned from native code
W/dalvikvm(662): in
Lcan/example/android/CanportReceive;.get:()Ljava/lang/String;
Can anybody suggest me any solution to solve this. The attached files are
my java and JNI files.
Thanks in advance,
Regards,
Salih
--
unsubscribe: [email protected]
website: http://groups.google.com/group/android-porting
package can.example.android;
public class CanportReceive {
static {
System.loadLibrary("can_port");
}
public native String get();
}
package can.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log; //rekha
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class can extends Activity {
canport canportnative;
CanportReceive canportnativeReceive;
String s;
EditText Receive , Emission;
public static final String LOG_TAG= "NANO"; //rekha
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Emission = (EditText) findViewById(R.id.EditTextEmission);
s = Emission.getText().toString();
Log.v(LOG_TAG, "Emission.getText().toString() : " + s ); //rekha
Receive = (EditText) findViewById(R.id.EditTextReceive);
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(sendListener);
Log.v(LOG_TAG, "sendListener : " + sendListener ); //rekha
Button receive = (Button) findViewById(R.id.receive);
receive.setOnClickListener(receiveListener); //rekha
}
private OnClickListener sendListener = new OnClickListener()
{
public void onClick(View v)
{
String s = Emission.getText().toString();
Log.v(LOG_TAG, "sendListener < > Emission.getText().toString() : " + s ); //rekha
// Receive.setText(Emission.getText());
canportnative = new canport();
Log.v(LOG_TAG, "sendListener < > canportnative : " + canportnative ); //rekha
canportnative.add(s);
}
};
private OnClickListener receiveListener = new OnClickListener()
{
public void onClick(View v)
{
canportnativeReceive = new CanportReceive();
Log.v(LOG_TAG, "receiveListener < > canportnativeReceive : " + canportnativeReceive ); //rekha
String receiveText = canportnativeReceive.get();
Log.v(LOG_TAG, "receiveListener < > receiveText : " + receiveText ); //rekha
Receive.setText(receiveText);
}
};
}/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class can_example_android_canport */
#ifndef _Included_can_example_android_canport
#define _Included_can_example_android_canport
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: can_example_android_canport
* Method: add
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT void JNICALL Java_can_example_android_canport_add
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
package can.example.android;
public class canport {
static {
System.loadLibrary("can_port");
}
public native void add( String s );
//private native void open();
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/types.h>
#include <jni.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include "can_example_android_canport.h"
#ifndef PF_CAN
#define PF_CAN 29
#endif
#ifndef AF_CAN
#define AF_CAN PF_CAN
#endif
JNIEXPORT void JNICALL Java_can_example_android_canport_add(JNIEnv *env, jobject obj,jstring path)
{
int skt;
printf("###IWAVE Java_can_example_android_canport_add\r\n");
/* Create the socket */
if ((skt = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
printf ("###IWAVE Java_can_example_android_canport_add Socket error\r\n");
perror("socket");
return 1;
}
/* Locate the interface you wish to use */
struct ifreq ifr;
strcpy(ifr.ifr_name, "can0");
ioctl(skt, SIOCGIFINDEX, &ifr);
printf ("###IWAVE Java_can_example_android_canport_add Locate the interface you\r\n");
/* Select that CAN interface, and bind the socket to it. */
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( skt, (struct sockaddr*)&addr, sizeof(addr) );
printf ("###IWAVE Java_can_example_android_canport_add Select that CAN interface\r\n");
/* Send a message to the CAN bus */
struct can_frame frame;
frame.can_id = 0x123;
printf ("###IWAVE Java_can_example_android_canport_add Send a message to the CAN\r\n");
/* converting string to hex */
const jbyte *str;
//str = (char*)malloc(100 * sizeof (char));
str = (*env)->GetStringUTFChars(env, path, 0); //GetStringUTFChars to correctly print the string passed to it from a Java application
/*if(str == NULL){
return NULL;
}
char buffer[21]="";
char *pbuffer = buffer;
int len = strlen( str );
int i;
for(i = 0; i < len ;i++ )
{
sprintf(pbuffer, "%x", str[i]);
pbuffer +=2;
}*/
strcpy( frame.data, str );
//(*env)->ReleaseStringUTFChars(env, path, str);
printf ("###IWAVE Java_can_example_android_canport_add )->ReleaseStringUTFChars\r\n");
frame.can_dlc = strlen( frame.data );
int bytes_sent = write( skt, &frame, sizeof(frame) );
return 0;
}
JNIEXPORT jstring JNICALL Java_can_example_android_CanportReceive_get(JNIEnv *env, jobject obj)
{
printf ("###IWAVE Java_can_example_android_CanportReceive_get\r\n");
int skt; int nbytes;int i;
/* Create the socket */
if ((skt = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
printf ("###IWAVE Java_can_example_android_CanportReceive_get socket err\r\n");
perror("socket");
return 1;
}
/* Locate the interface you wish to use */
struct ifreq ifr;
strcpy(ifr.ifr_name, "can0");
ioctl(skt, SIOCGIFINDEX, &ifr);
/* Select that CAN interface, and bind the socket to it. */
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind( skt, (struct sockaddr*)&addr, sizeof(addr) );
struct can_frame frame;
/* Read a message back from the CAN bus */
printf ("###IWAVE Java_can_example_android_CanportReceive_get socket message back from the\r\n");
int bytes_read = read( skt, &frame, sizeof(frame) );
char *buf = (char *)(frame.data);
canid_t id = frame.can_id;
return (*env)->NewStringUTF(env, buf);
}