java - 扫描发现在 android 应用程序中不起作用
问题描述
仅当其他设备在其“蓝牙设置屏幕”上时,我才能在扫描发现时列出未配对的设备。如果其他设备在其主页上,则我的应用程序无法找到这些设备。我的手机上有 android 7,其他设备上有 android 9。任何帮助表示赞赏。谢谢:)
public void startDiscovery()
{
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
BA.startDiscovery();
registerReceiver(mReceiver, filter);
Toast.makeText(getApplicationContext(), "Discovery started", Toast.LENGTH_SHORT).show();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("main","action found"+action);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Toast.makeText(getApplicationContext(), "Discovery started", Toast.LENGTH_SHORT).show();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Toast.makeText(getApplicationContext(), "Discovery finished", Toast.LENGTH_SHORT).show();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
Log.d("main","devicename"+deviceName);
Log.d("main","deviceHardwareAddress"+deviceHardwareAddress);
}
};
@Override
protected void onDestroy() {
Log.d("main","destroyed");
super.onDestroy();
BA.cancelDiscovery();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(mReceiver);
}
解决方案
它没有被发现,因为它根据 bt 协议隐藏但是请参阅https://stackoverflow.com/a/36984988/8461344
推荐阅读
- python - 在 Pandas 中结合工作日和小时
- r - 如何将大圆距离编码为 glmmtmb 混合效果模型?
- python-3.x - 简化具有两个变量的行的链式比较
- database - cassandra 如何处理 QUORUM 读取之间的写入时间戳冲突?
- html - 如何让 div 在浏览器中占据 100% 的视口高度
- python-3.x - 创建一列边
- linux - 如何让 git/java 在 ISO-8859-1 Linux 系统上正确处理 UTF-8 文件名?
- nginx - 如何阻止 nginx 代理服务器中的某些路由
- python - 使用 Counter() 后如何计算元组中的特定元素 - Python
- r - 使用 for 循环重新采样多个栅格