nativescript-dna-deviceinfo
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -27,2 +27,4 @@ import { Carrier, DisplayMetrics, StorageVolume } from './deviceinfo.interface'; | ||
static displayMetrics(): DisplayMetrics; | ||
static wifiIpv4Address(): Promise<string>; | ||
static cellularIpv4Address(): Promise<string>; | ||
static isPortrait(): boolean; | ||
@@ -46,2 +48,3 @@ static isTablet(): boolean; | ||
private static checkStorageMountState; | ||
private static ipv4Address; | ||
} |
@@ -312,2 +312,8 @@ var DeviceInfo_1; | ||
} | ||
static wifiIpv4Address() { | ||
return DeviceInfo_1.ipv4Address(); | ||
} | ||
static cellularIpv4Address() { | ||
return DeviceInfo_1.ipv4Address(); | ||
} | ||
static isPortrait() { | ||
@@ -384,14 +390,14 @@ const Configuration = android.content.res.Configuration; | ||
static isBluetoothEnabled() { | ||
const Build = android.os.Build; | ||
const BluetoothAdapter = android.bluetooth.BluetoothAdapter; | ||
const ctx = application.android.context; | ||
let btAdapter = null; | ||
if (Build.VERSION.SDK_INT > JELLY_BEAN_MR1) { | ||
const btm = ctx.getSystemService(application.android.context.BLUETOOTH_SERVICE); | ||
btAdapter = btm.getAdapter(); | ||
} | ||
else { | ||
btAdapter = BluetoothAdapter.getDefaultAdapter(); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const BluetoothAdapter = android.bluetooth.BluetoothAdapter; | ||
const Build = android.os.Build; | ||
const ctx = application.android.context; | ||
let btAdapter = null; | ||
if (Build.VERSION.SDK_INT > JELLY_BEAN_MR1) { | ||
const btm = ctx.getSystemService(android.content.Context.BLUETOOTH_SERVICE); | ||
btAdapter = btm ? btm.getAdapter() : null; | ||
} | ||
else { | ||
btAdapter = BluetoothAdapter.getDefaultAdapter(); | ||
} | ||
const permission = android.Manifest.permission; | ||
@@ -402,3 +408,3 @@ const contextCompat = DeviceInfo_1.androidSupport().content.ContextCompat; | ||
if (permissionStatus === PackageManager.PERMISSION_GRANTED) { | ||
resolve(btAdapter && btAdapter.getState() === BluetoothAdapter.STATE_ON); | ||
resolve(btAdapter && btAdapter.isEnabled()); | ||
} | ||
@@ -543,2 +549,28 @@ else { | ||
} | ||
static ipv4Address() { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const NetworkInterface = java.net.NetworkInterface; | ||
const Collections = java.util.Collections; | ||
const interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); | ||
for (let index = 0; index < interfaces.size(); index++) { | ||
const netInterface = interfaces.get(index); | ||
const addrs = Collections.list(netInterface.getInetAddresses()); | ||
for (let addrIndex = 0; addrIndex < addrs.size(); addrIndex++) { | ||
const addr = addrs.get(addrIndex); | ||
if (!addr.isLoopbackAddress()) { | ||
const hostAddr = addr.getHostAddress(); | ||
const isIPv4 = hostAddr.indexOf(':') < 0; | ||
if (isIPv4) { | ||
return resolve(hostAddr); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception) { | ||
} | ||
resolve(""); | ||
}); | ||
} | ||
}; | ||
@@ -545,0 +577,0 @@ DeviceInfo = DeviceInfo_1 = __decorate([ |
@@ -28,2 +28,11 @@ export declare const enum RadioAccessTechnology { | ||
} | ||
export declare enum AddressType { | ||
IPv4 = 2, | ||
IPv6 = 30 | ||
} | ||
export interface Address { | ||
address: string; | ||
type: AddressType; | ||
adapterName?: string; | ||
} | ||
export interface Carrier { | ||
@@ -81,2 +90,4 @@ carrierName: string; | ||
displayMetrics(): DisplayMetrics; | ||
wifiIpv4Address(): Promise<string>; | ||
cellularIpv4Address(): Promise<string>; | ||
isPortrait(): boolean; | ||
@@ -83,0 +94,0 @@ isTablet(): boolean; |
@@ -0,1 +1,6 @@ | ||
export var AddressType; | ||
(function (AddressType) { | ||
AddressType[AddressType["IPv4"] = 2] = "IPv4"; | ||
AddressType[AddressType["IPv6"] = 30] = "IPv6"; | ||
})(AddressType || (AddressType = {})); | ||
export function wirelessCellularGenerator(rat) { | ||
@@ -2,0 +7,0 @@ switch (rat) { |
@@ -29,2 +29,4 @@ import { Carrier, DisplayMetrics, StorageVolume } from './deviceinfo.interface'; | ||
static displayMetrics(): DisplayMetrics; | ||
static wifiIpv4Address(): Promise<string>; | ||
static cellularIpv4Address(): Promise<string>; | ||
static isPortrait(): boolean; | ||
@@ -39,2 +41,3 @@ static isTablet(): boolean; | ||
private static prepareCarrier; | ||
private static getInterfaceCardIpAddress; | ||
} |
var DeviceInfo_1; | ||
import { wirelessCellularGenerator } from './deviceinfo.interface'; | ||
import { AddressType, wirelessCellularGenerator } from './deviceinfo.interface'; | ||
import { networkProviderByMccMnc } from './network-provider'; | ||
@@ -179,3 +179,4 @@ import { round } from "./utility"; | ||
const interfaceName = CFArrayGetValueAtIndex(interfaces, 0); | ||
const dict = CNCopyCurrentNetworkInfo(interfaceName); | ||
var reference = new interop.Reference(interop.types.unichar, interfaceName); | ||
const dict = CNCopyCurrentNetworkInfo(reference.value); | ||
if (dict !== null && dict.count) { | ||
@@ -210,2 +211,20 @@ return dict.objectForKey(kCNNetworkInfoKeySSID); | ||
} | ||
static wifiIpv4Address() { | ||
return new Promise((resolve, reject) => { | ||
const iPhoneWifiInterfaceName = "en0"; | ||
const appleTvWifiInterfaceName = "en1"; | ||
let addresses = DeviceInfo_1.getInterfaceCardIpAddress(iPhoneWifiInterfaceName, AddressType.IPv4); | ||
if (addresses.length === 0 || addresses[0].address === "") { | ||
addresses = DeviceInfo_1.getInterfaceCardIpAddress(appleTvWifiInterfaceName, AddressType.IPv4); | ||
} | ||
return resolve(addresses.length ? addresses[0].address : ""); | ||
}); | ||
} | ||
static cellularIpv4Address() { | ||
return new Promise((resolve, reject) => { | ||
const iPhonePhoneDataServiceInterfaceName = "pdp_ip0"; | ||
let addresses = DeviceInfo_1.getInterfaceCardIpAddress(iPhonePhoneDataServiceInterfaceName, AddressType.IPv4); | ||
return resolve(addresses.length ? addresses[0].address : ""); | ||
}); | ||
} | ||
static isPortrait() { | ||
@@ -267,2 +286,50 @@ return UIDevice.currentDevice.orientation === 1 || | ||
} | ||
static getInterfaceCardIpAddress(interfaceCardName, ipAddressType) { | ||
const MAX_ADDRLEN = 46; | ||
let addresses = []; | ||
let iPtrPtr = new interop.Reference(); | ||
if (getifaddrs(iPtrPtr) === 0) { | ||
const interfacesPtr = iPtrPtr.value; | ||
let tempAddrPtr = interfacesPtr; | ||
while (tempAddrPtr != null) { | ||
let sinAddr = null; | ||
const addr = tempAddrPtr.value; | ||
const adapterName = NSString.stringWithUTF8String(addr.ifa_name).toString(); | ||
const sa = new interop.Reference(sockaddr, addr.ifa_addr).value; | ||
const sinFamily = sa.sa_family; | ||
if (sinFamily === 2) { | ||
sinAddr = interop.handleof(addr.ifa_addr).add(4); | ||
} | ||
else if (sinFamily === 30) { | ||
sinAddr = interop.handleof(addr.ifa_addr).add(8); | ||
} | ||
if (sinAddr) { | ||
const addrStr = interop.alloc(MAX_ADDRLEN); | ||
const result = inet_ntop(sinFamily, sinAddr, addrStr, MAX_ADDRLEN); | ||
if (result) { | ||
const addr = NSString.stringWithUTF8String(addrStr).toString(); | ||
let address = { | ||
address: addr, | ||
type: sinFamily, | ||
adapterName: adapterName | ||
}; | ||
if (interfaceCardName == null && ipAddressType == null) { | ||
addresses.push(address); | ||
} | ||
else if (adapterName === interfaceCardName && ipAddressType == null) { | ||
addresses.push(address); | ||
} | ||
else if (adapterName === interfaceCardName && ipAddressType === sinFamily) { | ||
addresses = []; | ||
addresses.push(address); | ||
return addresses; | ||
} | ||
} | ||
} | ||
tempAddrPtr = addr.ifa_next; | ||
} | ||
freeifaddrs(interfacesPtr); | ||
} | ||
return addresses; | ||
} | ||
}; | ||
@@ -269,0 +336,0 @@ DeviceInfo.radioAccessTechnology = new Map([ |
@@ -29,2 +29,4 @@ import { Carrier, StorageVolume, DisplayMetrics, RadioAccessTechnology, WCTGeneration } from './deviceinfo.interface'; | ||
static displayMetrics(): DisplayMetrics; | ||
static wifiIpv4Address(): Promise<string>; | ||
static cellularIpv4Address(): Promise<string>; | ||
static isPortrait(): boolean; | ||
@@ -31,0 +33,0 @@ static isTablet(): boolean; |
{ | ||
"name": "nativescript-dna-deviceinfo", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "NativeScript plugin to acquire device information.", | ||
@@ -27,2 +27,3 @@ "main": "deviceinfo", | ||
"demo.android": "npm i && cd ../demo && nsc run android", | ||
"demo.android.debug": "npm i && cd ../demo && nsc debug android", | ||
"demo.reset": "cd ../demo && npx rimraf -- hooks node_modules platforms package-lock.json", | ||
@@ -69,2 +70,3 @@ "plugin.prepare": "npm run build && cd ../demo && nsc plugin remove nativescript-dna-deviceinfo && nsc plugin add ../src", | ||
"WiFi SSID", | ||
"IPv4", | ||
"Bluetooth", | ||
@@ -71,0 +73,0 @@ "Orientation", |
@@ -8,6 +8,10 @@ | ||
The plugin offers cross-platform, utility, APIs to retrieve or query device-related information. The utility APIs are available for iOS and Android platforms. | ||
The plugin offers cross-platform, utility, APIs to retrieve or query device-related information. The utility APIs are available for iOS and Android platforms. | ||
Kindly visit [typescript demo](https://github.com/DeepakArora76/nativescript-dna-deviceinfo/tree/master/demo) or [js demo](https://github.com/DeepakArora76/NativescriptDeviceInfoJSDemo.git) repository for practical implementation guidance and hints. | ||
## Changelogs: | ||
- 3.3.0: Added "wifiIpv4Address" and "cellularIpv4Address" API for iOS and Android. Fixed issues related to Bluetooth detection for Android. | ||
- 3.2.1: Updated Apple's mobile device codes types a.k.a. machine ids (e.g. `iPhone1,1`, `Watch1,1`, etc.) and their matching product names. | ||
@@ -65,3 +69,37 @@ - 3.2.0: The API "userAgent" is modified to return Promise. iOS implementation of it uses WKWebView. | ||
## APIs | ||
Below is the list of APIs with their supported platforms. Kindly visit [typescript demo](https://github.com/DeepakArora76/nativescript-dna-deviceinfo/tree/master/demo) or [js demo](https://github.com/DeepakArora76/NativescriptDeviceInfoJSDemo.git) repository for practical implementation guidance and hints. | ||
| API | Return Type | iOS | Android | | ||
| -------------------------------| -----------------------|:-----:|:---------:| | ||
| totalMemory | number | + | + | | ||
| freeMemory | number | + | + | | ||
| totalStorageSpace | number | + | + | | ||
| freeStorageSpace | number | + | + | | ||
| deviceId | string | + | + | | ||
| deviceName | string | + | + | | ||
| deviceLocale | string | + | + | | ||
| deviceCountry | string | + | + | | ||
| timezone | string | + | + | | ||
| userAgent | Promise<string> | + | + | | ||
| appName | string | + | + | | ||
| appVersion | string | + | + | | ||
| bundleId | string | + | + | | ||
| bundleNumber | string | + | - | | ||
| systemManufacturer | string | + | + | | ||
| batteryLevel | number | + | + | | ||
| cellularServiceProviders | Carrier[] | + | + | | ||
| externalStoragePaths | string[] | - | + | | ||
| storageVolumes | StorageVolume[] | - | + | | ||
| wifiSSID | string | + | + | | ||
| displayMetrics | DisplayMetrics | + | + | | ||
| wifiIpv4Address | Promise<string> | + | + | | ||
| cellularIpv4Address | Promise<string> | + | + | | ||
| isPortrait | boolean | + | + | | ||
| isTablet | boolean | + | + | | ||
| is24Hour | boolean | + | + | | ||
| isEmulator | boolean | + | + | | ||
| isBatteryCharging | boolean | + | + | | ||
| isLocationEnabled | Promise<boolean> | + | + | | ||
| isBluetoothEnabled | Promise<boolean> | + | + | | ||
### - totalMemory | ||
@@ -144,3 +182,3 @@ | ||
Returns the user agent string of a device. | ||
Returns Promise which resolves to 'user agent' if fetched successfully, otherwise 'error'. | ||
@@ -272,2 +310,5 @@ ```javascript | ||
- Notes for iOS users: | ||
* Supported on iOS 12.0 and older versions. | ||
- Notes for Android users: | ||
@@ -326,2 +367,24 @@ * Permissions ACCESS_WIFI_STATE and ACCESS_FINE_LOCATION/ACCESS_COARSE_LOCATION are required. | ||
### - wifiIpv4Address | ||
Returns *Promise<string>* of a device. | ||
```javascript | ||
DeviceInfo.wifiIpv4Address(); | ||
``` | ||
- Notes for Android users: | ||
* Permissions *android.permission.INTERNET*, *android.permission.ACCESS_NETWORK_STATE*, and *android.permission.ACCESS_WIFI_STATE* are required for this API. Make sure that these permissions are in place in AndroidManifest.xml and in code too. | ||
### - cellularIpv4Address | ||
Returns *Promise<string>* of a device. | ||
```javascript | ||
DeviceInfo.cellularIpv4Address(); | ||
``` | ||
- Notes for Android users: | ||
* Permissions *android.permission.INTERNET*, *android.permission.ACCESS_NETWORK_STATE*, and *android.permission.ACCESS_WIFI_STATE* are required for this API. Make sure that these permissions are in place in AndroidManifest.xml and in code too. | ||
### - isPortrait | ||
@@ -411,6 +474,6 @@ Returns 'true' if a device is in portrait mode, otherwise 'false'. | ||
async printDeviceInfo() { | ||
console.log("Free memory: ", this.getSize(DeviceInfo.freeMemory())); | ||
console.log("Total memory: ", this.getSize(DeviceInfo.totalMemory())); | ||
console.log("Total storage space: ", this.getSize(DeviceInfo.totalStorageSpace())); | ||
console.log("Free storage space: ", this.getSize(DeviceInfo.freeStorageSpace())); | ||
console.log("Free memory: ", getSize(DeviceInfo.freeMemory())); | ||
console.log("Total memory: ", getSize(DeviceInfo.totalMemory())); | ||
console.log("Total storage space: ", getSize(DeviceInfo.totalStorageSpace())); | ||
console.log("Free storage space: ", getSize(DeviceInfo.freeStorageSpace())); | ||
console.log("Device id: ", DeviceInfo.deviceId()); | ||
@@ -438,3 +501,2 @@ console.log("Device name: ", DeviceInfo.deviceName()); | ||
console.log("Is Location service enabled: ", await DeviceInfo.isLocationEnabled().catch(error => console.log(error))); | ||
console.log("Is Bluetooth enabled: ", await DeviceInfo.isBluetoothEnabled().catch(error => console.log(error))); | ||
} | ||
@@ -453,2 +515,6 @@ | ||
} | ||
getSize(bytes: number) { | ||
return formatBytes(bytes, 2); | ||
} | ||
``` | ||
@@ -455,0 +521,0 @@ |
@@ -1,2 +0,3 @@ | ||
/// <reference path="./node_modules/@nativescript/types/index.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-android/lib/android-28.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-ios/index.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-android/lib/android-18.d.ts" /> | ||
/// <reference path="./node_modules/@nativescript/types-android/lib/android-28.d.ts" /> |
398603
15542
517