首页 > 解决方案 > 我的蓝牙应用程序无法正常工作,只有在打开我的蓝牙设置活动后才能工作

问题描述

我正在开发一个应用程序,如果蓝牙打开,它可以发现所有本地蓝牙支持的设备。但是我遇到了一个问题,如下所示:当我运行我的应用程序并等待可用设备时,它不会显示任何开启的设备,但是当我导航到蓝牙设置活动(内置电话活动)时,它当我返回时,在那里以及我的应用程序中显示所有本地可用设备。请帮我。我想通过停留在我的应用程序上来发现所有设备,并希望查看这些设备的列表。

我已获得 BLUETOOTH、BLUETOOTH ADMIN、ACCESS_FINE_LOCATION 的许可。

这是我的主要活动

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);
}}

标签: androidbluetoothconnectionandroid-bluetooth

解决方案


推荐阅读