Saturday, April 20, 2013

jni jnienv GetBooleanField example c c++ java


Get<type>Field Routines - GetBooleanField


NativeType Get<type>Field(JNIEnv *env, jobject obj,
jfieldID fieldID);

This family of accessor routines returns the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling GetFieldID(). (GetBooleanField)

The following table describes the Get<type>Field routine name and result type. You should replace type in Get<type>Field with the Java type of the field, or use one of the actual routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-1a Get<type>Field Family of Accessor Routines

Get<type>Field Routine Name

Native Type

GetObjectField()

jobject

GetBooleanField()

jboolean

GetByteField()

jbyte

GetCharField()

jchar

GetShortField()

jshort

GetIntField()

jint

GetLongField()

jlong

GetFloatField()

jfloat

GetDoubleField()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table:
Table 4-1b Get<type>Field Family of Accessor Routines

Get<type>Field Routine Name

Index

GetObjectField()
95

GetBooleanField()
96

GetByteField()
97

GetCharField()
98

GetShortField()
99

GetIntField()
100

GetLongField()
101

GetFloatField()
102

GetDoubleField()
103
PARAMETERS:

env: the JNI interface pointer.

obj: a Java object (must not be NULL).

fieldID: a valid field ID.

RETURNS:

Returns the content of the field.

Example - GetBooleanField

static void initSocketNative(JNIEnv *env, jobject obj) {
102
#ifdef HAVE_BLUETOOTH
103
    LOGV(__FUNCTION__);
104
#ifdef USE_BM3_BLUETOOTH
105
    // BM3: RFCOMM-only assumption
106
    jint type;
107
    type = env->GetIntField(obj, field_mType);
108
    if (TYPE_RFCOMM != type) {
109
        jniThrowIOException(env, ENOSYS);
110
        return;
111
    }
112

113
    int fd = 0; /* Bogus value for now--for BM3 we initialize the socket on connect/bind */
114
#else
115
    int fd;
116
    int lm = 0;
117
    int sndbuf;
118
    jboolean auth;
119
    jboolean encrypt;
120
    jint type;
121

122
    type = env->GetIntField(obj, field_mType);
123

124
    switch (type) {
125
    case TYPE_RFCOMM:
126
        fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
127
        break;
128
    case TYPE_SCO:
129
        fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
130
        break;
131
    case TYPE_L2CAP:
132
        fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
133
        break;
134
    default:
135
        jniThrowIOException(env, ENOSYS);
136
        return;
137
    }
138

139
    if (fd < 0) {
140
        LOGV("socket() failed, throwing");
141
        jniThrowIOException(env, errno);
142
        return;
143
    }
144

145
    auth = env->GetBooleanField(obj, field_mAuth);
146
    encrypt = env->GetBooleanField(obj, field_mEncrypt);
147

148
    /* kernel does not yet support LM for SCO */
149
    switch (type) {
150
    case TYPE_RFCOMM:
151
        lm |= auth ? RFCOMM_LM_AUTH : 0;
152
        lm |= encrypt ? RFCOMM_LM_ENCRYPT : 0;
153
        lm |= (auth && encrypt) ? RFCOMM_LM_SECURE : 0;
154
        break;
155
    case TYPE_L2CAP:
156
        lm |= auth ? L2CAP_LM_AUTH : 0;
157
        lm |= encrypt ? L2CAP_LM_ENCRYPT : 0;
158
        lm |= (auth && encrypt) ? L2CAP_LM_SECURE : 0;
159
        break;
160
    }
161

162
    if (lm) {
163
        if (setsockopt(fd, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm))) {
164
            LOGV("setsockopt(RFCOMM_LM) failed, throwing");
165
            jniThrowIOException(env, errno);
166
            return;
167
        }
168
    }
169

170
    if (type == TYPE_RFCOMM) {
171
        sndbuf = RFCOMM_SO_SNDBUF;
172
        if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))) {
173
            LOGV("setsockopt(SO_SNDBUF) failed, throwing");
174
            jniThrowIOException(env, errno);
175
            return;
176
        }
177
    }
178

179
    LOGV("...fd %d created (%s, lm = %x)", fd, TYPE_AS_STR(type), lm);
180
#endif /* USE_BM3_BLUETOOTH */
181
    initSocketFromFdNative(env, obj, fd);
182
    return;
183
#endif
184
    jniThrowIOException(env, ENOSYS);
185
}