所以我试着制作一个连接蓝牙的应用程序,但是无论我做什么,我都不能让许可对话框出现,我尝试了安卓使用startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT)提供的方法;但是它被废弃了,所以我做了一些研究,发现我需要使用ActivityResultLauncher,但是当应用程序试图调用它时会崩溃--这里是代码
ActivityResultLauncher<Intent> startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result != null && result.getResultCode() == RESULT_OK){
if (result.getData() != null ){
Intent data = result.getData();
}
}
}
});
Inside
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter == null) {
// Device doesn't support Bluetooth
//Toast.makeText(getApplicationContext(), "Device doesn't support Bluetooth", Toast.LENGTH_SHORT).show();
} else {
//Toast.makeText(getApplicationContext(), "Device does support Bluetooth", Toast.LENGTH_SHORT).show();
}
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startForResult.launch(enableBtIntent);
//startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
PS:,我不关心老的API,因为这个应用程序只是让我通过蓝牙控制房间里的led带。
发布于 2022-08-20 00:57:37
经过大量的尝试和错误之后,它终于起作用了,我不知道它是如何工作的,因为我确信我以前使用过相同的代码,它一直在崩溃,但现在没有了,所以这里是整个MAinActivity.java
package com.example.ledcontroller;
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Permission not Granted", Toast.LENGTH_SHORT).show();
}
});
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
Intent data = result.getData();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set Bluetooth status src
ImageView BtUnchecked = findViewById(R.id.not_connected);
ImageView BtChecked = findViewById(R.id.connected);
//BtUnchecked.setImageResource(R.drawable.ic_uncheck_grey);
//BtChecked.setImageResource(R.drawable.ic_check);
//Settings button function
ImageButton sett = findViewById(R.id.settingsbtn);
sett.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
});
//Color Picker button function
ImageButton colorpick = findViewById(R.id.colorPickerbtn);
colorpick.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, ColorPickerActivity.class);
startActivity(intent);
});
//Modes button function
ImageButton modes = findViewById(R.id.modesbtn);
modes.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, ModesActivity.class);
startActivity(intent);
});
//Bluetooth button function
BluetoothManager bluetoothManager = getSystemService(BluetoothManager.class);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
ImageButton bluetooth = findViewById(R.id.bluetoothbtn);
bluetooth.setOnClickListener(view -> {
if (!bluetoothAdapter.isEnabled()){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
someActivityResultLauncher.launch(enableBtIntent);
} else {
requestPermissionLauncher.launch(Manifest.permission.BLUETOOTH_CONNECT);
}
}if (bluetoothAdapter.isEnabled()){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "if done", Toast.LENGTH_SHORT).show();
} else {
requestPermissionLauncher.launch(Manifest.permission.BLUETOOTH_CONNECT);
Toast.makeText(getApplicationContext(), "else done", Toast.LENGTH_SHORT).show();
}
}
});
}
}
如果有人知道如何使代码更好,那将是非常感谢的!
编辑:我更新了if (bluetoothAdapter.isEnabled()),以请求许可,以防您重新安装应用程序,蓝牙已经启用,以便应用程序在按下按钮时不再崩溃。
https://stackoverflow.com/questions/73419310
复制相似问题