android - 我的蓝牙应用程序无法正常工作,只有在打开我的蓝牙设置活动后才能工作
问题描述
我正在开发一个应用程序,如果蓝牙打开,它可以发现所有本地蓝牙支持的设备。但是我遇到了一个问题,如下所示:当我运行我的应用程序并等待可用设备时,它不会显示任何开启的设备,但是当我导航到蓝牙设置活动(内置电话活动)时,它当我返回时,在那里以及我的应用程序中显示所有本地可用设备。请帮我。我想通过停留在我的应用程序上来发现所有设备,并希望查看这些设备的列表。
我已获得 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);
}}
解决方案
推荐阅读
- node.js - 你如何解析一个标记为零的nodejs条目?
- javascript - JavaFX Webview:Javascript Bridge 在第二个页面加载时延迟
- javascript - 无法从“date-fns”模拟 startOfToday
- php - SQLSTATE[HY000] [2002] 在带有 XAMPP 的 Mac OS Catalina 上连接被拒绝,在 StackOverflow 上尝试了大多数解决方案
- ruby-on-rails - 使用 Axios 在 ReactJS 中注册的 simple_token_authentication - Rails API
- haskell - 内存使用异常(内存泄漏?)
- php - 如何在 PHP 中通过 SOAP API 发送短信
- javascript - Javascript Codility 最大切片问题
- python - Tensorflow 无法在我的 Raspberry Pi 4 上下载并不断收到相同的错误消息
- laravel - 如何获取除最后一个查询之外的所有数据?