gpt4 book ai didi

java - 无法通过 Google Nearby API 连接两个设备

转载 作者:行者123 更新时间:2023-12-01 17:19:12 26 4
gpt4 key购买 nike

我正在开发一个应用程序,可以将手机上的本地音乐分享到其他设备。为了连接多个设备,我使用 Google 的附近 API。开始在两台设备上进行通告和发现后,onEndpointFound 会在两台设备上调用已找到的设备。要接受此连接,应用程序应显示一个 AlertDialog。这发生在 onConnectionInitiated 方法中。但由于某种原因,应用程序无法到达构建 AlertDialog 的部分。

有人知道如何解决这个问题吗?

这是代码:

package com.shuzo.musicshare;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.connection.AdvertisingOptions;
import com.google.android.gms.nearby.connection.ConnectionInfo;
import com.google.android.gms.nearby.connection.ConnectionLifecycleCallback;
import com.google.android.gms.nearby.connection.ConnectionResolution;
import com.google.android.gms.nearby.connection.ConnectionsClient;
import com.google.android.gms.nearby.connection.ConnectionsStatusCodes;
import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo;
import com.google.android.gms.nearby.connection.DiscoveryOptions;
import com.google.android.gms.nearby.connection.EndpointDiscoveryCallback;
import com.google.android.gms.nearby.connection.Payload;
import com.google.android.gms.nearby.connection.PayloadCallback;
import com.google.android.gms.nearby.connection.PayloadTransferUpdate;
import com.google.android.gms.nearby.connection.Strategy;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MusicShare";

private static final String[] REQUIRED_PERMISSIONS =
new String[] {
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION,
};

private static final int REQUEST_CODE_REQUIRED_PERMISSIONS = 1;

private static final Strategy STRATEGY = Strategy.P2P_STAR;

EditText usernameEdit;
Button connectButton;

private ConnectionsClient connectionsClient;

// Callbacks for receiving payloads
private final PayloadCallback payloadCallback =
new PayloadCallback() {
@Override
public void onPayloadReceived(String endpointId, Payload payload) {

}

@Override
public void onPayloadTransferUpdate(String endpointId, PayloadTransferUpdate update) {

}
};

// Callbacks for connections to devices
private final ConnectionLifecycleCallback connectionLifecycleCallback =
new ConnectionLifecycleCallback() {
@Override
public void onConnectionInitiated(final String endpointId, ConnectionInfo connectionInfo) {
Log.i(TAG, "onConnectionInitiated: accepting connection");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getApplicationContext());
alertDialog
.setTitle(R.string.accept_connection_title + " " + connectionInfo.getEndpointName())
.setMessage(R.string.confirm_codes + " " + connectionInfo.getAuthenticationToken())
.setPositiveButton(
R.string.accept_connection,
(DialogInterface dialog, int which) ->
connectionsClient.acceptConnection(endpointId, payloadCallback))
.setNegativeButton(
android.R.string.cancel,
(DialogInterface dialog, int which) ->
connectionsClient.rejectConnection(endpointId))
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}

@Override
public void onConnectionResult(String endpointId, ConnectionResolution result) {
switch (result.getStatus().getStatusCode()) {
case ConnectionsStatusCodes.STATUS_OK:
Log.i(TAG, "onConnectionResult: status ok");
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
break;
case ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED:
Log.i(TAG, "onConnectionResult: connection rejected");
Toast.makeText(getApplicationContext(), "Connection rejected", Toast.LENGTH_SHORT).show();
break;
case ConnectionsStatusCodes.STATUS_ERROR:
Log.i(TAG, "onConnectionResult: error");
Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_SHORT).show();
break;
default:
// Unknown status code
}
}

@Override
public void onDisconnected(String endpointId) {
Log.i(TAG, "Device disconnected");
}
};

private final EndpointDiscoveryCallback endpointDiscoveryCallback =
new EndpointDiscoveryCallback() {
@Override
public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) {
Log.i(TAG, "onEndpointFound: endpoint found, connecting");
connectionsClient
.requestConnection(getUserNickname(), endpointId, connectionLifecycleCallback)
.addOnSuccessListener(
(Void unused) -> {
Log.i(TAG, "onEndpointFound: successful");
})
.addOnFailureListener(
(Exception e) -> {
Log.i(TAG, "onEndpointFound: failed");
});
}

@Override
public void onEndpointLost(String endpointId) {

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

connectionsClient = Nearby.getConnectionsClient(this);

connectButton = findViewById(R.id.connectButton);

connectButton.setOnClickListener(view -> {
startAdvertising();
startDiscovery();
connectButton.setEnabled(false);
});
}

@Override
protected void onStart() {
super.onStart();

if (!hasPermissions(this, REQUIRED_PERMISSIONS)) {
requestPermissions(REQUIRED_PERMISSIONS, REQUEST_CODE_REQUIRED_PERMISSIONS);
}
}

private static boolean hasPermissions(Context context, String... permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

private void startAdvertising() {
AdvertisingOptions advertisingOptions =
new AdvertisingOptions.Builder().setStrategy(STRATEGY).build();
connectionsClient
.startAdvertising(getUserNickname(), getPackageName(), connectionLifecycleCallback, advertisingOptions)
.addOnSuccessListener(
(Void unused) -> {
Toast.makeText(getApplicationContext(), R.string.advertising_success, Toast.LENGTH_SHORT).show();
})
.addOnFailureListener(
(Exception e) -> {
Toast.makeText(getApplicationContext(), R.string.advertising_fail, Toast.LENGTH_SHORT).show();
});
}

private void startDiscovery() {
DiscoveryOptions discoveryOptions =
new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();
connectionsClient
.startDiscovery(getPackageName(), endpointDiscoveryCallback, discoveryOptions)
.addOnSuccessListener(
(Void unused) -> {
Toast.makeText(getApplicationContext(), R.string.discovery_success, Toast.LENGTH_SHORT).show();
})
.addOnFailureListener(
(Exception e) -> {
Toast.makeText(getApplicationContext(), R.string.discovery_failed, Toast.LENGTH_SHORT).show();
});
}

private String getUserNickname() {
usernameEdit = findViewById(R.id.usernameEdit);
return usernameEdit.getText().toString();
}
}

最佳答案

鉴于评论中的其他详细信息,这似乎是蓝牙经典问题。我查看了我们的记录,我们都有报告的设备,因此我们将尝试在我们这边重现。

您可以执行的一些步骤:1) 尝试重新启动设备并查看问题是否可以重现。2) 从两台设备收集日志 (adb logcat) 或错误报告 (adb bugreport) 并将其附加到本文中,或直接通过电子邮件发送给我(我的用户名}@google.com)。这些包含敏感数据,因此仅当您在两台设备上都有测试帐户时才发送它。如果您有个人帐户,则先过滤日志(adb logcat NearbyConnections:* NearbyMediums:* *:S)

免责声明:我从事“附近连接”工作

关于java - 无法通过 Google Nearby API 连接两个设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61328892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com