You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-native-device-info的getDeviceName在Android端返回unknown求助

Fixing getDeviceName() Returning "unknown" on Android with react-native-device-info v0.25.0

Hey there, I’ve run into this exact issue before with older versions of react-native-device-info — let’s break down how to fix it:

Why this happens

In v0.25.0, the Android implementation of getDeviceName() relies on the BluetoothAdapter to fetch the device’s user-friendly name. If Bluetooth is disabled, your app lacks the necessary permissions, or the device doesn’t expose this name via Bluetooth, it falls back to returning "unknown". iOS doesn’t have this limitation because it uses a direct system API to grab the device name.

Solution 1: Add Bluetooth permissions (quick fix for v0.25.0)

If you want to stick with the old version, update your android/app/src/main/AndroidManifest.xml to include these permissions:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

For Android 6.0 (API 23) and above, you’ll also need to request these permissions dynamically at runtime in your React Native code. You can use a permission-handling library or write a small native module to trigger the permission request.

Solution 2: Use a fallback to get the device name without Bluetooth

If you don’t want to rely on Bluetooth, create a custom native module to fetch the device model/manufacturer directly from Android’s Build class. Here’s a quick snippet of the native code:

// In your Android module
import android.os.Build;

public String getDeviceName() {
    String manufacturer = Build.MANUFACTURER;
    String model = Build.MODEL;
    if (model.startsWith(manufacturer)) {
        return capitalize(model);
    } else {
        return capitalize(manufacturer) + " " + model;
    }
}

private String capitalize(String s) {
    if (s == null || s.length() == 0) {
        return "";
    }
    char first = s.charAt(0);
    return Character.isUpperCase(first) ? s : Character.toUpperCase(first) + s.substring(1);
}

You can then call this method from your React Native code via NativeModules.

Solution 3: Upgrade react-native-device-info (best long-term fix)

v0.25.0 is extremely outdated (we’re on v10+ now). Newer versions completely changed how getDeviceName() works on Android — it no longer depends on Bluetooth, using a more reliable system API that returns the correct device name without extra permissions.

To upgrade:

  1. Uninstall the old version: npm uninstall react-native-device-info
  2. Install a recent stable version: npm install react-native-device-info@latest (or pick a specific version like v8.7.0 if you need compatibility with older React Native releases)
  3. Follow the upgrade instructions for the new version (most modern versions auto-link, but double-check for your React Native version)

After upgrading, getDeviceName() should work seamlessly on both iOS and Android without extra setup.

内容的提问来源于stack exchange,提问作者ninh_nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:49:39