Example #1
0
void NativeMap::open()
{
	qDebug() << "NativeMap::open()...";
	qDebug() << "location = " << location;
	qDebug() << "longitude = " << longitude;
	qDebug() << "latitude = " << latitude;
	qDebug() << "zoom = " << zoom;

#ifdef Q_OS_ANDROID
	qDebug() << "NativeMap::open()...ANDROID";
	QAndroidJniObject activity = QtAndroid::androidActivity();

	if ( activity.isValid() )
	{
		/*QAndroidJniEnvironment env;
		jclass cls = env->FindClass( "wpp.android.AMapActivity" );
		qDebug() << "cls:" << cls;
		QAndroidJniObject anIntent("android/content/Intent","(Landroid/content/Context;Ljava/lang/Class;)V",
								 activity.object<jobject>(), cls);
		qDebug() << "anIntent.isvalid=" << anIntent.isValid();
		*/

		// Equivalent to Jave code: 'Intent intent = new Intent();'
		QAndroidJniObject intent("android/content/Intent","()V");
		if ( intent.isValid() )
		{
			QAndroidJniObject packageName = activity.callObjectMethod("getPackageName","()Ljava/lang/String;");
			qDebug() << "packageName.isValid=" << packageName.isValid();
			qDebug() << "packageName=" << packageName.toString();

			QAndroidJniObject param2 = QAndroidJniObject::fromString("wpp.android.AMapActivity");

			if ( packageName.isValid() && param2.isValid() )
			{
				qDebug() << "111...";
				// Equivalent to Jave code: 'intent.setClassName("com.kuulabu.android.app", "com.kuulabu.android.app.BasicMapActivity");'
				intent.callObjectMethod("setClassName",
										"(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
										packageName.object<jobject>(),
										param2.object<jobject>());

				QAndroidJniObject locationKey = QAndroidJniObject::fromString("MAP_LOCATION");
				QAndroidJniObject locationValue = QAndroidJniObject::fromString(this->location);
				intent.callObjectMethod(
							"putExtra","(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
							locationKey.object<jstring>(), locationValue.object<jstring>());

				QAndroidJniObject longitudeKey = QAndroidJniObject::fromString("MAP_LONGITUDE");
				QAndroidJniObject longitudeValue = QAndroidJniObject::fromString(QString::number(this->longitude));
				intent.callObjectMethod(
							"putExtra","(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
							longitudeKey.object<jstring>(), longitudeValue.object<jstring>());

				QAndroidJniObject latitudeKey = QAndroidJniObject::fromString("MAP_LATITUDE");
				QAndroidJniObject latitudeValue = QAndroidJniObject::fromString(QString::number(this->latitude));
				intent.callObjectMethod(
							"putExtra","(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
							latitudeKey.object<jstring>(), latitudeValue.object<jstring>());

				QAndroidJniObject zoomKey = QAndroidJniObject::fromString("MAP_ZOOM");
				QAndroidJniObject zoomValue = QAndroidJniObject::fromString(QString::number(this->zoom));
				intent.callObjectMethod(
							"putExtra","(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
							zoomKey.object<jstring>(), zoomValue.object<jstring>());

				qDebug() << "222...";
				// Equivalent to Jave code: 'startActivity(intent);'
				//activity.callMethod<void>("startActivity","(Landroid/content/Intent;)V",intent.object<jobject>());
				//QtAndroid::startActivity(intent, 0, 0);
				int LOCATE_EVENT_VENUE = 1;
				QtAndroid::startActivity(intent, LOCATE_EVENT_VENUE, this);
				qDebug() << "333...";
			}

		}
	}
#endif

}
static QVariantMap createVariantMap(jobject data) {
    QVariantMap res;

    QAndroidJniEnvironment env;
    /* Reference : https://community.oracle.com/thread/1549999 */

    // Get the HashMap Class
    jclass jclass_of_hashmap = (env)->GetObjectClass(data);

    // Get link to Method "entrySet"
    jmethodID entrySetMethod = (env)->GetMethodID(jclass_of_hashmap, "entrySet", "()Ljava/util/Set;");

    // Invoke the "entrySet" method on the HashMap object
    jobject jobject_of_entryset = env->CallObjectMethod(data, entrySetMethod);

    // Get the Set Class
    jclass jclass_of_set = (env)->FindClass("java/util/Set"); // Problem during compilation !!!!!

    if (jclass_of_set == 0) {
         qWarning() << "java/util/Set lookup failed\n";
         return res;
    }

    jclass jclass_of_string = env->FindClass("java/lang/String");
    jclass jclass_of_integer = env->FindClass("java/lang/Integer");
    jclass jclass_of_boolean = env->FindClass("java/lang/Boolean");


    // Get link to Method "iterator"
    jmethodID iteratorMethod = env->GetMethodID(jclass_of_set, "iterator", "()Ljava/util/Iterator;");

    // Invoke the "iterator" method on the jobject_of_entryset variable of type Set
    jobject jobject_of_iterator = env->CallObjectMethod(jobject_of_entryset, iteratorMethod);

    // Get the "Iterator" class
    jclass jclass_of_iterator = (env)->FindClass("java/util/Iterator");

    // Get link to Method "hasNext"
    jmethodID hasNextMethod = env->GetMethodID(jclass_of_iterator, "hasNext", "()Z");

    jmethodID nextMethod = env->GetMethodID(jclass_of_iterator, "next", "()Ljava/lang/Object;");

    while (env->CallBooleanMethod(jobject_of_iterator, hasNextMethod) ) {
        QAndroidJniObject entry = env->CallObjectMethod(jobject_of_iterator,nextMethod);
        QAndroidJniObject key = entry.callObjectMethod("getKey","()Ljava/lang/Object;");
        QAndroidJniObject value = entry.callObjectMethod("getValue","()Ljava/lang/Object;");
        QString k = key.toString();

        if (!value.isValid())
            continue;

        if (env->IsInstanceOf(value.object<jobject>(),jclass_of_boolean)) {
            res[k] = QVariant::fromValue<bool>(value.callMethod<jboolean>("booleanValue","()Z"));
        } else if (env->IsInstanceOf(value.object<jobject>(),jclass_of_integer)) {
            res[k] = value.callMethod<jint>("intValue","()I");
        } else if (env->IsInstanceOf(value.object<jobject>(),jclass_of_string)) {
            QString v = value.toString();
            res[k] = v;
        }
    }

    if (env->ExceptionOccurred()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }

    // Delete local reference
    return res;
}
void DeviceDiscoveryBroadcastReceiver::onReceive(JNIEnv *env, jobject context, jobject intent)
{
    Q_UNUSED(context);
    Q_UNUSED(env);

    QAndroidJniObject intentObject(intent);
    const QString action = intentObject.callObjectMethod("getAction", "()Ljava/lang/String;").toString();

    qCDebug(QT_BT_ANDROID) << "DeviceDiscoveryBroadcastReceiver::onReceive() - event:" << action;

    if (action == valueForStaticField(JavaNames::BluetoothAdapter,
                                      JavaNames::ActionDiscoveryFinished).toString()) {
        emit finished();
    } else if (action == valueForStaticField(JavaNames::BluetoothAdapter,
               JavaNames::ActionDiscoveryStarted).toString()) {

    } else if (action == valueForStaticField(JavaNames::BluetoothDevice,
               JavaNames::ActionFound).toString()) {
        //get BluetoothDevice
        QAndroidJniObject keyExtra = valueForStaticField(JavaNames::BluetoothDevice,
                                     JavaNames::ExtraDevice);
        const QAndroidJniObject bluetoothDevice =
            intentObject.callObjectMethod("getParcelableExtra",
                                          "(Ljava/lang/String;)Landroid/os/Parcelable;",
                                          keyExtra.object<jstring>());

        if (!bluetoothDevice.isValid())
            return;

        const QString deviceName = bluetoothDevice.callObjectMethod<jstring>("getName").toString();
        const QBluetoothAddress deviceAddress(bluetoothDevice.callObjectMethod<jstring>("getAddress").toString());
        keyExtra = valueForStaticField(JavaNames::BluetoothDevice,
                                       JavaNames::ExtraRssi);

        int rssi = intentObject.callMethod<jshort>("getShortExtra",
                   "(Ljava/lang/String;S)S",
                   keyExtra.object<jstring>(),
                   0);
        const QAndroidJniObject bluetoothClass = bluetoothDevice.callObjectMethod("getBluetoothClass",
                "()Landroid/bluetooth/BluetoothClass;");
        if (!bluetoothClass.isValid())
            return;
        int classType = bluetoothClass.callMethod<jint>("getDeviceClass");


        static QList<qint32> services;
        if (services.count() == 0)
            services << QBluetoothDeviceInfo::PositioningService
                     << QBluetoothDeviceInfo::NetworkingService
                     << QBluetoothDeviceInfo::RenderingService
                     << QBluetoothDeviceInfo::CapturingService
                     << QBluetoothDeviceInfo::ObjectTransferService
                     << QBluetoothDeviceInfo::AudioService
                     << QBluetoothDeviceInfo::TelephonyService
                     << QBluetoothDeviceInfo::InformationService;

        //Matching BluetoothClass.Service values
        qint32 result = 0;
        qint32 current = 0;
        for (int i = 0; i < services.count(); i++) {
            current = services.at(i);
            int id = (current << 16);
            if (bluetoothClass.callMethod<jboolean>("hasService", "(I)Z", id))
                result |= current;
        }

        result = result << 13;
        classType |= result;

        QBluetoothDeviceInfo info(deviceAddress, deviceName, classType);
        info.setRssi(rssi);

        emit deviceDiscovered(info);
    }
}