chariotsolutions / phonegap-nfc

PhoneGap NFC Plugin
MIT License
706 stars 559 forks source link

App crashes after 3rd write to tag #150

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hey, thx for this great plugin!

I have a strange error, everytime my Cordova + Ionic app crashes after the third write. Reading works perfect 100 times. The data is written onto the tag but the app crashes afterwards. My device is a Galaxy Nexus with Android 4.3. The app is build with NFC plugin version 0.6.0 and Cordova is version 4.0.0

Im getting this error log after executing write to tag the third time: D/NfcPlugin( 9045): execute writeTag W/dalvikvm( 9045): threadid=24: thread exiting with uncaught exception (group=0x417ba700) E/AndroidRuntime( 9045): FATAL EXCEPTION: pool-1-thread-3 E/AndroidRuntime( 9045): java.lang.IllegalStateException: Close other technology first! E/AndroidRuntime( 9045): at android.nfc.Tag.setConnectedTechnology(Tag.java:414) E/AndroidRuntime( 9045): at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:78) E/AndroidRuntime( 9045): at android.nfc.tech.Ndef.connect(Ndef.java:72) E/AndroidRuntime( 9045): at com.chariotsolutions.nfc.plugin.NfcPlugin$1.run(NfcPlugin.java:240) E/AndroidRuntime( 9045): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) E/AndroidRuntime( 9045): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) E/AndroidRuntime( 9045): at java.lang.Thread.run(Thread.java:841) W/ActivityManager( 376): Force finishing activity com.ionicframework.insysapp600721/.insysApp D/CordovaLog( 9045): file:///android_asset/www/js/services.js: Line 201 : Wrote data to tag. I/Web Console( 9045): Wrote data to tag. at file:///android_asset/www/js/services.js:201

My Code:

//NFC Plugin

.factory('$nfc', [ '$q','$nfcMode' ,function ($q,$nfcMode) {
  return {
       registerHandler: function() {
             var deferred = $q.defer();
        nfc.addNdefListener(function(tagDiscover)
        {
         console.log("Event recieved");

        switch($nfcMode.getMode())
        {
        case 0: //schreiben
        var data = $nfcMode.getData();
        var message = [];
        console.log("Length:" + data.length);
        for(var i = 0; i < data.length;i++)
         {

            message.push(ndef.textRecord(data[i])) 
           // console.log(JSON.stringify(message))
         }
         //console.log(JSON.stringify(message));

            nfc.write(message, 
                function () {
                    console.log("Wrote data to tag.");

                }, 
                function (reason) {
                    console.log("NFC Haud ned: "+reason);

                }
            );  
        break;

        case 1: //lesen

          var message = tagDiscover.tag.ndefMessage[0]["payload"];
         console.log("TAG Complete:"+JSON.stringify(tagDiscover.tag));

         console.log("DATA:" + nfc.bytesToString(message ));

         return deferred.resolve(nfc.bytesToString(message));

        break;

        case 2: //record lesen
        var message = tagDiscover.tag.ndefMessage[$nfcMode.getIndex()]["payload"];
         console.log("TAG Complete:"+JSON.stringify(tagDiscover.tag));

         console.log("DATA:" + nfc.bytesToString(message ));

         return deferred.resolve(nfc.bytesToString(message));

        break;

        default:
        console.log("Default case");

        }

        },
        function()
        {
         $nfcMode.setNFC(1);           
        },
        function()
        {
         $nfcMode.setNFC(0);          
        });

        return deferred.promise;

       }

     }}])

Any idea??

Thx for your help!

don commented 9 years ago

Can you see if you can get it to crash with this example? https://github.com/don/phonegap-nfc-issue-150

Can you try your app on other phones? Does it matter how fast or slow you write to the tags?

ghost commented 9 years ago

Thx for you quick response.

No, your example runs without any error. Yes, i tried it also on a Sony device.

I found a workaround, so it doesn't crash anymore. I parse the event to a function. Like in your examples.

Controller Code:

