Crash when using Bluetooth

Kate Alhola kate.alhola at gmail.com
Wed Dec 12 14:21:41 UTC 2012


Hi, I have finally had time to look this issue.  The problem clearly is
that JNI_OnLoad is running on different thread thanJavaVM instance. I Tried
quick fix with
int status = m_jvm->AttachCurrentThread(&env, NULL) but it does not solve
problem because code is running on different thread agan.

It looks that only fix is to add JNIEnv*  env to all Bluetooth private
classes like class QBluetoothLocalDevicePrivate and then use
>AttachCurrentThread to get environment
because it is still possible to run bluetooth in different thread than
QApplication .


Kate

On Thu, Nov 8, 2012 at 1:52 PM, Lauri Laanmets <lauri.laanmets at proekspert.ee
> wrote:

>  Hi
>
> I'm quite sure you are on the right track. I remember I used to do quite a
> lot of things in "JNI_OnLoad " because only in that case it was possible to
> access the Necessitas Java classes. I remember an e-mail about redesigning
> that part, to be able to access Necessitas Java classes also from other
> threads. I would suspect something has changed in this field. I started
> working on it but since my "/local/data" is inaccessible, I kind of a "lost
> a little motivation".
>
> Can anybody comment, are Necessitas Java classes now available also in the
> other threads?
>
> Lauri
>
>
>
> On 8.11.2012 13:39, Kate Alhola wrote:
>
> Hi, I have been doing little bit research about this issue. I just under
> investigation one simple function QBluetoothLocaldevice::name().
> The function crashes if vm->GetEnv() is done in JINI_OnLoad() . When I
> moved that code inside of QBluetoothLocaldevice constructor
> code works. I have not yet made any patches, this was just result of
> simple experiment.
>
> I tried in some other code do same allocations in JINI_OnLoad and got
> errors that VM was allocated for wrong thread.
> It looks that JINI_OnLoad is too early phase to allocate
>
> Before going forward with fix, I just like hear opinion am I at right
> track. The bthost is my experimental class
> just to test code. initJini is called from constructor. Name() duplicates
> function from QBluetoothLocalDevice
>
> Then other related question, if I do patched libQtConnectivity, what is
> best way to use it ? Necessitas always
> installs it's own and it can't be copied over without rooting the device.
>
>
> Kate
>
> void BtHost::initJini()
> {
>     void* venv = NULL;
>
>     qDebug() <<  "BtHost:JNI_OnLoad()";
>     if (m_jvm->GetEnv(&venv, JNI_VERSION_1_4) != JNI_OK)
>     {
>        qDebug() << "Qt:GetEnv failed";
>        return;
>     }
>     jclass qtApplicationC;
>     env = reinterpret_cast<JNIEnv*>(venv);
>     btAdapterClass = env->FindClass("android/bluetooth/BluetoothAdapter");
>     if (btAdapterClass == NULL)
>     {
>      qDebug() << "Qt:Native registration unable to find class
> android/bluetooth/BluetoothAdapter";
>     }
>
>     jmethodID getDefaultAdapterID =env->GetStaticMethodID(btAdapterClass,
> "getDefaultAdapter", "()Landroid/bluetooth/BluetoothAdapter;");
>     if(getDefaultAdapterID == NULL){
>         qDebug() << "Qt:Native registration unable to get method ID:
> getDefaultAdapter of android/bluetooth/BluetoothAdapter";
>     }
>
>     btAdapterObject = env->CallStaticObjectMethod(btAdapterClass,
> getDefaultAdapterID);
>     if(btAdapterObject == NULL){
>         qDebug() << "Qt:Device does not support Bluetooth";
>     }
>     btAdapterObject = env->NewGlobalRef(btAdapterObject);
> }
>
> QString BtHost::name() const
> {
>     qDebug() << " BtHost::name()";
>         static jmethodID getNameID = env->GetMethodID(btAdapterClass,
> "getName", "()Ljava/lang/String;");
>             qDebug() << " QBluetoothLocalDevice::name() env->GetMethodID";
>         jstring peerName = (jstring)
> env->CallObjectMethod(btAdapterObject, getNameID);
>            qDebug() << " QBluetoothLocalDevice::name()
> env->CallObjectMethod";
>         jboolean isCopy;
>         const jchar* name = env->GetStringChars(peerName, &isCopy);
>         QString qtName = QString::fromUtf16(name,
> env->GetStringLength(peerName));
>          qDebug() << " QBluetoothLocalDevice::name() name=" << qtName;
>         env->ReleaseStringChars(peerName, name);
>          qDebug() <<" QBluetoothLocalDevice::name() exit";
>         return qtName;
>
>
> }
>
>
>
>
>  On Thu, Nov 1, 2012 at 7:29 PM, Kate Alhola <kate.alhola at gmail.com>wrote:
>
>> On Mon, Oct 29, 2012 at 10:59 PM, Micke Prag <micke.prag at telldus.se>wrote:
>>
>>>  Hi,
>>>
>>>  It only happens after I call start(). Only creating the object works
>>> fine.
>>> Thank you for looking into this.
>>>
>>
>> I noticed that exactly same thing happens with QBluetoothSocket and
>> QBluetoothLocalDevice, in both cases they happen If I try use some member
>> functions. Creation of object goes OK in both of cases. I posted report to
>> android-qt list but may be this is better forum.
>>
>> Here is two cases where I got same bug
>>
>> Code:
>>      qDebug() << "Turning bluetooth on";
>>       localDevice->powerOn();
>>
>> D/Qt ( 4746): Turning bluetooth on
>>
>> E/dalvikvm( 4746): JNI ERROR (app bug): accessed stale local reference
>> 0x9f50001d (index 7 in a table of size 4)
>>
>> E/dalvikvm( 4746): VM aborting
>>
>> Or
>>
>> from:
>>   wmSocket = new QBluetoothSocket(
>> QBluetoothSocket::RfcommSocket);
>>
>>     qDebug() << "btHost::tryConnect()" << wmPulse.address().toString() <<
>> ":" << PORT;
>>     wmSocket->connectToService(wmPulse.address(), PORT);
>>
>> D/Qt ( 6195): btHost::tryConnect() "83:7C:82:00:39:31" : 5
>>
>> E/dalvikvm( 6195): JNI ERROR (app bug): accessed stale local reference
>> 0xb3b0001d (index 7 in a table of size 0)
>>
>> E/dalvikvm( 6195): VM aborting
>>
>>
>>  Kate
>>
>>
>>
>>>
>>>  Regards
>>>  Micke
>>>
>>> On Monday 29 October 2012 at 21:54, Lauri Laanmets wrote:
>>>
>>>   Hi
>>>
>>> I guess that is addressed to me :). Crash is definitely a bug,
>>> regardless of Android permissions or missing Java files. Can you verify if
>>> it crashes on creating the DiscoveryAgent object or when starting the
>>> discovery?
>>>
>>> I have lost my old devel set-up, It takes me a few days to get on that
>>> issue.
>>>
>>> Regards
>>> Lauri
>>>
>>>
>>> On 29.10.2012 22:31, Micke Prag wrote:
>>>
>>>  Hello,
>>>
>>>  I know that the Bluetooth support in Necessitas is not complete but I
>>> wanted to try it out anyway. I am only trying to discover bluetooth devices
>>> but my application crashes. I don't know if I am doing something wrong or
>>> if it is not supported yet?
>>>
>>>  The output from logcat can be found here: http://pastebin.com/khcj28Xy
>>> This is what I am doing:
>>> QtMobility::QBluetoothDeviceDiscoveryAgent agent;
>>> agent.start();
>>>
>>>  libs.xml:
>>>  <?xml version='1.0' encoding='utf-8'?>
>>>  <resources>
>>>    <array name="qt_libs">
>>>      <item>QtCore</item>
>>>      <item>QtGui</item>
>>>      <item>QtNetwork</item>
>>>      <item>QtDeclarative</item>
>>>      <item>QtXml</item>
>>>      <item>QtOpenGL</item>
>>>      <item>QtSystemInfo</item>
>>>      <item>QtConnectivity</item>
>>>    </array>
>>>   <array name="bundled_libs"/>
>>> </resources>
>>>
>>>  I also have this in AndroidManifest.xml:
>>> <uses-permission android:name="android.permission.BLUETOOTH" />
>>>
>>>  Bluetooth is activated on the device.
>>>
>>>  The device is a Cube U30GT-H running Android 4.1.1
>>>
>>>  --
>>> Micke Prag
>>>
>>>
>>> _______________________________________________
>>> Necessitas-devel mailing listNecessitas-devel at kde.orghttps://mail.kde.org/mailman/listinfo/necessitas-devel
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Necessitas-devel mailing list
>>> Necessitas-devel at kde.org
>>> https://mail.kde.org/mailman/listinfo/necessitas-devel
>>>
>>>
>>
>
>
> _______________________________________________
> Necessitas-devel mailing list
> Necessitas-devel at kde.org
> https://mail.kde.org/mailman/listinfo/necessitas-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/necessitas-devel/attachments/20121212/c6c622db/attachment.html>


More information about the Necessitas-devel mailing list