SERVICE
This class is a "service" and it is automatically injected if declared as a member in the constructor of the consuming class.
- See the constructor section below for details
- See the Automatic Instance Injection page for details on the topic
The DeviceService class provides methods to work device/platform specific features
Don't instantiate directly instances of the DeviceService.
DeviceService instances are automatically injected by Dynamics Mobile framework.
import { View, SqlQueryService } from'$dms';import { Customer } from'$dms-bo';@View()exportclassMyView {constructor(//we need to only declare the memberprivate DeviceService:DeviceService ){ }asyncload(){ //we can now use the injected instance let location =awaitthis.DeviceService.location.get(0);console.log(location.longitude); }}
There is a difference in the behaviour of the DeviceService API in mobile and backend apps.
logOut method performs user logout from the application and navigates to application login screen
sync method opens the application synchronization screen and performs full synchronization of the app. It can be used only in mobile applications and does not have effect in backend applications.
void
import { DeviceService } from'@dms';...this.DeviceService.sync();// code after the sync method will not be called
hideOnScreenKeyboard(): Promise<void>
hideKeyboard method hides the mobile on-screen keyboard. The method can be used only in mobile apps and does not have effect in backend apps.
isNFCSupported method returns true if the mobile device supports NFC ( near field communication). The method can be used only in mobile apps and does not have effect in backend apps.
true if the mobile device supports NFC
false if the mobile does not support NFC
import { DeviceService } from'@dms';...constsupported=awaitthis.DeviceService.isNFCSupported();if(supported){console.log(`NFC is supported`);}else {thrownewError(`NFC is not supported`);}
readNFCTag(): Promise<string>
readNFCTag method activates NFC and tries to read nearby NFC tag and returns the tag reading
string - the content of the NFC tag
import { DeviceService } from'@dms';...consttag=awaitthis.DeviceService.readNFCTag();if(tag){console.log(`the NFC tag is ${tag}`);}else {thrownewError(`NFC reading failed`);}
getPlatformType method returns the type of the platform where the app is running.
The type of the platform where the app is running. See the example for details.
import { DeviceService } from'@dms';...constplatformType=awaitthis.FileService.getPlatformType()switch(platformType){casePlatformType.Web: console.log('we are running in a web browser');break;casePlatformType.Android: console.log('we are running in a mobile device under Android OS');break;casePlatformType.iOS: console.log('we are running in a mobile device under iOS');break;casePlatformType.Windows: console.log('we are running in a mobile device under Windows 10');break;casePlatformType.Test: console.log('we are running our code under unit test host, e.g. jest, mocha');break;}
getInfo(): Promise<DeviceInfo>
getInfo method returns system information obtained from the device.
The method returns a structure with the following properties:
platform: string - the type of the underlying platform (Android, Windows, iOS, Browser)
version: string - the version if the platform (e.g. operating system version )
isDevice: string - true if the platform is a mobile device , otherwise it means it is a browser
isBrowser: string - true if the app is running in a web browder
isWebview: string - true if the app is running on a mobile device in webview
shellver: string - the version of the native mobile app.
webViewVersion: string - the version of the webview
sdkVersion: string - the version if Dynamics Mobile SDK - e.g. this SDK
sdkDate: string - the date when the SDK version was released
import { DeviceService } from'@dms';...constinfo=awaitthis.DeviceService.getInfo();console.log('platform',info.platform);console.log('version',info.version);console.log('isDevice',info.isDevice);if('platofrm'=='Android')console.log('ooops we are running on Android os');
openWebBrowser(): Promise<void>
openWebBrowser method opens a given url in the default web browser on the device.
takePicture method opens the device camera and allows the user to take a picture. The user can confirm or cancel the picture taking. The method returns the url pointing to a selected picture file.
folder: string - the full name of the file folder- myfolder/subfolder
Returns structure with the following members:
url: string - points to the file location/name where the camera snapshot was saved.
The method will return null if the user cancels the camera snapshot
import { DeviceService } from'@dms';...constpicture=awaitthis.DeviceService.takePicture();if(!picture)console.log('user did not take a camera snapshot');if(picture &&picture.url)console.log(`the snapshot taken by the user was saved here: ${picture.url}`);
selectPicture(): Promise<{ url: string }>
selectPicture method opens the Picture Selection dialog on the device, where the user can select one picture. The method returns the url pointing to a selected picture file.
Returns structure with the following members:
url: string - points to the file location/name selected by the user.
The method will return null if the user cancels the picture selection.
import { DeviceService } from'@dms';...constpicture=awaitthis.DeviceService.selectPicture();if(!picture)console.log('user did not select any picture');if(picture &&picture.url)console.log(`user selected the following pciture: ${picture.url}`);
getBTDevices(): Promise<Array<stirng>>
getBTDevices returns a list of with the names of bluetooth devices, currently paired with the mobile device. Can be used in mobile apps only.
void
import { DeviceService } from'@dms';...constbtDevices=awaitthis.DeviceService .getBTDevices();if(btDevices.length==0)console.log('There are no paired BT devices, currently!');
beepOk(): Promise<void>
beepOk method plays a short ok-alike sound on the mobile device. Can be used in mobile apps only. Can be used after successful operations like successful barcode scan for example.
beepDoubleOk method plays a two times ok-alike sound on the mobile device. Can be used in mobile apps only. Can be used after successful operations like successful barcode scan for example.
beepError method plays a short error alike sound on the mobile device. Can be used in mobile apps only. Can be used after successful operations like successful barcode scan for example.