企业网站美工设计大一html网页制作作业简单
文章目录
- 一、前言
- 二、拓展阅读
一、前言
项目开发过程中,需要获取设备信息,例如获取设备名称。可通过使用开源的第三方组件react-native-device-info,该组件适用于iOS
和Android
双平台。
在ReactNative
项目中可通过npm
命令下载 react-native-device-info 组件依赖包:
npm install --save react-native-device-info
android
需要在AndroidManifest.xml
配置文件添加android.permission.BLUETOOTH
权限
<uses-permission android:name="android.permission.BLUETOOTH"/>
具体应用示例如下,有关更多获取设备属性方法详参 react-native-device-info。
import DeviceInfo from 'react-native-device-info';
DeviceInfo.getDeviceName()
注⚠️:官方文档指出,在调用getDeviceName()
方法获取设备名称时,当版本号低于v3时,android
需要在AndroidManifest.xml
配置文件添加android.permission.BLUETOOTH
权限,若版本号不低于v3,android.permission.BLUETOOTH
权限可不添加。
DeviceInfo.getDeviceName().then((deviceName) => {// iOS: "Becca's iPhone 6"// Android: ?// Windows: ?
});
This used to require the android.permission.BLUETOOTH but the new implementation in v3 does not need it. You may remove that from your AndroidManifest.xml if you had it for this API. iOS 16 and greater require entitlements to access user-defined device name, otherwise a generic value is returned (ie. ‘iPad’, ‘iPhone’)
总结:作者曾经调用DeviceInfo.getDeviceName()
方法获取设备名称,但获取值一直为undefined
,曾怀疑由于手机隐私设置导致无法获取,但是其他属性却可以获取,故可排除手机隐私设置所致。经过研读插件文档,才发现自己所引用的react-native-device-info
插件版本号为2.3.2,需要在AndroidManifest.xml
配置文件添加android.permission.BLUETOOTH
权限,而通过检查AndroidManifest.xml
配置文件,发现缺少添加android.permission.BLUETOOTH
权限,这才导致获取值为undefined
,故通过在AndroidManifest.xml
配置文件添加android.permission.BLUETOOTH
权限获取到设备名称。
二、拓展阅读
- 《react-native-device-info 插件文档》