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

使用Nearby Connect API时蓝牙设备名称异常变更问题求助

问题分析与解决方案

问题根源

使用Google Nearby Connections的Strategy.P2P_STAR策略启动广告时,SDK默认会将蓝牙设备名称替换为随机字符串,这是为了在点对点连接场景中保护设备隐私,避免直接暴露真实设备名称。

解决方法

要保留自定义设备名称,只需在构建AdvertisingOptions时,通过setCustomName()方法显式指定要展示的设备名称即可,P2P_STAR策略支持该配置。

修改后的代码

public class NearbyConnectionUI extends HanSystemUI {
    private ConnectionsClient mConnectionsClient;

    private void startAdvertising(){
        // 获取当前蓝牙自定义名称
        String customDeviceName = Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_name");
        // 也可以通过BluetoothAdapter获取更准确的当前蓝牙名称
        // BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        // String customDeviceName = bluetoothAdapter != null ? bluetoothAdapter.getName() : "MyDevice";

        mConnectionsClient
                .startAdvertising(
                        customDeviceName,
                        BuildConfig.APPLICATION_ID, // serviceID
                        new ConnectionLifecycleCallback() {

                            @Override
                            public void onConnectionInitiated(@NonNull String endpointID, @NonNull ConnectionInfo connectionInfo) {
                                Log.d(TAG, "onConnectionInitiated: endpointID : " + endpointID + "endpointName : " + connectionInfo.getEndpointName());

                                mNearbyConfirmDlg.setDeviceName(connectionInfo.getEndpointName());
                                mNearbyConfirmDlg.setConnectionConfirmInterface(result -> {
                                    if ("ok".equals(result)) {
                                        acceptConnection(endpointID);
                                    } else {

                                    }
                                });

                                mNearbyConfirmDlg.show();
                            }

                            @Override
                            public void onConnectionResult(@NonNull String endpointID, @NonNull ConnectionResolution connectionsResult) {
                                Log.d(TAG, "onConnectionResult: endpointID : " + endpointID + ", result : " + connectionsResult.getStatus().getStatusCode());
                                if (connectionsResult.getStatus().isSuccess()) {

                                }
                            }

                            @Override
                            public void onDisconnected(@NonNull String endpointID) {
                                Log.d(TAG, "onDisconnected: endpointID : " + endpointID);
                            }
                        },
                        new AdvertisingOptions.Builder()
                                .setStrategy(Strategy.P2P_STAR)
                                .setCustomName(customDeviceName) // 关键:添加自定义名称配置
                                .build()
                )
                .addOnSuccessListener(
                        unusedResult ->
                                Log.d(TAG, "advertising success. endpointName : " +
                                        Settings.Global.getString(mContext.getContentResolver(), Settings.Global.DEVICE_NAME))
                )
                .addOnFailureListener(
                        exception ->
                                Log.d(TAG, "advertising fail. exception : " + exception.getMessage())
                );
    }
}

额外注意事项

  • 确保应用已申请必要的蓝牙权限:Android 12及以上需要BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT权限;Android 11及以下需要BLUETOOTH权限,权限需在Manifest中声明并完成动态申请。
  • 部分系统对设备名称有长度限制,建议自定义名称不超过16个字符,避免被系统截断或强制替换。

内容的提问来源于stack exchange,提问作者정재현

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:53:19