@@ -184,7 +184,6 @@ public WifiP2pService(Context context) {
184184
185185 mThisDevice .primaryDeviceType = mContext .getResources ().getString (
186186 com .android .internal .R .string .config_wifi_p2p_device_type );
187- mThisDevice .deviceName = getDefaultDeviceName ();
188187
189188 mP2pStateMachine = new P2pStateMachine (TAG , mP2pSupported );
190189 mP2pStateMachine .start ();
@@ -205,14 +204,6 @@ private void enforceChangePermission() {
205204 "WifiP2pService" );
206205 }
207206
208- /* We use the 4 digits of the ANDROID_ID to have a friendly
209- * default that has low likelihood of collision with a peer */
210- private String getDefaultDeviceName () {
211- String id = Settings .Secure .getString (mContext .getContentResolver (),
212- Settings .Secure .ANDROID_ID );
213- return "Android_" + id .substring (0 ,4 );
214- }
215-
216207 /**
217208 * Get a reference to handler. This is used by a client to establish
218209 * an AsyncChannel communication with WifiP2pService
@@ -376,6 +367,10 @@ public boolean processMessage(Message message) {
376367 WifiP2pManager .CLEAR_SERVICE_REQUESTS_FAILED ,
377368 WifiP2pManager .BUSY );
378369 break ;
370+ case WifiP2pManager .SET_DEVICE_NAME :
371+ replyToMessage (message , WifiP2pManager .SET_DEVICE_NAME_FAILED ,
372+ WifiP2pManager .BUSY );
373+ break ;
379374 case WifiP2pManager .REQUEST_PEERS :
380375 replyToMessage (message , WifiP2pManager .RESPONSE_PEERS , mPeers );
381376 break ;
@@ -474,6 +469,10 @@ public boolean processMessage(Message message) {
474469 WifiP2pManager .CLEAR_SERVICE_REQUESTS_FAILED ,
475470 WifiP2pManager .P2P_UNSUPPORTED );
476471 break ;
472+ case WifiP2pManager .SET_DEVICE_NAME :
473+ replyToMessage (message , WifiP2pManager .SET_DEVICE_NAME_FAILED ,
474+ WifiP2pManager .P2P_UNSUPPORTED );
475+ break ;
477476 default :
478477 return NOT_HANDLED ;
479478 }
@@ -583,6 +582,16 @@ public boolean processMessage(Message message) {
583582 mWifiNative .closeSupplicantConnection ();
584583 transitionTo (mP2pDisablingState );
585584 break ;
585+ case WifiP2pManager .SET_DEVICE_NAME :
586+ WifiP2pDevice d = (WifiP2pDevice ) message .obj ;
587+ if (d != null && setAndPersistDeviceName (d .deviceName )) {
588+ if (DBG ) logd ("set device name " + d .deviceName );
589+ replyToMessage (message , WifiP2pManager .SET_DEVICE_NAME_SUCCEEDED );
590+ } else {
591+ replyToMessage (message , WifiP2pManager .SET_DEVICE_NAME_FAILED ,
592+ WifiP2pManager .ERROR );
593+ }
594+ break ;
586595 case WifiP2pManager .DISCOVER_PEERS :
587596 // do not send service discovery request while normal find operation.
588597 clearSupplicantServiceRequest ();
@@ -1412,8 +1421,39 @@ private void p2pConnectWithPinDisplay(WifiP2pConfig config, boolean join) {
14121421 }
14131422 }
14141423
1424+ private String getPersistedDeviceName () {
1425+ String deviceName = Settings .Secure .getString (mContext .getContentResolver (),
1426+ Settings .Secure .WIFI_P2P_DEVICE_NAME );
1427+ if (deviceName == null ) {
1428+ /* We use the 4 digits of the ANDROID_ID to have a friendly
1429+ * default that has low likelihood of collision with a peer */
1430+ String id = Settings .Secure .getString (mContext .getContentResolver (),
1431+ Settings .Secure .ANDROID_ID );
1432+ return "Android_" + id .substring (0 ,4 );
1433+ }
1434+ return deviceName ;
1435+ }
1436+
1437+ private boolean setAndPersistDeviceName (String devName ) {
1438+ if (devName == null ) return false ;
1439+
1440+ if (!mWifiNative .setDeviceName (devName )) {
1441+ loge ("Failed to set device name " + devName );
1442+ return false ;
1443+ }
1444+
1445+ mThisDevice .deviceName = devName ;
1446+ mWifiNative .setP2pSsidPostfix ("-" + mThisDevice .deviceName );
1447+
1448+ Settings .Secure .putString (mContext .getContentResolver (),
1449+ Settings .Secure .WIFI_P2P_DEVICE_NAME , devName );
1450+ sendThisDeviceChangedBroadcast ();
1451+ return true ;
1452+ }
1453+
14151454 private void initializeP2pSettings () {
14161455 mWifiNative .setPersistentReconnect (true );
1456+ mThisDevice .deviceName = getPersistedDeviceName ();
14171457 mWifiNative .setDeviceName (mThisDevice .deviceName );
14181458 //DIRECT-XY-DEVICENAME (XY is randomly generated)
14191459 mWifiNative .setP2pSsidPostfix ("-" + mThisDevice .deviceName );
0 commit comments