0

PhoneGap Torch Plugin

I am trying to get the torch plugin for phonegap/android to work. I used this code Github Torch Plugin I don't get any errors and I dont know where a possible mistake could be. I am using cordova-2.0.0 library.

This is my index.html

<script type="text/javascript" src="Torch.js">
</script> <script type="text/javascript" src="TorchFunctions.js"> </script> </body> </html>

This is TorchFunctions.js

    window.plugins.Torch.toggle( 
    function() { console.log( "toggle" ) }                          
,   function() { console.log( "error" ) }                           
);

Iam debugging this on a Samsung Galaxy SIII. I read that there might me problems with Samsung devices - is that true? If so can anybody suggest anything? Maybe the plugin isn't up-to-date. Did anybody work with the plugin recently?

whostolemyhat
  • 3,107
  • 4
  • 34
  • 50
AppRoyale
  • 403
  • 1
  • 5
  • 21

3 Answers3

0

Have you tried:

window.plugins.Torch.turnOn( 
    function() { console.log( "turnOn" ) }                          // success
,   function() { console.log( "error" ) }                           // error
);

Otherwise i can tell you that I am having problems with my S3 and other samsung phones too. In several of my projects I am utilizing the front led which i didnt get to work a 100% either. It might be better if you test it first on a non samsung device if you have the possibility and only after that adapt the code if necessary.

Also make sure you follow all the other steps on https://github.com/phonegap/phonegap-plugins/blob/master/Android/Torch/README.md

Make sure also you have copied the js file before to the root of your phonegap application or give the correct path in the html

  <script src="/folderWithJscript/Torch.js"></script>

The declaration has to be in the head of your html file like:

 <html>
 <head>

 <script src="Torch.js"></script>

 </head>
 <body></body></html>

To call the function from a button/link it out of your html it would be something like:

<a href="#torch" onclick="window.plugins.Torch.turnOn()">Torch on</a>

That is just a guess though. Please check the documentation for the rest.

Keeping your Torch function separate inside your TorchFunctions.js is fine too. You just would have to include that script in the html header again!

As well you have to create inside your android project a folder called res if not there yet, then another called xml with a file called plugins.xml Add a plugin line to res/xml/plugins.xml

