首页 > 解决方案 > 扫描发现在 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);
    }

标签: javaandroid

解决方案


它没有被发现,因为它根据 bt 协议隐藏但是请参阅https://stackoverflow.com/a/36984988/8461344


推荐阅读