$scope.writeNFC3= function(){

     data = ["1234557","Data","setLed(2)","LED 400,400"];

     $nfcMode.setData(data);
     $nfcMode.setMode(0);
    $nfc.registerHandler().then(function(event){

       $nfc.actionTag(event).then(function(res)
       {alert("Event result: "+res);}); 

    });

Factory Code:

.factory('$nfc', [ '$q','$nfcMode' ,function ($q,$nfcMode) {

  return {

       actionTag: function(tagDiscover)
       {
        var deferred = $q.defer();

        switch($nfcMode.getMode())
        {
        case 0: //schreiben
        var data = $nfcMode.getData();
        var message = [];
        console.log("Length:" + data.length);
        for(var i = 0; i < data.length;i++)
         {

            message.push(ndef.textRecord(data[i])) 
           // console.log(JSON.stringify(message))
         }
         //console.log(JSON.stringify(message));

         nfc.write(message, 
                function () {
                    console.log("Wrote data to tag.");

          deferred.resolve("okay"); 

                }, 
                function (reason) {
                    console.log("NFC Haud ned: "+reason);

          deferred.resolve(reason);        

                }
            );  
          break;

        case 1:

          var message = tagDiscover.tag.ndefMessage[0]["payload"];
         console.log("TAG Complete:"+JSON.stringify(tagDiscover.tag));

         console.log("DATA:" + nfc.bytesToString(message ));

         deferred.resolve(nfc.bytesToString(message));

        break;

        case 2: 
        var message = tagDiscover.tag.ndefMessage[$nfcMode.getIndex()]["payload"];
         console.log("TAG Complete:"+JSON.stringify(tagDiscover.tag));

         console.log("DATA:" + nfc.bytesToString(message ));

          deferred.resolve(nfc.bytesToString(message));

        break;

        default:
        console.log("Default case");

        }

        return deferred.promise;  

       },

       registerHandler: function() {
             var deferred = $q.defer();

        nfc.addNdefListener(function(tagDiscover)
        {
         console.log("Event recieved");
          return deferred.resolve(tagDiscover);
        },
        function()
        {
         $nfcMode.setNFC(1);           
        },
        function()
        {
         $nfcMode.setNFC(0);          
        });

        return deferred.promise;

       }

     }}])

I guess if you use the event in the addNdefListener function causes the error.

Now the App runs perfect.

don commented 9 years ago

Glad it's working

Oussie00 commented 4 years ago

Hi, I'm having the same issue. As soon as I try to write or read the second time the app suddenly crashes. I found this error in logcat:

   `2020-03-24 22:34:04.977 12911-13071/be.della7 E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-3
Process: XXXXX, PID: 12911
java.lang.IllegalStateException: Close other technology first!
    at android.nfc.Tag.setConnectedTechnology(Tag.java:458)
    at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:78)
    at android.nfc.tech.Ndef.connect(Ndef.java:71)
    at com.chariotsolutions.nfc.plugin.NfcPlugin.lambda$writeNdefMessage$2$NfcPlugin(NfcPlugin.java:344)
    at com.chariotsolutions.nfc.plugin.NfcPlugin$$Lambda$2.run(Unknown Source:6)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)`

Any idea about what the problem could be? I'm using Ionic 4 with the latest version of the plugin,

Thanks in advance for helping!

RTsosssoldi commented 3 years ago

Hi, I'm having the same issue. As soon as I try to write or read the second time the app suddenly crashes. I found this error in logcat:

   `2020-03-24 22:34:04.977 12911-13071/be.della7 E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-3
Process: XXXXX, PID: 12911
java.lang.IllegalStateException: Close other technology first!
    at android.nfc.Tag.setConnectedTechnology(Tag.java:458)
    at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:78)
    at android.nfc.tech.Ndef.connect(Ndef.java:71)
    at com.chariotsolutions.nfc.plugin.NfcPlugin.lambda$writeNdefMessage$2$NfcPlugin(NfcPlugin.java:344)
    at com.chariotsolutions.nfc.plugin.NfcPlugin$$Lambda$2.run(Unknown Source:6)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)`

Any idea about what the problem could be? I'm using Ionic 4 with the latest version of the plugin,

Thanks in advance for helping!

Hey @Oussie00, any news?