Get mac address and ip in Nativescript - nativescript

We are implementing a server for app distribution and we need restrict the access to the apps by:
mac address
ip
At the moment I have not found any module that can obtain this data from the device in nativescript, so i don't know if there's a plugin or how else can I achieve this.

In nativescript you can access native apis of device
so if there isn't any module/plugin for it you can use this option for accessing native apis.
https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript
for example there is solution for mac adress here
in JAVA:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
we can write it in javascript like this:
first we should fine where is this getSystemService:
after searching in documentation of android we found:
getSystemService is in android.content.Context
for accessing context in nativescript http://docs.nativescript.org/cookbook/application
we can do:
import app = require("application");
app.android.context;
so let's write it in javascript:
we don't have types in javascript so we use var instead;
var context = android.content.Context;
var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
var wInfo = wifiManager.getConnectionInfo();
var mac = wInfo.getMacAddress();
NOTE1 : as mentioned in above java solution link you should add this permision <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> to app/App_Resources/AndroidManifest
NOTE2 : the above solution was for android,for ios you should find the solution with objective_c and convert to javascript with help of nativescript documentation.
NOTE3:In android 6 you might need request permision
You can also use this method to create a plugin for nativescript.

Related

Logging via TraceSource in Xamarin (UWP)

I just want to log to console and to a log file, using a standard TraceSource, in my Xamarin app that will run on UWP, Mac OS X, iOS and Android. I'm developing/debugging on UWP.
TraceSource, TraceListener, and TextWriterTraceListener are indeed all available in .Net Standard library, so perhaps I'm setting it up incorrectly? Most places on the Internet insist on setting up trace listeners in an app.config file, but this is not applicable nor possible for Xamarin apps. So here is my logging initialization code, mostly based on an example in Microsoft docs:
private void SetupLogging()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application.log");
if (!File.Exists(logFilePath)) File.Create(logFilePath);
var logFileTraceListener = new TextWriterTraceListener(logFilePath, "logFileTraceListener");
Trace.Listeners.Add(logFileTraceListener);
Trace.Write("Test");
Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
Trace.Flush();
}
When I run this in a Xamarin UWP app, a file is created but nothing is written to it, nor can I find anything in the Output of the program (there is no ConsoleTraceListener so I'm trying to write a TextWriterTraceListener to Console.Out). Can someone provide a working example for Xamarin? (I haven't tried the Android or iOS apps yet; want to get UWP on the local machine working first.)
The problem is that you passed wrong string parameter to TextWriterTraceListener method. Please try to pass Stream parameter. You could use following code directly. by the way, you'd better use LocalApplicationData SpecialFolder that could be accessed successfully within uwp.
private void SetupLogging()
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Application.log");
if (!File.Exists(logFilePath))
{
File.Create(logFilePath);
}
var logFileTraceListener = new TextWriterTraceListener(File.Open(logFilePath,FileMode.Open), "logFileTraceListener");
Trace.Listeners.Add(logFileTraceListener);
Trace.Write("Test");
Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
Trace.Flush();
}

How to access mobile data usage from settings in android mobile using xamarin forms

In my application i want to access contact names and mobile data usage from Android mobile. I get the contacts list from the mobile by using the below code. But I have no idea how to access mobile data usage from the settings in android mobile. I have referred many sites but none of them could give me a clear answer. How do I access the mobile data usage when mobile data is turned ON?
var context = MainActivity.GetAppContext();
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id,
ContactsContract.Contacts.InterfaceConsts.DisplayName,
ContactsContract.Contacts.InterfaceConsts.HasPhoneNumber
};
var cursor = context.ContentResolver.Query(uri, projection, null, null, null);
ObservableCollection<UserContact> UserContactList = new
ObservableCollection<UserContact>();
cursor.MoveToFirst();
You can query only basic info using the Android.Net.TrafficStats class:
var receivedTotal = Android.Net.TrafficStats.MobileRxBytes;
var transmittedTotal = Android.Net.TrafficStats.MobileTxBytes;
//app-specific
var uid = Android.OS.Process.MyUid();
var receivedByApp = Android.Net.TrafficStats.GetUidRxBytes(uid);
var transmittedByApp = Android.Net.TrafficStats.GetUidTxBytes(uid);
Please note that the app-specific methods return overall number of bytes since the network interface or app started and may be zeroed-out when connection drops or app closes. Also they count not only mobile data, but include wi-fi connection as well.
There is no built-in API to query mobile data usage of other apps in the default API. Some manufacturers might offer their own implementations for this, but it is not available in the "clean Android OS".

