Android 我的蓝牙应用程序无法正常工作,只能在打开蓝牙设置活动后才能工作

Android 我的蓝牙应用程序无法正常工作,只能在打开蓝牙设置活动后才能工作,android,bluetooth,connection,android-bluetooth,Android,Bluetooth,Connection,Android Bluetooth,我正在开发一个应用程序,可以发现所有本地蓝牙支持的设备,如果蓝牙是开着的。但我遇到了一个问题,问题如下: 当我运行应用程序并等待可用设备时,它不会显示任何已打开的设备,但当我导航到蓝牙设置活动(内置手机活动)时 然后,当我返回时,它会显示所有本地可用设备以及我的应用程序中的设备。请帮帮我。我想在我的应用程序上查找所有设备,并想查看这些设备的列表 我已获得蓝牙、蓝牙管理员的许可,可以访问您的位置 这是我的主要活动 public class MainActivity extends AppCompa

我正在开发一个应用程序,可以发现所有本地蓝牙支持的设备,如果蓝牙是开着的。但我遇到了一个问题,问题如下: 当我运行应用程序并等待可用设备时,它不会显示任何已打开的设备,但当我导航到蓝牙设置活动(内置手机活动)时 然后,当我返回时,它会显示所有本地可用设备以及我的应用程序中的设备。请帮帮我。我想在我的应用程序上查找所有设备,并想查看这些设备的列表

我已获得蓝牙、蓝牙管理员的许可,可以访问您的位置

这是我的主要活动

public class MainActivity extends AppCompatActivity {

private static final String TAG = "bluetooth";
private BluetoothAdapter bluetoothAdapter;
private Button mBTEnableButton;
private BroadcastReceiver mReceiver;
private TextView mShowDevices;
private ProgressBar mProgressBar;

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

    mBTEnableButton = findViewById(R.id.enable_bt_button);
    mShowDevices = findViewById(R.id.show_connected_devices);
    mProgressBar = findViewById(R.id.progressbar);


    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    mReceiver = new BlueToothReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, intentFilter);


    mProgressBar.setVisibility(View.INVISIBLE);

    if (bluetoothAdapter == null) {
        Toast.makeText(this, "Device does not support bluetooth", Toast.LENGTH_SHORT).show();
    }

    mBTEnableButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!bluetoothAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

                startActivityForResult(intent, 1000);

                bluetoothAdapter.startDiscovery();

            }
        }
    });


}

private class BlueToothReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("bluetooth", "onReceive");
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            String deviceAddress = device.getAddress();
            mShowDevices.setText("action : " + action + "\n device name: " + deviceName + "\ndevice address : " + deviceAddress);
            Log.d("bluetooth", "action : " + action + "\n device name: " + deviceName + "\ndevice address : " + deviceAddress);
        } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            mProgressBar.setVisibility(View.VISIBLE);
            Log.d(TAG, "discovery start: ");
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            mProgressBar.setVisibility(View.INVISIBLE);
            Log.d(TAG, "discovery finish: ");
        }

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);
}}

此链接可能有助于您在单击按钮时调用startDiscovery()方法。您是否尝试过点击按钮?编写代码以使您的设备可被发现。其他人无法发现您的设备。打开BT设置可以根据android行为发现它