I'm trying to add native android code to an already existing Flutter app, that does something quite different.
So I'd just like to have all of that functionality on top of my current app.
The native android app is using Bluetooth to connect to an external device and is collecting data periodically.
I know, that there is the possibility to call native code via platform channels, but it seemed to me, that you'd have to call that code every time you want to use it? Is this a reasonable way to implement the app or should it be rewritten in Dart? What's the best practice here?
Hope you can help me, thanks a lot!
The purpose of platform channels is to call platform-specific APIs.
...you'd have to call that code every time you want to use it...
Correct.
...Is this a reasonable way to implement the app or should it be rewritten in dart? What's the best practice here...
Check the source code of any package that relies on platform channels (for example camera, file picker, etc.) and read the official documentation. As a short summary, platform-specific code is written in Kotlin/Java (Android), Swift/Objective-C (iOS) or C/C++. Dart is used for building the UI and communicating with said code via platform channels and messages.
Related
I'd like to give a try with flutter, but so far I have found only NFC reader plugin. I will need two other things.
react on NFC tag present intent and then maybe use NFC plugin to read it
write to NFC tag, probably using Platform channels
I just need to confirm it is feasible at all with flutter and will need the kick in the right direction, before I will leave plain android.
perhaps I am a little late to the party, however as I just tackled a very similar problem, I want to weigh in on this topic:
So reading/writing NFC with Flutter is possible. As mentioned before you need a platform channel in order hand the command to the native system, AND an event channel, if you want to read data to your flutter app, to accomplish this task.
The best way would be, if a plugin was available to handle this, however I could not get the one which you mentioned too to work with my flutter app (specifically, because I tried with IOS and swift).
However here are some ressources, from which I puzzled together my system:
Communication from flutter to native system via platform channels: This link is the offical flutter page which interestingly only described the communication from flutter to native system, but not the other way around. For the other way you need:
Communication from native system to flutter app via event channels: (yes you need a different channel for the communication back to the flutter app). This example is only for android. For swift, all I could find was this ressource, which however seems to be a bit old.
NFC Tutorial for IOS: This is actually pretty simple as long as you have a developper account. A good minimalistic tutorial can be found here
NFC Tutorial for Android: This is actually even simpler, as nfc is longer established on android. I like this one
I think what you're looking to do is definitely possible, but as you mentioned in your question you will have to use Platform Channels.
The platform channels can go both ways; you should be able to set it up so that your main activity receives the NFC tag present intent, and then you send a method call from android to dart. Or you could start listening from dart and then have the method return when the intent is received.
Writing to NFC is about the same, you use method channels to call across.
Depending on what you're doing, you may want to consider splitting the NFC functionality into a plugin, even if you don't end up publishing the plugin.
With this fork from flutter-nfc-reader you can now read and write nfc tags from android and read them from ios https://github.com/semakers/flutter-nfc-reader
to install add the following dependency your pubspec.yaml
dependencies:
flutter_nfc_reader:
git:
url: git: //github.com/semakers/flutter-nfc-reader.git
ref: master
In the Readme.md of the repo are the installation and use instructions.
Happy NFC Tag writing!!
I'm developing an app for iOS and android that identifies songs. We have a matching engine in c++ that works well, but my question is about the rest of the app. We have a user interface design that allows the user to record, match and save, as well as share, edit, and see their recordings.
I'm not a coder - just a designer- and I'm getting conflicting advice about how to implement. One person wants to do the majority of the interface in c++ with just a thin native GUI skin, while the other says we should write in native IOS (swift) and Android and only have the engine in c++. The argument for c++ is it will be a single code base to maintain - it would include doing the sharing, storing history, displaying sonograms, etc. The argument against is that in fact it will not be easier to maintain, and will also create a lot of problems making calls, for example, between the interface and the sharing module.
Hope this is clear - it's a very hard thing for me to assess as ignorant as I am! Any advice would be greatly appreciated.
In my opinion, the way to go is to write the GUI the way it was meant to be. Native iOS/Android will yield a much better user experience and will allow you to use the platform latest and greatest way of implementing a UI.
Also, I am assuming that the engine will probably not be developped by the same people that develop the GUI itself, so the argument that it would be simpler to have a single code base doesn't really make sense.
Another solution could be to use Xamarin, so that you can consolidate iOS and Android development, while still having a common C++ engine.
Edit: Typo
My specific task requires that I write a long running service for Android. The service should be running all the time unless the user specifically shuts it down. This I can do. However, one of my managers suggested I look into using Xamarin so we could also run this on Windows 10 and iOS. I have no experience with Xamarin or iOS so I am struggling to find any advantage to using Xamarin for this specific task, other than perhaps the code base is all in C#.
1) Does anyone have any experience with writing this type of service in Xamarin for multiple OS and if so can you offer any insight?
2) Will I have to write separate c# code for the Android, iOS and Windows implementations of the service?
3) If anyone has any compelling reasons to champion Xamarin for this task please let me know your thoughts.
Thanks
Xamarin has ported all the Android / iOS libraries to C#. Hence you can write the code in C# language but the Android Service still works in the same way.
What you will essentially do is create a PCL code (shared library) which can be used across the platforms like iOS / Android. All you business logic goes here in this library. Make sure any platform specific code is not here.
Now you will create separate projects for Android and iOS which will use the above mentioned PCL library. This is platform specific non PCL code.
For example : In Android Project, you will still use
StartService(intent) to start the service but say onHandleIntent(), you will call the PCL(shared library) code.
I would recommend to inject the dependency on PCL code as and when required.
Please Note : If the lines of code in the shared library is considerably very less as compared to platform specific code then it makes no sense to choose Xamarin over native.
So here's the background story:The phonegap application is an online e-store has a plugin (which my team has created) to print receipts after the orders has been completed. All is working good but the printing is taking too long on Phonegap. In fact, it is taking 5-6 seconds per print, which is not good -- the client expects a much faster process of printing, somewhere down to 1-2 seconds.However, this is not possible using Phonegap, so I had to resort to using native applications. I created a native app with the help of a sample code. Eureka! The printing took only 1-2 seconds!On a side note, both Phonegap and the Native App are using the same Printer library -- the code for printing is entirely the same, just that the Native App's performance is better than Phonegap's.Here is where the problem lies:1. The third-party native app should ONLY run in the background (Hence Android Background Services)2. Phonegap should be able to pass the order data to this third-party native app's Android Background Service so that it will be able to print the receipt.So the question is: How do I do this?What I've currently tried is that I used a phonegap plugin (Link). It works, however, it opens the third-party app, which isn't good at all. I can close it immediately after it opens, but what the client wants is that it doesn't open up at all, to avoid confusion with their customers.Any help is greatly appreciated! If there are any clarifications on what needs to be done, please add comments. Thanks!
I fear that it is not possible to create a background service in cordova/phonegap but you can implement you supporting (Native)app with BroadcastReceiver or IntentService which(Runs in backgroun without UI) gets triggered by the (explicit)intent which you call from the cordova plugin.
More on < Service (Deep Guide), IntentServices, BroadcastReceiver > (Native) and invoking intent using cordova webIntent (plugin).
I guess this tutorial may help you.
Check this article explaining js to native code calling (or plugin development).
Create one test app, which will implement all steps explained in above article. Once implemented, you will get to know how js to native code call is executed.
Now, add printer library to your code (native wrapper), then add your code for printing (in one function, again in native code). Call this function from js. That's it.
I had been working on android since a little long. Now I am wondering about titanium. Is it possible to write android (java) code in titanium app since the app is build in titanium? If yes then how? Actually I am having problem dealing with push notification for android in titanium. So I got a solution to build complete app in titanium and then if possible use android code in app to deal with push notification. How? Please do response as quickly as possible. Thanks in advance.
To answer your question directly, you will need to develop a native Android extension to call Java code via JavaScript in a Titanium application.
Rai
You should be checking out the docs Here tells you all you need to know
Hope it helps
Frank
yes it is possible but remember that the titanium tools lag behind the official android ones. Documentation is poor and often wrong, code is laggy and the resulting .apk are much bigger.
Why are you thinking of using Titanium? For cross-compatibility?
If you are thinking about that you have multiple possible ways:
1) Build a minimum common denominator, like titanium, but better: Write a core application for both android and iPhone so that it will be fast and light on memory, with a modified broswer to show your content. This way the interfaces will be much easier and you'll have to write the content just once.
Still you will be able to access low level hardware, like GPS, compass, gyroscope.... easily without consuming too much battery (unlike with titanium) as needed.
2) Find an existing minimum common denominator: Javascript work both on iPhone and Android.
3) There are many other options (Adobe air, python, custom scripting, mobile web site, ...) but I think that the cons outweigh greatly the pros.