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 <br>int status = m_jvm->AttachCurrentThread(&env, NULL) but it does not solve problem because code is running on different thread agan.<br>
<br>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<br>because it is still possible to run bluetooth in different thread than QApplication .<br>
<br><br>Kate<br><br><div class="gmail_quote">On Thu, Nov 8, 2012 at 1:52 PM, Lauri Laanmets <span dir="ltr"><<a href="mailto:lauri.laanmets@proekspert.ee" target="_blank">lauri.laanmets@proekspert.ee</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

  
    
  
  <div text="#000000" bgcolor="#FFFFFF">
    <div>Hi<br>
      <br>
      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".<br>
      <br>
      Can anybody comment, are Necessitas Java classes now available
      also in the other threads?<span><font color="#888888"><br>
      <br>
      Lauri</font></span><div><div><br>
      <br>
      <br>
      On 8.11.2012 13:39, Kate Alhola wrote:<br>
    </div></div></div><div><div>
    <blockquote type="cite">Hi, I have been doing little bit research about this
      issue. I just under investigation one simple function
      QBluetoothLocaldevice::name().<br>
      The function crashes if vm->GetEnv() is done in JINI_OnLoad() .
      When I moved that code inside of QBluetoothLocaldevice constructor<br>
      code works. I have not yet made any patches, this was just result
      of simple experiment.  <br>
      <br>
      I tried in some other code do same allocations in JINI_OnLoad and
      got errors that VM was allocated for wrong thread.<br>
      It looks that JINI_OnLoad is too early phase to allocate<br>
      <br>
      Before going forward with fix, I just like hear opinion am I at
      right track. The bthost is my experimental class<br>
      just to test code. initJini is called from constructor. Name()
      duplicates function from QBluetoothLocalDevice<br>
      <br>
      Then other related question, if I do patched libQtConnectivity,
      what is best way to use it ? Necessitas always <br>
      installs it's own and it can't be copied over without rooting the
      device.<br>
      <br>
      <br>
      Kate<br>
      <br>
      void BtHost::initJini()<br>
      {<br>
          void* venv = NULL;<br>
      <br>
          qDebug() <<  "BtHost:JNI_OnLoad()";<br>
          if (m_jvm->GetEnv(&venv, JNI_VERSION_1_4) != JNI_OK)<br>
          {<br>
             qDebug() << "Qt:GetEnv failed";<br>
             return;<br>
          }<br>
          jclass qtApplicationC;<br>
          env = reinterpret_cast<JNIEnv*>(venv);<br>
          btAdapterClass =
      env->FindClass("android/bluetooth/BluetoothAdapter");<br>
          if (btAdapterClass == NULL)<br>
          {<br>
           qDebug() << "Qt:Native registration unable to find
      class android/bluetooth/BluetoothAdapter";<br>
          }<br>
      <br>
          jmethodID getDefaultAdapterID
      =env->GetStaticMethodID(btAdapterClass, "getDefaultAdapter",
      "()Landroid/bluetooth/BluetoothAdapter;");<br>
          if(getDefaultAdapterID == NULL){<br>
              qDebug() << "Qt:Native registration unable to get
      method ID: getDefaultAdapter of
      android/bluetooth/BluetoothAdapter";<br>
          }<br>
      <br>
          btAdapterObject =
      env->CallStaticObjectMethod(btAdapterClass,
      getDefaultAdapterID);<br>
          if(btAdapterObject == NULL){<br>
              qDebug() << "Qt:Device does not support Bluetooth";<br>
          }<br>
          btAdapterObject = env->NewGlobalRef(btAdapterObject);<br>
      }<br>
      <br>
      QString BtHost::name() const<br>
      {<br>
          qDebug() << " BtHost::name()";<br>
              static jmethodID getNameID =
      env->GetMethodID(btAdapterClass, "getName",
      "()Ljava/lang/String;");<br>
                  qDebug() << " QBluetoothLocalDevice::name()
      env->GetMethodID";<br>
              jstring peerName = (jstring)
      env->CallObjectMethod(btAdapterObject, getNameID);<br>
                 qDebug() << " QBluetoothLocalDevice::name()
      env->CallObjectMethod";<br>
              jboolean isCopy;<br>
              const jchar* name = env->GetStringChars(peerName,
      &isCopy);<br>
              QString qtName = QString::fromUtf16(name,
      env->GetStringLength(peerName));<br>
               qDebug() << " QBluetoothLocalDevice::name() name="
      << qtName;<br>
              env->ReleaseStringChars(peerName, name);<br>
               qDebug() <<" QBluetoothLocalDevice::name() exit";<br>
              return qtName;<br>
      <br>
      <br>
      }<br>
      <br>
      <br>
      <div class="gmail_extra"><br>
        <br>
        <div class="gmail_quote">
          On Thu, Nov 1, 2012 at 7:29 PM, Kate Alhola <span dir="ltr"><<a href="mailto:kate.alhola@gmail.com" target="_blank">kate.alhola@gmail.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div>On Mon, Oct 29, 2012 at 10:59 PM, Micke Prag
              <span dir="ltr"><<a href="mailto:micke.prag@telldus.se" target="_blank">micke.prag@telldus.se</a>></span>
              wrote:<br>
            </div>
            <div class="gmail_extra">
              <div class="gmail_quote">
                <div>
                  <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                    <div>
                      <div>Hi,</div>
                      <div><br>
                      </div>
                      <div>It only happens after I call start(). Only
                        creating the object works fine.</div>
                      <div>Thank you for looking into this.</div>
                    </div>
                  </blockquote>
                </div>
                <div><br>
                  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.<br>
                  <br>
                  Here is two cases where I got same bug<br>
                  <br>
                  Code:<br>
                       qDebug() << "Turning bluetooth on";<br>
                        localDevice->powerOn();<br>
                  <br>
                  <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(60,60,60)">D/Qt
                      ( 4746): Turning bluetooth on </span></p>
                  <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)">E/dalvikvm(
                      4746): JNI ERROR (app bug): accessed stale local
                      reference 0x9f50001d (index 7 in a table of size
                      4)</span></p>
                  <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)">E/dalvikvm(
                      4746): VM aborting</span></p>
                  <br>
                  Or<br>
                  <br>
                  from:<br>
                    wmSocket = new QBluetoothSocket(
                  <div>
                    QBluetoothSocket::RfcommSocket);<br>
                    <br>
                        qDebug() << "btHost::tryConnect()"
                    << wmPulse.address().toString() << ":"
                    << PORT;<br>
                        wmSocket->connectToService(wmPulse.address(),
                    PORT);<br>
                    <br>
                    <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(60,60,60)">D/Qt
                        ( 6195): btHost::tryConnect()
                        "83:7C:82:00:39:31" : 5 </span></p>
                    <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)">E/dalvikvm(
                        6195): JNI ERROR (app bug): accessed stale local
                        reference 0xb3b0001d (index 7 in a table of size
                        0)</span></p>
                    <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)">E/dalvikvm(
                        6195): VM aborting</span></p>
                    <span><font color="#888888">
                        <p style="margin:0px;text-indent:0px">
                          <br>
                          <span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)"></span></p>
                        <p style="margin:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:rgb(190,20,20)">Kate<br>
                          </span></p>
                      </font></span></div>
                  <br>
                   </div>
                <div>
                  <div>
                    <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                      <div>
                        <div><br>
                        </div>
                        <div>Regards</div>
                        <span><font color="#888888">
                            <div>Micke</div>
                          </font></span></div>
                      <div>
                        <div>
                          <p style="color:rgb(160,160,168)">On Monday 29
                            October 2012 at 21:54, Lauri Laanmets wrote:</p>
                          <blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
                            <span>
                              <div>
                                <div>
                                  <div>Hi<br>
                                    <br>
                                    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?<br>
                                    <br>
                                    I have lost my old devel set-up, It
                                    takes me a few days to get on that
                                    issue.<br>
                                    <br>
                                    Regards<br>
                                    Lauri<br>
                                    <br>
                                    <br>
                                    On 29.10.2012 22:31, Micke Prag
                                    wrote:<br>
                                  </div>
                                  <blockquote type="cite">
                                    <div>
                                      <div> Hello, </div>
                                      <div><br>
                                      </div>
                                      <div>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?</div>
                                      <div><br>
                                      </div>
                                      <div>The output from logcat can be
                                        found here: <a href="http://pastebin.com/khcj28Xy" target="_blank">http://pastebin.com/khcj28Xy</a></div>
                                      <div>This is what I am doing:</div>
                                      <div>QtMobility::QBluetoothDeviceDiscoveryAgent
                                        agent;</div>
                                      <div>agent.start();</div>
                                      <div><br>
                                      </div>
                                      <div>libs.xml:</div>
                                      <div>
                                        <div><?xml version='1.0'
                                          encoding='utf-8'?></div>
                                        <div> <resources></div>
                                        <div>   <array
                                          name="qt_libs"></div>
                                        <div>   
                                           <item>QtCore</item></div>
                                        <div>   
                                           <item>QtGui</item></div>
                                        <div>   
                                           <item>QtNetwork</item></div>
                                        <div>   
                                           <item>QtDeclarative</item></div>
                                        <div>   
                                           <item>QtXml</item></div>
                                        <div>   
                                           <item>QtOpenGL</item></div>
                                        <div>   
                                           <item>QtSystemInfo</item></div>
                                        <div>   
                                           <item>QtConnectivity</item></div>
                                        <div>   </array></div>
                                        <div>  <array
                                          name="bundled_libs"/></div>
                                        <div></resources></div>
                                        <div><br>
                                        </div>
                                        <div>I also have this in
                                          AndroidManifest.xml:</div>
                                        <div><uses-permission
                                          android:name="android.permission.BLUETOOTH"
                                          /></div>
                                        <div><br>
                                        </div>
                                        <div>Bluetooth is activated on
                                          the device.</div>
                                        <div><br>
                                        </div>
                                        <div>The device is a Cube
                                          U30GT-H running Android 4.1.1</div>
                                      </div>
                                      <div>
                                        <div><br>
                                        </div>
                                        <div>-- </div>
                                        <div>Micke Prag</div>
                                      </div>
                                      <br>
                                      <fieldset></fieldset>
                                      <br>
                                      <pre>_______________________________________________
