DeviceService

Overview

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
Properties
Methods
Constructor
App Types
No properties
Don't instantiate directly instances of the DeviceService.
DeviceService instances are automatically injected by Dynamics Mobile framework.
1
import { View, SqlQueryService } from '$dms';
2
import { Customer } from '$dms-bo';
3
4
@View()
5
export class MyView {
6
7
constructor(
8
//we need to only declare the member
9
private DeviceService: DeviceService
10
){
11
12
}
13
14
async load(){
15
//we can now use the injected instance
16
let location = await this.DeviceService.location.get(0);
17
console.log(location.longitude);
18
}
19
20
}
Copied!
📳
Mobile
🌐
Backend
There is a difference in the behaviour of the DeviceService API in mobile and backend apps.

logOut(): Promise<void>

Description
Parameters
Returns
Example
logOut method performs user logout from the application and navigates to application login screen
void
1
import { DeviceService } from '@dms';
2
3
...
4
this.DeviceService.logOut();
Copied!

sync(): Promise<void>

Description
Parameters
Returns
Example
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
1
import { DeviceService } from '@dms';
2
3
...
4
this.DeviceService.sync();
5
// code after the sync method will not be called
Copied!

hideOnScreenKeyboard(): Promise<void>

Description
Parameters
Returns
Example
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.
void
1
import { DeviceService} from '@dms';
2
import { Customer } from '@dms-bo';
3
4
...
5
await this.DeviceService.hideKeyboard();
Copied!

isNFCSupported(): Promise<boolean>

Description
Parameters
Returns
Example
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
1
import { DeviceService } from '@dms';
2
3
4
...
5
const supported = await this.DeviceService.isNFCSupported();
6
if(supported){
7
console.log(`NFC is supported`);
8
}
9
else {
10
throw new Error(`NFC is not supported`);
11
}
12
Copied!

readNFCTag(): Promise<string>

Description
Parameters
Returns
Example
readNFCTag method activates NFC and tries to read nearby NFC tag and returns the tag reading
string - the content of the NFC tag
1
import { DeviceService } from '@dms';
2
3
4
...
5
const tag= await this.DeviceService.readNFCTag();
6
if(tag){
7
console.log(`the NFC tag is ${tag}`);
8
}
9
else {
10
throw new Error(`NFC reading failed`);
11
}
12
Copied!

barcodeScan(): Promise<{content: string, success: boolean, message: string, format: string }>

Description
Parameters
Returns
Example
barcodeScan method activates the device-camera barcode scanning. It opens the device camera app and allows the user to scan a barcode.
Returns a structure with the following members:
    success: boolean - true if the scanning was successful. If false, the message attribute will contain the reason
    content: string - the barcode which was scanned if success is true
    message: string - error message if success is false
    format: string - format of the barcode
1
import { DeviceService } from '@dms';
2
3
4
...
5
const barcode = await this.DeviceService.scanBarcode();
6
if(barcode.success){
7
console.log(`the scanned barcode was ${barcode.content}`);
8
}
9
else {
10
throw new Error(`Barcode scan failed: ${barcode.message}`);
11
}
12
Copied!

getPlatformType(): Promise<PlatformType>

Description
Parameters
Returns
Example
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.
1
import { DeviceService } from '@dms';
2
3
...
4
const platformType = await this.FileService.getPlatformType()
5
switch(platformType){
6
case PlatformType.Web:
7
console.log('we are running in a web browser');
8
break;
9
case PlatformType.Android:
10
console.log('we are running in a mobile device under Android OS');
11
break;
12
case PlatformType.iOS:
13
console.log('we are running in a mobile device under iOS');
14
break;
15
case PlatformType.Windows:
16
console.log('we are running in a mobile device under Windows 10');
17
break;
18
case PlatformType.Test:
19
console.log('we are running our code under unit test host, e.g. jest, mocha');
20
break;
21
}
22
Copied!

getInfo(): Promise<DeviceInfo>

Description
Parameters
Returns
Example
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
1
import { DeviceService } from '@dms';
2
3
...
4
const info = await this.DeviceService.getInfo();
5
console.log('platform', info.platform);
6
console.log('version', info.version);
7
console.log('isDevice', info.isDevice);
8
9
if('platofrm'== 'Android')
10
console.log('ooops we are running on Android os');
11
12
Copied!

openWebBrowser(): Promise<void>

Description
Parameters
Returns
Example
openWebBrowser method opens a given url in the default web browser on the device.
    url: string - url to be opened in the web browser
void
1
import { DeviceService } from '@dms';
2
3
...
4
this.DeviceService.openWebBrowser('https://www.dynamicsmobile.com');
5
Copied!

takePicture(): Promise<void>

Description
Parameters
Returns
Example
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
1
import { DeviceService } from '@dms';
2
3
...
4
const picture = await this.DeviceService.takePicture();
5
if(!picture)
6
console.log('user did not take a camera snapshot');
7
if(picture && picture.url)
8
console.log(`the snapshot taken by the user was saved here: ${picture.url}`);
Copied!

selectPicture(): Promise<{ url: string }>

Description
Parameters
Returns
Example
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.
1
import { DeviceService } from '@dms';
2
3
...
4
const picture = await this.DeviceService.selectPicture();
5
if(!picture)
6
console.log('user did not select any picture');
7
if(picture && picture.url)
8
console.log(`user selected the following pciture: ${picture.url}`);
Copied!

getBTDevices(): Promise<Array<stirng>>

Description
Parameters
Returns
Example
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
1
import { DeviceService } from '@dms';
2
3
...
4
const btDevices = await this.DeviceService .getBTDevices();
5
if(btDevices.length==0)
6
console.log('There are no paired BT devices, currently!');
7
Copied!

beepOk(): Promise<void>

Description
Parameters
Returns
Example
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.
void
1
import { DeviceService } from '@dms';
2
3
...
4
await this.DeviceService.beepOk();
Copied!

beepDoubleOk(): Promise<void>

Description
Parameters
Returns
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.
void
1
import { DeviceService } from '@dms';
2
3
...
4
await this.DeviceService.beepDoubleOk();
Copied!

beepError(): Promise<void>

Description
Parameters
Returns
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.
void
1
import { DeviceService } from '@dms';
2
3
...
4
await this.DeviceService.beepError();
Copied!

location.get(precision:number): Promise<{latitude: number; longitude: number; speed: number; accuracy: number;}>

Description
Parameters
Returns
Example
location.get method obtains the current device geographic location from the device's location subsystem like GPS, WiFi or Cellular network.
    precision: number - not used as of now, please provide 0 for future compatibility
A flat structure with the following members:
    accuracy: number - not used as of now
    longitude: number - the longitude segment of the current geo location of the device
    latitude: number - the latitude segment of the current geo location of the device
    speed: number - the current speed of the mobile device
1
import { DevieService } from '@dms';
2
3
...
4
const location = await this.DeviceService.location.get(0);
5
if(location && location.latitude!=0){
6
console.log('longitude: ', location.longitude);
7
console.log('latitude: ', location.latitude);
8
}
9
else {
10
throw new Error('Please check if location services are enabled and try again!');
11
}
Copied!
Last modified 1yr ago