6 Replies Latest reply on Jun 15, 2016 2:30 PM by Gurgue

    Phonegap and Bluetooth plugin

    Gurgue Level 1

      Hello everybody !

      First, I beg you pardon for my english...

       

      I am trying to use a cordova/phonegap plugin for bluetooth connexion. You can find it -->here<--

      When i use the methods like bluetoothSerial.connect (of course with my MAC adress) or just when i use examples with Arduino, nothing works.

       

      It's like the methods of plugin stop the progress of the code but no error appears in console.

       

      I install plugin with

      cordova plugin add cordova-plugin-bluetooth-serial

       

      Perhaps i forget to do one thing and so it doesn't works...

       

      Somebody can help me with this plugin ?

      Or someone who has already use Bluetooth in a phonegap/cordova project?

       

      Thank you, and really sorry for my english.

        • 1. Re: Phonegap and Bluetooth plugin
          kerrishotts Adobe Community Professional

          Hi -- we need to know a bit more information, I think.

           

          What device(s) and platform(s) are you testing on? What OS version(s)?

           

          Which console are you referring to? The JavaScript or device console? If you haven't already, please take a look at the device console as well to see if any errors are being logged there.

           

          Try putting alerts or console.log statements around the code you suspect is failing and see if the code after your plugin request is executing.

           

          It might also help to see your config.xml file and index.html file.

          • 2. Re: Phonegap and Bluetooth plugin
            Gurgue Level 1

            I try my code on a Nexus 5 with Android 6.0.1

             

            I speak about the Javascript console. I don't know how to access device console (I am beginner with Cordova and Android application). I want to try with Android Studio but downloading doesn't work.

             

            I have new errors in Js console. Cordova.js is not found. So I change the path in index.html and it find the script but after that, my alerts who works before don't work after

            So, this is my index.html (how can we put code in this forum...i don't find this too...^^") :

             

            <!DOCTYPE html>

            <html>

                <head>

                    <meta charset="utf-8" />

                    <meta name="format-detection" content="telephone=no" />

                    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

                    <script type="text/javascript" src="../platforms/android/assets/www/cordova.js"></script>

                    <link rel="stylesheet" type="text/css" href="css/index.css" />

                    <title>Bluetooth Count</title>

                </head>

                <body>

                    <h1>Bluetooth Counter</h1>

                    <div id="counter">...</div>

                    <div id="statusDiv"></div>

                   

                    <script type="text/javascript" src="js/index.js"></script>

                    <script type="text/javascript">

                        app.initialize();

                    </script>

                </body>

            </html>

             

            and my config.xml

                  

            <?xml version='1.0' encoding='utf-8'?>

            <widget id="com.example.compte" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">

                <name>Compter</name>

                <description>

                    A sample Apache Cordova application that responds to the deviceready event.

                </description>

                <author email="dev@cordova.apache.org" href="http://cordova.io">

                    Apache Cordova Team

                </author>

                <content src="index.html" />

                <plugin name="cordova-plugin-whitelist" spec="1" />

                <plugin name="cordova-plugin-bluetooth-serial" spec="1" />

                <access origin="*" />

                <allow-intent href="http://*/*" />

                <allow-intent href="https://*/*" />

                <allow-intent href="tel:*" />

                <allow-intent href="sms:*" />

                <allow-intent href="mailto:*" />

                <allow-intent href="geo:*" />

                <platform name="android">

                    <allow-intent href="market:*" />

                </platform>

                <platform name="ios">

                    <allow-intent href="itms:*" />

                    <allow-intent href="itms-apps:*" />

                </platform>

            </widget>

             

            and in case, my index.js

             

            var app = {

                initialize: function() {

                    this.bindEvents();

                },

             

                bindEvents: function() {

                    document.addEventListener('deviceready', this.onDeviceReady, false);

                },

             

                onDeviceReady: function() {

                    alert("patatère"); //works if i don't change Cordova.js path

                    bluetoothSerial.connect("98:D3:31:FC:11:A4", connectSuccess, connectFailure);

                    alert("ça passe");// doesn't work in any case

                },

                connectSuccess: function(){

                    alert("Success!");//so, doesn't work

                },

             

                connectFailure : function(){

                    alert("Failed");// and doesn't work

                }

            };

             

            app.initialize();

                                   

            Thanks for your reply and sorry to show my code like this...

            • 3. Re: Phonegap and Bluetooth plugin
              kerrishotts Adobe Community Professional

              Well, this is part of the problem:

               

              <script type="text/javascript" src="../platforms/android/assets/www/cordova.js"></script>
              

               

              You should only use the following version:

               

               <script type="text/javascript" src="cordova.js"></script>
              

               

              Also, the current version of the plugin in question is 0.4.5, not 1, so I'd revise your plugin tag accordingly:

               

              <plugin name="cordova-plugin-bluetooth-serial" spec="0.4.5" />
              

               

              Furthermore, the whitelisting plugin has more recent versions as well, so you might want to update that tag:

               

              <plugin name="cordova-plugin-whitelist" spec="1.2.2" />
              

               

              Finally, how are you testing your app? Are you using "cordova run android" or are you using the PhoneGap Desktop/Developer app?

               

              Note: You can embed code by switching the editor to the advanced view, and then clicking the ">>" button in the toolbar, then selecting "Syntax Highlighting", and selecting the language of your choice. It's a bit buried, unfortunately.

              • 4. Re: Phonegap and Bluetooth plugin
                Gurgue Level 1

                I don't have the ">>" button in my toolbar...

                 

                ok, i change the versions in config.xml and cordova'js path. Nothing changes after that...

                 

                I am testing my app with both, "run android" and PhoneGap app.

                • 5. Re: Phonegap and Bluetooth plugin
                  kerrishotts Adobe Community Professional

                  You get the >> icon if you click "use advanced editor".

                   

                  Please post your changed config.xml and index.html files so we an get an idea of the changes made.

                   

                  Also, the PhoneGap developer app does not support all plugins -- it just does core plus a few extras. So don't rely on solely upon that when determining if things work or not. Building with "phonegap run android", though, your plugins should be available. It is possible, however, that the plugin itself is bugged. You could try going back a couple point releases to see if that changes anything.

                  • 6. Re: Phonegap and Bluetooth plugin
                    Gurgue Level 1

                    ok, now my code works perfectly !

                     

                    Thanks for "spec" attribut !

                     

                    and if someone has the same problem than me, maybe your Tx and Rx pins are inversed....like me. ^^