CompanionDeviceManager 'onDeviceFound' 回调函数未被调用
Posted
技术标签:
【中文标题】CompanionDeviceManager \'onDeviceFound\' 回调函数未被调用【英文标题】:CompanionDeviceManager 'onDeviceFound' Callback Function Not Being CalledCompanionDeviceManager 'onDeviceFound' 回调函数未被调用 【发布时间】:2020-10-31 03:52:39 【问题描述】:我们正在尝试使用 CompanionDeviceManager 类将我们的 BLE 设备与我们的 android(版本 10)手机配对,而无需位置权限。
出于测试目的,我们在测试设备周围激活了多部手机和蓝牙设备的蓝牙。
我们正在使用来自official site 的示例代码,但没有成功。
将这些代码添加到 AndroidManifest 文件中:
<uses-feature android:name="android.hardware.bluetooth"/>
<uses-feature android:name="android.software.companion_device_setup"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
MainActivity 完整代码:
public class MainActivity extends AppCompatActivity
private CompanionDeviceManager deviceManager;
private AssociationRequest pairingRequest;
private BluetoothDeviceFilter deviceFilter;
private static final int SELECT_DEVICE_REQUEST_CODE = 42;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("ArkSigner", "onCreate called.");
deviceManager = getSystemService(CompanionDeviceManager.class);
// To skip filtering based on name and supported feature flags (UUIDs),
// don't include calls to setNamePattern() and addServiceUuid(),
// respectively. This example uses Bluetooth.
deviceFilter = new BluetoothDeviceFilter.Builder()
//.setNamePattern(Pattern.compile("Test"))
//.addServiceUuid(new ParcelUuid(new UUID(0x123abcL, -1L)), null)
.build();
// The argument provided in setSingleDevice() determines whether a single
// device name or a list of device names is presented to the user as
// pairing options.
pairingRequest = new AssociationRequest.Builder()
.addDeviceFilter(deviceFilter)
//.setSingleDevice(true)
.build();
List<String> associations = deviceManager.getAssociations();
// When the app tries to pair with the Bluetooth device, show the
// appropriate pairing request dialog to the user.
deviceManager.associate(pairingRequest,
new CompanionDeviceManager.Callback()
@Override
public void onDeviceFound(IntentSender chooserLauncher)
try
Log.e("ArkSigner", "onDeviceFound called.");
startIntentSenderForResult(chooserLauncher,
SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0);
catch (IntentSender.SendIntentException e)
e.printStackTrace();
@Override
public void onFailure(CharSequence error)
Log.e("ArkSigner", "onFailure called.");
,
null);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
Log.e("ArkSigner", "onActivityResult called.");
if (requestCode == SELECT_DEVICE_REQUEST_CODE &&
resultCode == Activity.RESULT_OK)
// User has chosen to pair with the Bluetooth device.
BluetoothDevice deviceToPair =
data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
deviceToPair.createBond();
// ... Continue interacting with the paired device.
在我们运行我们的 Android 应用程序后,我们没有看到任何关于找到的设备的对话框。(蓝牙或 BLE 设备)
编辑:如果我们在 ".setNamePattern(Pattern.compile("Test"))" 行中提供设备名称或使用 ".setSingleDevice(true)" 它不会改变任何东西。
【问题讨论】:
您是否设法使用 uuid 使其工作? 你搞定了吗?我有同样的问题。 【参考方案1】:您应该改用BluetoothLeDeviceFilter
,因为根据https://developer.android.com/reference/android/companion/BluetoothDeviceFilter 的文档,BluetoothDeviceFilter
用于非 BLE 设备。
对于 BLE 设备,还期望 parcelable extra 包含 android.bluetooth.le.ScanResult 而不是 BluetoothDevice
。
【讨论】:
这有帮助,谢谢!现在卡住了,因为我不想启动选择器...【参考方案2】:我在 Android 11 中遇到了同样的问题。当我评论“.addDeviceFilter(deviceFilter)”部分时,CompanionDeviceManager 显示 Wi-Fi 网络没有问题。所以,问题只出在蓝牙设备上。
为了解决这个问题,我刚刚启用了“位置”,并且 CompanionDeviceManager 按预期显示了蓝牙设备。
也在清单中添加此权限:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
参考:https://developer.android.com/guide/topics/connectivity/bluetooth
“在 Android 10 及更高版本上运行的服务无法发现蓝牙设备,除非它们具有 ACCESS_BACKGROUND_LOCATION 权限”
【讨论】:
这很奇怪。在这种情况下,它不是扫描服务。它是设备配套应用(系统应用),因此您自己的应用不需要任何位置权限。 使用配套设备管理器的重点是避免需要位置来进行 BLE 扫描。以上是关于CompanionDeviceManager 'onDeviceFound' 回调函数未被调用的主要内容,如果未能解决你的问题,请参考以下文章
滚动数组要来回赋初值呀。。orzzzzzzzzzzzzzzzzzzzzzzzzzz