Open Native iOS app from Xamarin.Forms

I have an native iOS sample app installed in my iPad and also my sample xamarin.Forms App installed. I want to open native iOS app from Xamarin.forms when clicked on button. I have set URL schema in my native iOS app in info.plist.
How do I open native app from xamarin.forms on click of a button. I already tried
var request = Device.OnPlatform(
string.Format("native app url"),
string.Format("native app url"),
string.Format("native app url"));
Device.OpenUri(new Uri(request));
but its not working.
It's basically how you are doing it.
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Device.OpenUri(new Uri(request));
Be aware that each platform have some requirements in the way to url is configured and parameters are sent.
Let say to get the maps app open in each platform you would use these urls schemes:
// iOS doesn't like %s or spaces in their URLs, so manually replace spaces with +s
var iOSUrl = string.Format("http://maps.apple.com/maps?q={0}&sll={1}", name.Replace(' ', '+'), loc);
var androidUrl = string.Format("geo:0,0?q={0}({1})", string.IsNullOrWhiteSpace(addr) ? loc : addr, name);
var winPhoneUrl = $"bingmaps:?cp={loc}&q={name}";
var url = Device.OnPlatform(iOSUrl, androidUrl, winPhoneUrl);
Also you cannot open any arbitrary app unless you know its Url scheme.
Anyway, if the Device.OpenUri() doesn't fulfill your requirements you are good to create your own implementations using the native APIs.
Please go through the below link and attached photo
Link:-launch-an-app-from-within-another-iphone
I am using below code in xamarin forms
NSUrl request = new NSUrl("Camera://");
var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(request.ToString()));
if (!canOpen)
{ Task.FromResult(false); }
else
{
Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(request.ToString())));
}

How can i use internal database from scratch

How can i using internal database for example (sqlite) for offline app in nativescript without using any plugin.
i'm searched every were how i can installed or used sqlite or other internal database for nativescript but i didn't have any answer.
Just like you would do with any code that you need to access the native APIs
e.g. (JavaScript) Android example
var query = "select sqlite_version() AS sqlite_version";
var db = android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(":memory:", null);
var cursor = db.rawQuery(query, null);
var sqliteVersion = "";
if (cursor.moveToNext()) {
sqliteVersion = cursor.getString(0);
console.log(sqliteVersion);
}
The API references for SQLite in Android here and that said you can now follow a basic Android database tutorial and implement it step by step in your NativeScript application using JavaScript or TypeScript
Still, the plugin could provide all that wrapped in a ready-to-go functionality so unless you are lacking something it will be easier to use the nativescript-sqlite and avoid writing native code for Android and then for iOS.

Using Sinch with Xamarin

I'm targetting iOS and Android platforms for a messaging application.
I want to use voice and messaging features of the sdk.
Are there any bindings available for sinch sdk for xamarin?
Sinch has a C# wrapper for sending SMS via their REST interface. It is available via a Nuget (Sinch.SMS) and/or you can grab the code on Github; https://github.com/sinch/Sinch.SMS
i.e. (from their hello world example):
To send an SMS use:
var client = new Sinch.SMS.Client("yourkey", "yoursecret");
var messageid = await client.SendSMS("+46722223355", "hello from sinch");
To check a status of an SMS use:
var client = new Sinch.SMS.Client("yourkey", "yoursecret");
var result = await client.CheckStatus(messageid);
As far as a complete SDK bindings for their Android/iOS SDKs, I do not know of one personally.
I 'just' would import them into XStudio and convert their sample call apps and give it a try. Should not take very long to see if the auto-wrappers work. Otherwise you would have manually correct/write the C# bindings to their 'native' lib
Here are some Xamarin binding libraries available on nuget. They are by a Vietnamese Software Development company called NAXAM. I believe these are all open source, and published on their github here: https://github.com/NAXAM
https://www.nuget.org/packages/Naxam.SinchVoice.Droid/
https://www.nuget.org/packages/Naxam.SinchVoice.iOS/3.11.0-pre1
https://www.nuget.org/packages/Naxam.SinchVerification.Droid/
https://www.nuget.org/packages/Naxam.SinchVerification.iOS/2.0.4-pre1

Resources