dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' compile 'com.google.android.gms:play-services:7.0.0' }
@Override protected void onCreate(Bundle savedInstanceState) { … mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Nearby.CONNECTIONS_API) .build(); … } @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart"); mGoogleApiClient.connect(); } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop"); if (mGoogleApiClient != null) { mGoogleApiClient.disconnect(); } }
private void startAdvertising() { debugLog("startAdvertising"); if (!isConnectedToNetwork()) { debugLog("startAdvertising: not connected to WiFi network."); return; } // . List<AppIdentifier> appIdentifierList = new ArrayList<>(); appIdentifierList.add(new AppIdentifier(getPackageName())); AppMetadata appMetadata = new AppMetadata(appIdentifierList); // . . , , "LGE Nexus 5" String name = null; Nearby.Connections.startAdvertising(mGoogleApiClient, name, appMetadata, TIMEOUT_ADVERTISE, this).setResultCallback(new ResultCallback<Connections.StartAdvertisingResult>() { @Override public void onResult(Connections.StartAdvertisingResult result) { Log.d(TAG, "startAdvertising:onResult:" + result); if (result.getStatus().isSuccess()) { debugLog("startAdvertising:onResult: SUCCESS"); updateViewVisibility(STATE_ADVERTISING); } else { debugLog("startAdvertising:onResult: FAILURE "); // 'Advertise' , 'STATUS_ALREADY_ADVERTISING' int statusCode = result.getStatus().getStatusCode(); if (statusCode == ConnectionsStatusCodes.STATUS_ALREADY_ADVERTISING) { debugLog("STATUS_ALREADY_ADVERTISING"); } else { updateViewVisibility(STATE_READY); } } } }); }
private void startDiscovery() { debugLog("startDiscovery"); if (!isConnectedToNetwork()) { debugLog("startDiscovery: not connected to WiFi network."); return; } // Nearby . String serviceId = getString(R.string.service_id); Nearby.Connections.startDiscovery(mGoogleApiClient, serviceId, TIMEOUT_DISCOVER, this) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { debugLog("startDiscovery:onResult: SUCCESS"); updateViewVisibility(STATE_DISCOVERING); } else { debugLog("startDiscovery:onResult: FAILURE"); // 'Discover' , 'STATUS_ALREADY_DISCOVERING' int statusCode = status.getStatusCode(); if (statusCode == ConnectionsStatusCodes.STATUS_ALREADY_DISCOVERING) { debugLog("STATUS_ALREADY_DISCOVERING"); } else { updateViewVisibility(STATE_READY); } } } }); }
@Override public void onEndpointFound(final String endpointId, String deviceId, String serviceId, final String endpointName) { Log.d(TAG, "onEndpointFound:" + endpointId + ":" + endpointName); // . , . if (mMyListDialog == null) { // Configure the AlertDialog that the MyListDialog wraps AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("Endpoint(s) Found") .setCancelable(true) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mMyListDialog.dismiss(); } }); // mMyListDialog = new MyListDialog(this, builder, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String selectedEndpointName = mMyListDialog.getItemKey(which); String selectedEndpointId = mMyListDialog.getItemValue(which); MainActivity.this.connectTo(selectedEndpointId, selectedEndpointName); mMyListDialog.dismiss(); } }); } mMyListDialog.addItem(endpointName, endpointId); mMyListDialog.show(); }
/** * . * @param endpointId - * @param endpointName - , . . * */ private void connectTo(String endpointId, final String endpointName) { debugLog("connectTo:" + endpointId + ":" + endpointName); // . String myName = null; byte[] myPayload = null; Nearby.Connections.sendConnectionRequest(mGoogleApiClient, myName, endpointId, myPayload, new Connections.ConnectionResponseCallback() { @Override public void onConnectionResponse(String endpointId, Status status, byte[] bytes) { Log.d(TAG, "onConnectionResponse:" + endpointId + ":" + status); if (status.isSuccess()) { debugLog("onConnectionResponse: " + endpointName + " SUCCESS"); Toast.makeText(MainActivity.this, "Connected to " + endpointName, Toast.LENGTH_SHORT).show(); mOtherEndpointId = endpointId; updateViewVisibility(STATE_CONNECTED); } else { debugLog("onConnectionResponse: " + endpointName + " FAILURE"); } } }, this); }
@Override public void onConnectionRequest(final String endpointId, String deviceId, String endpointName, byte[] payload) { debugLog("onConnectionRequest:" + endpointId + ":" + endpointName); // . . mConnectionRequestDialog = new AlertDialog.Builder(this) .setTitle("Connection Request") .setMessage("Do you want to connect to " + endpointName + "?") .setCancelable(false) .setPositiveButton("Connect", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { byte[] payload = null; Nearby.Connections.acceptConnectionRequest(mGoogleApiClient, endpointId, payload, MainActivity.this) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { debugLog("acceptConnectionRequest: SUCCESS"); mOtherEndpointId = endpointId; updateViewVisibility(STATE_CONNECTED); } else { debugLog("acceptConnectionRequest: FAILURE"); } } }); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Nearby.Connections.rejectConnectionRequest(mGoogleApiClient, endpointId); } }).create(); mConnectionRequestDialog.show(); }
@Override public void onMessageReceived(String endpointId, byte[] payload, boolean isReliable) { // , debugLog("onMessageReceived:" + endpointId + ":" + new String(payload)); }
<?xml version="1.0" encoding="utf-8"?> <resources> ... <string name="service_id"><!-- , ..--></string> ... </resources>
<application> <meta-data android:name="com.google.android.gms.nearby.connection.SERVICE_ID" android:value="@string/service_id" /> <activity> ... </activity> </application>
Source: https://habr.com/ru/post/256071/
All Articles