蓝牙许可对话相乘

我遇到了奇怪的问题。 根据活动,我通过意向请求蓝牙激活和300秒的可发现性:

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()是一个布尔值,指示应用程序是服务器还是客户端。 如果是真的,它是服务器。 这是问题所在。

任何人都可以帮助我吗?


您要启用的代码位于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);
    }   
}

我希望你觉得这有帮助。


看看蓝牙聊天示例,这可能会有所帮助

链接地址: http://www.djcxy.com/p/26925.html

上一篇: Bluetooth Permission Dialog multiplication

下一篇: Android Custom Alert Dialog Display Error after changing the Build Version