<plugin name="Torch" value="nl.debree.phonegap.plugin.torch.TorchPlugin" />
SunnySonic
  • 1,318
  • 11
  • 37
  • @Andre: lets say your html file is called index.html. The Torch.js file has to be in the same folder if you declare Also you have to add that declaration to the head of your webpage. See my edit above. – SunnySonic Sep 24 '12 at 10:46
  • how are you testing your phone application? are you using eclipse and wrapped everything up into an android project? If you are testing it just on the browser itself on your pc or phone it wont work. – SunnySonic Sep 24 '12 at 12:27
  • Iam using Eclipse with Android SDK. Unfortunately I only got my Galaxy S3. I think an AVD wont simulate a flashlight as well. Of course it wont work on my browser. ;) Furthermore Iam guessing if I dont need a reference to Torch in Torchfunctions.js? Because when I do this: Torch on How would TorchFunctions, which is window.plugins.Torch.turnOn( function() { console.log( "turnOn" ) } // success , function() { console.log( "error" ) }); know what to do? – AppRoyale Sep 24 '12 at 12:39
  • you can do only one at a time. Either you are calling the function directly or through an javascript file, which would be your TorchFunctions.js. – SunnySonic Sep 24 '12 at 13:10
  • As for the plugins.xml. If you are using eclipse you are seeing your project in the package explorer. underneath your project you will find different folders. For example src, bin, res. Inside the res folder you will have to create another folder called "xml". Inside the "xml" folder you will have to create a file called "plugins.xml". Inside that file you will have to add: " " Only after that file exists you can go on with your testing. without it it won't work because the app doesnt recognize your phonegap plugin. – SunnySonic Sep 24 '12 at 13:10
  • Yeha I got that file. My question was if I need to work with a reference to this xml/plugin or if its okay if it is just in the folder? The mistake must be somewhere else? If you have any device differend than a samsung I would appreciate you installing this plugin in a simple button or something. Takes like 2 minutes I guess. It would be very awesome. – AppRoyale Sep 24 '12 at 13:47
  • Well, it takes much longer, but let me try it for you. – SunnySonic Sep 24 '12 at 14:17
  • The thing you are missing is that the TorchPlugin.java has to be copied to a package under /src/nl/debree/phonegap/plugin/torch/ like it says in the readme on their webpage. Will try it for you and get back to you. – SunnySonic Sep 24 '12 at 14:23
  • I am sorry to tell you but i couldnt get it work on my s3 either. Otherwise i only have a htc flyer without flash around that i cant use for testing this kind unfortunately. In this link it states, that it doesnt always work. It might also be the the plugin is too old and only works with an older version of phonegap. https://github.com/phonegap/phonegap-plugins/issues/720 You should resort to "standard" android programming to get the functionality you would like. – SunnySonic Sep 24 '12 at 15:43
  • you could try go through the readme again and you would also have to add permissions to the manifest. – SunnySonic Sep 24 '12 at 15:43
  • check this link: https://github.com/phonegap/phonegap-plugins/pull/687 maybe you should try the whole thing with phonegap/cordova 1.9 instead of the newest. – SunnySonic Sep 24 '12 at 15:47
  • The problem ist that I want to test html5/javascript apps. Is it possible to to do so and still implement some native code? If so can you give a small explanation? Thanks. I love you. – AppRoyale Sep 25 '12 at 07:19
  • hehe. yes. You can implement native code. Either through plugins like this one or the way i do in one of my phonegap apps is with an android options menu that pops up when clicking/touching the options button on your phone. Here a guide: http://mobile.tutsplus.com/tutorials/android/android-sdk-implement-an-options-menu/ – SunnySonic Sep 25 '12 at 07:27
  • Thanks for your effort. I made already a few native android apps. Iam rather interested in how you can use the html/css look and combine it with android activities. Did you do this already? – AppRoyale Sep 25 '12 at 07:31
  • yes. that is what im saying. phonegap always runs inside a webview. besides that webview you can put anything you like. like an options menu or an actionbar or even other things. (check my other answer above for the actionbar) – SunnySonic Sep 25 '12 at 07:35
  • if you only want to use phonegap and call native code from within you will have to go the way of creating some sort of plugin. Here a very easy example: http://catchmayuri.blogspot.in/2011/05/getting-access-of-android-java-code-to.html – SunnySonic Sep 25 '12 at 07:46
  • My pleasure. I struggled with that too in the beginning and its nice to have options of mixing native with phonegap apps. Would be nice if you could upvote my answer. thanks a lot! – SunnySonic Sep 25 '12 at 08:29
  • I dont have enough reputation. But as soon as I get there - I will. – AppRoyale Sep 25 '12 at 08:33
0

Another option to use native code in an android phonegap app is to use an actionbar with your webview for phonegap underneath or above.

Here info about the native android action bar which only works from 3.0 up:

http://developer.android.com/guide/topics/ui/actionbar.html

or a third party action bar that works on almost all versions / adaption of native action bar:

You might want to checkout the ActionBarSherlock http://actionbarsherlock.com/. The source code is available here https://github.com/JakeWharton/ActionBarSherlock. It will give you an idea of how it is used along with fragments. It will give you some idea on how to proceed.

There is an android compatibility library which google created to use some of the new API's they created for honeycomb on lower version such as 1.5 and above. It can be found in the android sdk folder like E:\Program Files\Android\android-sdk-windows\android-compatibility\v4\android-support-v4.jar. If you don not have this jar you will need to download it using the avd manager.

SunnySonic
  • 1,318
  • 11
  • 37
0

I had this problem. In adb's log output, I was seeing:

D/TorchPlugin(30890): Plugin created
W/System.err(30890):    at nl.debree.phonegap.plugin.torch.TorchPlugin.<init>(TorchPlugin.java:41)
I/System.out(30890): Error adding plugin nl.debree.phonegap.plugin.torch.TorchPlugin.

The solution was to add the following permissions to my Android manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />

I hope this helps someone!

  • Gavin
gavD_UK
  • 395
  • 5
  • 11