I have following piece of code created in Android Studio:
public void onClick(View v) {
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
final GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation();
if (location != null) {
Toast.makeText(MainActivity.this,"LAC: " + location.getLac() + " CID: " + location.getCid(),Toast.LENGTH_LONG).show();
}
}
}
If NativeScript have 100% access do Android API how reach same result in .ts NativeScript?
Please refer the docs on Java to JS to understand how the native api calls are marshalled into JS / TS.
You will be able to get the current activity from application module with application.android.foregroundActivity. So the code below should return instance of telephony manager.
const telephony = application.android.foregroundActivity.getSystemService(android.content.Context.TELEPHONY_SERVICE)
Use nativescript-permissions plugin to acquire permissions or to know whether your app already has the required permissions on device.
There is also nativescript-toast plugin that implements cross platform toast messages.
Thanks Manoj for useful help.
So steps that needed to be taken are:
Install:
npm install tns-platform-declarations --save-dev
Add two lines into references.d.ts, in my case it was in file ProjectName\references.d.ts
/// <reference path="node_modules/tns-platform-declarations/android.d.ts" />
/// <reference path="node_modules/tns-platform-declarations/ios.d.ts" />
Install nativescript-permissions plugin: tns plugin add nativescript-permissions
Install nativescript-toast plugin $ tns plugin add nativescript-toast
Import libraries on the begining of file
var application = require("application");
const permissions = require( "nativescript-permissions" );
import * as Toast from 'nativescript-toast';
And write code to get LAC, CID, MCC, MNC
buttonGetInfoTap(args) {
const telephonyService = application.android.foregroundActivity.getSystemService(android.content.Context.TELEPHONY_SERVICE);
permissions.requestPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION, "I need these permissions because I'm cool")
.then( () => {
if (telephonyService.getPhoneType() == android.telephony.TelephonyManager.PHONE_TYPE_GSM) {
let location = <android.telephony.gsm.GsmCellLocation>telephonyService.getCellLocation();
if (location != null) {
console.log("LAC: " + location.getLac() + " CID: " + location.getCid())
Toast.makeText("LAC: " + location.getLac() + " CID: " + location.getCid(), "long").show();
const networkOperator = telephonyService.getNetworkOperator();
const MCC =networkOperator.substring(0,3)
const MNC =networkOperator.substring(3)
Toast.makeText("MCC:"+MCC+"MNC:"+MNC, "long").show();
}
}
})
.catch( () => {
Toast.makeText("No permission", "long").show();
});
}
Related
I am using the CrossGeolocator.Current.StartListeningAsync method in my xamarin app to be able to listen to location updates in background for iOS.
I am using essentials to request permissions.
On first run we get location permission (in app only) and location using essentials and then we use StartListeningAsync for ios to be able to track location if the app is in background or foreground.
When the callback is hit we get a popup saying this app uses background location and gives you the option to use it or change back to use in app only option. On selection of any option the callback never completes and subsequent code isnt run.
Here is the popup I get after I have permission for when in use and then start listening:
Popup on ios
On subsequent runs once permissions are set manually the callback works.
Xamarin Forms Version: 5.0.0.1931
Xamarin Essential Version: 1.6.1
Geolocator Plugin Version: 4.6.2-beta
Code example:
private async Task StartListening()
{
if (CrossGeolocator.Current.IsListening)
return;
try
{
var settings = new ListenerSettings
{
ActivityType = ActivityType.Other,
DeferLocationUpdates = true,
DeferralDistanceMeters = 15,
DeferralTime = TimeSpan.FromSeconds(10),
ListenForSignificantChanges = false,
PauseLocationUpdatesAutomatically = false,
AllowBackgroundUpdates = true,
};
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(15), 5, true, settings);
CrossGeolocator.Current.PositionChanged += PositionChanged;
CrossGeolocator.Current.PositionError += PositionError;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
You need to make sure the permission is ok before starting to listen to location updates. Have a try with following code:
public async Task GetLocationAsync()
{
var status = await CheckAndRequestPermissionAsync(new Permissions.LocationAlways());
if (status != PermissionStatus.Granted)
{
// Notify user permission was denied
return;
}else{
await StartListening();
}
}
public async Task<PermissionStatus> CheckAndRequestPermissionAsync<T>(T permission)
where T : BasePermission
{
var status = await permission.CheckStatusAsync();
if (status != PermissionStatus.Granted)
{
status = await permission.RequestAsync();
}
return status;
}
Trying to use Alexa presentation Language features in AWS hosted custom lambda function. Intent handler are firing but when I add the
Alexa.getSupportedInterfaces it is failing .
Message is "Error handled: Alexa.getSupportedInterfaces is not a function"
// 1. Intent Handlers =============================================
const LaunchRequest_Handler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest';
},
handle(handlerInput) {
let responseBuilder = handlerInput.responseBuilder;
let speakOutput = 'Welcome to test Bot. ';
// let skillTitle = capitalize(invocationName);
// Add APL directive to response
if (Alexa1.getSupportedInterfaces(handlerInput.requestEnvelope)['Alexa.Presentation.APL']) {
// Add the RenderDocument directive to the responseBuilder
responseBuilder.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
token: Echo_Token,
document: Customer
});
// Tailor the speech for a device with a screen.
speakOutput += " You should now also see my greeting on the screen."
} else {
// User's device does not support APL, so tailor the speech to this situation
speakOutput += " This example would be more interesting on a device with a screen, such as an Echo Show or Fire TV.";
}
return responseBuilder
.speak(speakOutput)
.withShouldEndSession(false)
.reprompt('try again, ' + speakOutput)
.withSimpleCard("CustomerSupport!", "CustomerSupport)")
// .reprompt('add a reprompt if you want to keep the session open for the user to respond')
//.withStandardCard('Welcome!',
// 'Hello!\nThis is a card for your skill, ' + skillTitle,
// welcomeCardImg.smallImageUrl, welcomeCardImg.largeImageUrl)
.getResponse();
},
};
Instead of using the below condition:
Alexa1.getSupportedInterfaces(handlerInput.requestEnvelope['Alexa.Presentation.APL]
you can use, below condition to check if the device supports APL:
if (supportsAPL(handlerInput))
Make sure you include below functions definition in your index file:
function supportsAPL(handlerInput) {
const supportedInterfaces = handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
const aplInterface = supportedInterfaces['Alexa.Presentation.APL'];
return aplInterface != null && aplInterface != undefined;
}
function supportsAPLT(handlerInput) {
const supportedInterfaces = handlerInput.requestEnvelope.context.System.device.supportedInterfaces;
const aplInterface = supportedInterfaces['Alexa.Presentation.APLT'];
return aplInterface != null && aplInterface != undefined;
}
Hope that helps as it worked for me.
Saved image not showing on galary iOS(working fine on Android but not iOS)
Here is my code for saving image to device
public void SavePicture(string name, byte[] data, string location = "Downloads")
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, location);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
File.WriteAllBytes(filePath, data); // writes to local storage
}
Also allow for photo permission on info.plist
Privacy - Photo Library Usage Description
after execute this code I cant find the saved photo on iOS device
How to solve this?
Well in iOS to show an image into the gallery you have to write additional code as just using File.WriteAllBytes won't call the native UIImageWriteToSavedPhotosAlbum API to show it in the album.
To do that you will have to call this API as follows:
Make a dependency service:
public interface ISavePhotosToAlbum
{
void SavePhotosWithFilePath(String Path);
}
Now Add a native class and do the following:
[assembly: Dependency(typeof(SaveToAlbum))]
namespace SavePhotosDemo.iOS
{
public class SaveToAlbum : ISavePhotosToAlbum
{
public void SavePhotosWithFilePath(String Path)
{
using (var url = new NSUrl (Path))
using (var data = NSData.FromUrl (url))
var image = UIImage.LoadFromData (data);
image.SaveToPhotosAlbum((img, error) =>
{
if (error == null)
{//Success }
else
{//Failure}
});
}
}
}
Then use this Dependency Service as follows:
DependencyService.Get<ISavePhotosToAlbum>().SavePhotosWithStream(imageUrl);
Also, see to it that you add this dependency service call only for ios by adding it in an OnPlatform if statement.
UPDATE
Use the following code to Add a Custom Photo Album in Photos library:
void AddAssetToAlbum(UIImage image, PHAssetCollection album, string imageName)
{
try
{
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
{
// Create asset request
var creationRequest = PHAssetCreationRequest.CreationRequestForAsset();
var options = new PHAssetResourceCreationOptions
{
OriginalFilename = imageName
};
creationRequest.AddResource(PHAssetResourceType.Photo, image.AsJPEG(), options);
// Change asset request (change album by adding photo to it)
var addAssetRequest = PHAssetCollectionChangeRequest.ChangeRequest(album);
addAssetRequest.AddAssets(new PHObject[] { creationRequest.PlaceholderForCreatedAsset });
}, (success, error) =>
{
if (!success)
Console.WriteLine("Error adding asset to album");
});
}
catch (Exception ex)
{
string h = ex.Message;
}
}
See to it that you request authorization using the PHPhotoLibrary.RequestAuthorization method as we need to request access to be able to use the Photos library. Also, As of iOS 10 and above we also need to add an entry for access in the target .plist file for "Privacy - Photo Library Usage Description":
<key>NSPhotoLibraryUsageDescription</key>
<string>Access to photos is needed to provide app features</string>
Update
At the end, adding the following started showing the documents in the files app:
<key>UIFileSharingEnabled</key> <true/> <key>LSSupportsOpeningDocumentsInPlace</key> <true/>
Is there a way to get the name of a mobile device (e.g. "John's iPhone" ) using javascript?
Maybe I wasn't very clear... what I meant is not whether it's an iPhone, iPad etc. but the "device name" - for example it can be "John's iPhone".
You can't do this through javascript for a web app running in a native browser - javascript generally doesn't have access to this personal identifying data.
One possible way is to use a framework like PhoneGap which may have an API to access the device name. But then, you can only deploy your web site via an app store, so this could be very limiting based on your use case.
Your best bet is to use the user agent:
e.g.
const ua = navigator.userAgent
const device = {
iPad: /iPad/.test(ua),
iPhone: /iPhone/.test(ua),
Android4: /Android 4/.test(ua)
}
The object will allow you to write nice conditional logic such as if(device.iPad) { /* do stuff */ }
I'm working with mobile device with embedded scanner. To be able to use some the JavaScript library of different devices and to avoid conflict with those libraries of different manufacturer (Zebra, Honeywell, Datalogic, iOs ect...) I need to come up with a way to identify each devices so I can load the proper library and this is what I came up with. Enjoy
getDeviceName: function () {
var deviceName = '';
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
Datalogic: function() {
return navigator.userAgent.match(/DL-AXIS/i);
},
Bluebird: function() {
return navigator.userAgent.match(/EF500/i);
},
Honeywell: function() {
return navigator.userAgent.match(/CT50/i);
},
Zebra: function() {
return navigator.userAgent.match(/TC70|TC55/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Datalogic() || isMobile.Bluebird() || isMobile.Honeywell() || isMobile.Zebra() || isMobile.BlackBerry() || isMobile.Android() || isMobile.iOS() || isMobile.Windows());
}
};
if (isMobile.Datalogic())
deviceName = 'Datalogic';
else if (isMobile.Bluebird())
deviceName = 'Bluebird';
else if (isMobile.Honeywell())
deviceName = 'Honeywell';
else if (isMobile.Zebra())
deviceName = 'Zebra';
else if (isMobile.BlackBerry())
deviceName = 'BlackBerry';
else if (isMobile.iOS())
deviceName = 'iOS';
else if ((deviceName == '') && (isMobile.Android()))
deviceName = 'Android';
else if ((deviceName == '') && (isMobile.Windows()))
deviceName = 'Windows';
if (deviceName != '') {
consoleLog('Devices information deviceName = ' + deviceName);
consoleLog('Devices information any = ' + isMobile.any());
consoleLog('navigator.userAgent = ' + navigator.userAgent);
}
return deviceName;
},
and this is an example of how it can be used:
initializeHandheldScanners: function () {
if (DeviceCtrl.getDeviceName() == 'Zebra')
DeviceCtrl.initializeSymbolScanner();
if (DeviceCtrl.getDeviceName() == 'Honeywell')
DeviceCtrl.initializeHoneywellScanner();
if (DeviceCtrl.getDeviceName() == 'Datalogic')
DeviceCtrl.initializeDatalogicScanner();
},
You can say thanks to Cory LaViska. I did this based on his work. Here is the link if you want to know more
https://www.abeautifulsite.net/detecting-mobile-devices-with-javascript
You can use this snippet:
const getUA = () => {
let device = "Unknown";
const ua = {
"Generic Linux": /Linux/i,
"Android": /Android/i,
"BlackBerry": /BlackBerry/i,
"Bluebird": /EF500/i,
"Chrome OS": /CrOS/i,
"Datalogic": /DL-AXIS/i,
"Honeywell": /CT50/i,
"iPad": /iPad/i,
"iPhone": /iPhone/i,
"iPod": /iPod/i,
"macOS": /Macintosh/i,
"Windows": /IEMobile|Windows/i,
"Zebra": /TC70|TC55/i,
}
Object.keys(ua).map(v => navigator.userAgent.match(ua[v]) && (device = v));
return device;
}
console.log(getUA());
With the help of this link 'https://gist.github.com/nathanpc/2464060' I have written code to download a file in PhoneGap. I tried it on Android phone and it works perfectly.
But on Windows phone, It says that it has downloaded and the path is -- //xyz/SIFA.pdf.
To look at the file system on Windows phone I have downloaded an app called 'Files' using which I looked up on the phone but I could not find any folder xyz nor the file.
I also connected the Windows phone to my PC and had a look but could not find the folder nor the pdf
Could you suggest what I am doing wrong in the code or if I am not looking for the file in the right way.
Thanks for your help in advance
Here is my code to download file in PhoneGap.
I have added alerts to indicate the progress.
var folderName = 'xyz';
var fileName;
var downloadFileOne = function (URL) {
//step to request a file system
alert("step one");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
alert("step two");
function fileSystemSuccess(fileSystem) {
alert("step three");
var download_link = encodeURI(URL);
alert("step four - " + download_link);
fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL
var directoryEntry = fileSystem.root; // to get root path of directory
directoryEntry.getDirectory(folderName, {
create: true,
exclusive: false
}, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
alert("step five");
var rootdir = fileSystem.root;
var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory
fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give
// download function call
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
// Directory created successfuly
alert("directory created successfully");
}
function onDirectoryFail(error) {
//Error while creating directory
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
//Unable to access file system
alert(evt.target.error.code);
}
}
function filetransfer(download_link, fp) {
alert("step six - file transfer method");
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function (entry) {
alert("download complete: " + entry.fullPath);
},
function (error) {
//Download abort errors or download failed errors
alert("download error source " + error.source);
}
);
}