Necessitas-devel mailing list
<a href="mailto:Necessitas-devel@kde.org" target="_blank">Necessitas-devel@kde.org</a>
<a href="https://mail.kde.org/mailman/listinfo/necessitas-devel" target="_blank">https://mail.kde.org/mailman/listinfo/necessitas-devel</a>
</pre>
                                    </div>
                                  </blockquote>
                                  <br>
                                </div>
                              </div>
                            </span> </blockquote>
                          <div> <br>
                          </div>
                        </div>
                      </div>
                      <br>
                      _______________________________________________<br>
                      Necessitas-devel mailing list<br>
                      <a href="mailto:Necessitas-devel@kde.org" target="_blank">Necessitas-devel@kde.org</a><br>
                      <a href="https://mail.kde.org/mailman/listinfo/necessitas-devel" target="_blank">https://mail.kde.org/mailman/listinfo/necessitas-devel</a><br>
                      <br>
                    </blockquote>
                  </div>
                </div>
              </div>
              <br>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </div></div></div>

<br>_______________________________________________<br>
Necessitas-devel mailing list<br>
<a href="mailto:Necessitas-devel@kde.org" target="_blank">Necessitas-devel@kde.org</a><br>
<a href="https://mail.kde.org/mailman/listinfo/necessitas-devel" target="_blank">https://mail.kde.org/mailman/listinfo/necessitas-devel</a><br>
<br></blockquote></div><br>