Bluetooth device names change when using Nearby Connect API

30 Views Asked by At

I tried the code below.

public class NearbyConnectionUI extends HanSystemUI {
private ConnectionsClient mConnectionsClient;

private void startAdvertising(){
        mConnectionsClient
                .startAdvertising(
                        Settings.Secure.getString(mContext.getContentResolver(), "bluetooth_name"),
                        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).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())
                );
}
}

I set the device name to "jaeHyun" in the Bluetooth settings. When i run advertising, the name displayed on other devices in Bluetooth settings will change to a random string. For example "ISUEdvDFSAFEIOWFFD". And when I run stopAdvertising(), the set name will be displayed properly. I want to set it so that even when I run startAdvertising(), it is displayed as “my device” that I set.

0

There are 0 best solutions below