Skip to content

Commit 5b44183

Browse files
authored
feat: Added getSerialNumber for windows (#1287)
* Feat: Implementation SerialNumber for windows
1 parent 876666a commit 5b44183

6 files changed

Lines changed: 31 additions & 6 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Most APIs return a Promise but also have a corresponding API with `Sync` on the
108108

109109
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.
110110

111-
| Method | Return Type |  iOS | Android | Windows | Web |
111+
| Method | Return Type | iOS | Android | Windows | Web |
112112
| ----------------------------------------------------------------- | ------------------- | :--: | :-----: | :-----: | :-: |
113113
| [getAndroidId()](#getandroidid) | `Promise<string>` |||||
114114
| [getApiLevel()](#getapilevel) | `Promise<number>` |||||
@@ -152,7 +152,7 @@ The example app in this repository shows an example usage of every single API, c
152152
| [getProduct()](#getproduct) | `Promise<string>` |||||
153153
| [getPreviewSdkInt()](#getPreviewSdkInt) | `Promise<number>` |||||
154154
| [getReadableVersion()](#getreadableversion) | `string` |||||
155-
| [getSerialNumber()](#getserialnumber) | `Promise<string>` ||| ||
155+
| [getSerialNumber()](#getserialnumber) | `Promise<string>` ||| ||
156156
| [getSecurityPatch()](#getsecuritypatch) | `Promise<string>` |||||
157157
| [getSystemAvailableFeatures()](#getSystemAvailableFeatures) | `Promise<string[]>` |||||
158158
| [getSystemName()](#getsystemname) | `string` |||||
@@ -841,9 +841,12 @@ Gets the device serial number. Will be 'unknown' in almost all cases [unless you
841841
DeviceInfo.getSerialNumber().then((serialNumber) => {
842842
// iOS: unknown
843843
// Android: ? (maybe a serial number, if your app is privileged)
844-
// Windows: unknown
844+
// Windows: ? (a serial number, if your app has the "capability smbios")
845845
});
846846
```
847+
## Notes
848+
### capability smbios
849+
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.
847850
848851
---
849852

example/windows/example.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Microsoft Visual Studio Solution File, Format Version 12.00
32
# Visual Studio Version 16
43
VisualStudioVersion = 16.0.29215.179

example/windows/example/Package.appxmanifest

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
55
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
66
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
7-
IgnorableNamespaces="uap mp">
7+
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
8+
IgnorableNamespaces="uap mp rescap">
89

910
<Identity
1011
Name="RNDeviceInfoExample"
@@ -46,5 +47,6 @@
4647

4748
<Capabilities>
4849
<Capability Name="internetClient" />
50+
<rescap:Capability Name="smbios"/>
4951
</Capabilities>
5052
</Package>

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const [getInstanceId, getInstanceIdSync] = getSupportedPlatformInfoFuncti
4444

4545
export const [getSerialNumber, getSerialNumberSync] = getSupportedPlatformInfoFunctions({
4646
memoKey: 'serialNumber',
47-
supportedPlatforms: ['android'],
47+
supportedPlatforms: ['android', 'windows'],
4848
getter: () => RNDeviceInfo.getSerialNumber(),
4949
syncGetter: () => RNDeviceInfo.getSerialNumberSync(),
5050
defaultValue: 'unknown',

windows/code/RNDeviceInfoCPP.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace winrt::RNDeviceInfoCPP
2727
{
2828
provider.Add(L"uniqueId", getUniqueIdSync());
2929
provider.Add(L"deviceId", getDeviceIdSync());
30+
provider.Add(L"serialNumber", getSerialNumberSync());
3031
provider.Add(L"bundleId", getBundleIdSync());
3132
provider.Add(L"systemVersion", getSystemVersionSync());
3233
provider.Add(L"appVersion", getAppVersionSync());
@@ -550,6 +551,25 @@ namespace winrt::RNDeviceInfoCPP
550551
{
551552
promise.Resolve(getDeviceIdSync());
552553
}
554+
555+
REACT_SYNC_METHOD(getSerialNumberSync);
556+
std::string getSerialNumberSync() noexcept
557+
{
558+
try
559+
{
560+
return winrt::to_string(Windows::System::Profile::SystemManufacturers::SmbiosInformation::SerialNumber().c_str());
561+
}
562+
catch (...)
563+
{
564+
return "unknown";
565+
}
566+
}
567+
568+
REACT_METHOD(getSerialNumber);
569+
void getSerialNumber(ReactPromise<std::string> promise) noexcept
570+
{
571+
promise.Resolve(getSerialNumberSync());
572+
}
553573

554574
REACT_SYNC_METHOD(getSystemManufacturerSync);
555575
std::string getSystemManufacturerSync() noexcept

windows/code/pch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
#include <winrt/Windows.Services.Store.h>
1818
#include <winrt/Windows.Data.Json.h>
1919
#include <winrt/Windows.System.Power.h>
20+
#include <winrt/Windows.System.Profile.SystemManufacturers.h>

0 commit comments

Comments
 (0)