Skip to content

Commit

Permalink
feat: Added getSerialNumber for windows (#1287)
Browse files Browse the repository at this point in the history
* Feat: Implementation SerialNumber for windows
  • Loading branch information
KewtonV authored Aug 12, 2021
1 parent 876666a commit 5b44183
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Most APIs return a Promise but also have a corresponding API with `Sync` on the

The example app in this repository shows an example usage of every single API, consult the example app if you have questions, and if you think you see a problem make sure you can reproduce it using the example app before reporting it, thank you.

| Method | Return Type |  iOS | Android | Windows | Web |
| Method | Return Type | iOS | Android | Windows | Web |
| ----------------------------------------------------------------- | ------------------- | :--: | :-----: | :-----: | :-: |
| [getAndroidId()](#getandroidid) | `Promise<string>` |||||
| [getApiLevel()](#getapilevel) | `Promise<number>` |||||
Expand Down Expand Up @@ -152,7 +152,7 @@ The example app in this repository shows an example usage of every single API, c
| [getProduct()](#getproduct) | `Promise<string>` |||||
| [getPreviewSdkInt()](#getPreviewSdkInt) | `Promise<number>` |||||
| [getReadableVersion()](#getreadableversion) | `string` |||||
| [getSerialNumber()](#getserialnumber) | `Promise<string>` ||| ||
| [getSerialNumber()](#getserialnumber) | `Promise<string>` ||| ||
| [getSecurityPatch()](#getsecuritypatch) | `Promise<string>` |||||
| [getSystemAvailableFeatures()](#getSystemAvailableFeatures) | `Promise<string[]>` |||||
| [getSystemName()](#getsystemname) | `string` |||||
Expand Down Expand Up @@ -841,9 +841,12 @@ Gets the device serial number. Will be 'unknown' in almost all cases [unless you
DeviceInfo.getSerialNumber().then((serialNumber) => {
// iOS: unknown
// Android: ? (maybe a serial number, if your app is privileged)
// Windows: unknown
// Windows: ? (a serial number, if your app has the "capability smbios")
});
```
## Notes
### capability smbios
If you want to use this method in windows, you have to add smbios capability in your aplication. Please following this [documentation](https://docs.microsoft.com/en-us/windows/win32/sysinfo/access-smbios-information-from-a-universal-windows-app) for add the capability in your manifest file.
---
Expand Down
1 change: 0 additions & 1 deletion example/windows/example.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29215.179
Expand Down
4 changes: 3 additions & 1 deletion example/windows/example/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
IgnorableNamespaces="uap mp">
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">

<Identity
Name="RNDeviceInfoExample"
Expand Down Expand Up @@ -46,5 +47,6 @@

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="smbios"/>
</Capabilities>
</Package>
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const [getInstanceId, getInstanceIdSync] = getSupportedPlatformInfoFuncti

export const [getSerialNumber, getSerialNumberSync] = getSupportedPlatformInfoFunctions({
memoKey: 'serialNumber',
supportedPlatforms: ['android'],
supportedPlatforms: ['android', 'windows'],
getter: () => RNDeviceInfo.getSerialNumber(),
syncGetter: () => RNDeviceInfo.getSerialNumberSync(),
defaultValue: 'unknown',
Expand Down
20 changes: 20 additions & 0 deletions windows/code/RNDeviceInfoCPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace winrt::RNDeviceInfoCPP
{
provider.Add(L"uniqueId", getUniqueIdSync());
provider.Add(L"deviceId", getDeviceIdSync());
provider.Add(L"serialNumber", getSerialNumberSync());
provider.Add(L"bundleId", getBundleIdSync());
provider.Add(L"systemVersion", getSystemVersionSync());
provider.Add(L"appVersion", getAppVersionSync());
Expand Down Expand Up @@ -550,6 +551,25 @@ namespace winrt::RNDeviceInfoCPP
{
promise.Resolve(getDeviceIdSync());
}

REACT_SYNC_METHOD(getSerialNumberSync);
std::string getSerialNumberSync() noexcept
{
try
{
return winrt::to_string(Windows::System::Profile::SystemManufacturers::SmbiosInformation::SerialNumber().c_str());
}
catch (...)
{
return "unknown";
}
}

REACT_METHOD(getSerialNumber);
void getSerialNumber(ReactPromise<std::string> promise) noexcept
{
promise.Resolve(getSerialNumberSync());
}

REACT_SYNC_METHOD(getSystemManufacturerSync);
std::string getSystemManufacturerSync() noexcept
Expand Down
1 change: 1 addition & 0 deletions windows/code/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
#include <winrt/Windows.Services.Store.h>
#include <winrt/Windows.Data.Json.h>
#include <winrt/Windows.System.Power.h>
#include <winrt/Windows.System.Profile.SystemManufacturers.h>

0 comments on commit 5b44183

Please sign in to comment.