[英]Bluetooth Permission Dialog multiplication
我遇到了奇怪的問題。 在“活動”中,我通過意圖請求藍牙激活和300s可發現性:
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
無論藍牙是否已激活,我都可以使用它。 這樣,對話框權限就會像預期的那樣顯示出來:
“藍牙許可請求:您手機上的應用程序正在請求許可以打開藍牙並...”
但是,無論我是否同意,對話框始終會多次出現。 我不知道為什么。 我的代碼:
public class Initial extends Activity {
private Integer REQUEST_ENABLE_BT = 1;
private Integer REQUEST_ENABLE_DISCBT = 2;
private ListView listView;
private ArrayAdapter<String> mArrayAdapter;
TextView editText;
private Global global;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_initial);
this.global= ((Global)this.getApplicationContext());
editText = (TextView) findViewById(R.id.textView1);
global.setAdapter(BluetoothAdapter.getDefaultAdapter());
if (global.getAdapter() == null) {
// Device does not support Bluetooth
editText.setText("Erro: Sistema não suporta Bluetooth!");
finish();
}
listView = (ListView) findViewById(R.id.list);
mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(mArrayAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.initial, menu);
return true;
}
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
if(!global.getEstado()){
if (!global.getAdapter().isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else
editText.setText("Bluetooth Ligado!");
global.setReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
Thread novocliente = new novoCliente(device);
novocliente.start();
}
}
});
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(global.getReceiver(), filter); // Don't forget to unregister during onDestroy
global.getAdapter().startDiscovery();
}
else{
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ENABLE_BT){
switch(resultCode){
case RESULT_OK: editText.setText("Bluetooth Ligado!");break;
case RESULT_CANCELED: editText.setText("Impossivel ligar Bluetooth!");finish();break;
}
}
if(requestCode == REQUEST_ENABLE_DISCBT){
switch(resultCode){
case RESULT_CANCELED: editText.setText("Bluetooth não Detetável!");break;
default : editText.setText("Bluetooth Detetável por "+resultCode+" segundos!");
Thread servidor = new ServerSocket();
servidor.start();
break;
}
}
}
@Override
public void onStop() {
super.onStop(); // Always call the superclass method first
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
BluetoothAdapter.getDefaultAdapter().disable();
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass method first
unregisterReceiver(global.getReceiver()); // Don't forget to unregister during onDestroy
}
}我的可變getEstado()是一個布爾值,指示應用程序是服務器還是客戶端。 如果為true,則為服務器。 問題就在其他方面。
誰能幫我嗎?
您要啟用的代碼在onResume()中。 顯示對話框時,您的活動被暫停,而被取消(肯定或否定)時,您的活動將恢復。 如果對話框是肯定的,則也將需要一點時間來啟用BT適配器,因此global.getAdapter()。isEnabled()仍將返回false。 這就是為什么您要反復顯示對話框的原因。
要解決此問題,您需要使用其他觸發器(為什么要專門使用onResume?您可以將其放在onCreate中),或者可以使用存儲狀態。 就像是:
private boolean requestedEnable = false;
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
if(!global.getEstado()){
if (!global.getAdapter().isEnabled()) {
if(!requestedEnable){
requestedEnable = true;
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
else
editText.setText("Bluetooth Ligado!");
global.setReceiver(new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
Thread novocliente = new novoCliente(device);
novocliente.start();
}
}
});
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(global.getReceiver(), filter); // Don't forget to unregister during onDestroy
global.getAdapter().startDiscovery();
}
else{
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
我希望你覺得這有幫助。
看一下藍牙聊天示例,可能會有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.