android - 单击按钮时以编程方式关闭蓝牙可发现状态

标签 android button bluetooth discoverability

我们如何以编程方式关闭蓝牙可发现状态。

第一次点击(按钮)我可以让它被发现,第二次点击同一个按钮它不起作用

我的意思是,如果它处于可发现状态,我应该关闭它

这是我的代码

     bt_strength.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        //  if (mBtAdapter.getScanMode()!= BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
            if(!mBtAdapter.isDiscovering())
            {
                // if(bluetoothimg.getTag().toString().equalsIgnoreCase("off"))
                Log.i(TAG, "BLUETOOTH STATUS ON");
                bt_strength.setImageResource(R.drawable.bt);
                 Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 3600);
                    startActivity(discoverableIntent);  
            } 
            else if(mBtAdapter.isDiscovering()){

                bt_strength.setImageResource(R.drawable.bt_grey);
                // Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
               //     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
                //    startActivity(discoverableIntent);    
                mBtAdapter.cancelDiscovery();


            }

非常感谢任何帮助,谢谢

最佳答案

您在 if 子句中所做的是启用可发现模式 - 这意味着您的设备可以被其他蓝牙设备看到。

但是您在 else-if-clause 中所做的事情是您取消发现 - 这意味着您停止搜索其他设备。

你现在写的评论(在 else-if-clause 中)应该是最有效的解决方案,尽管并不完美,关于: Disable Bluetooth discoverable mode on Android

关于android - 单击按钮时以编程方式关闭蓝牙可发现状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11050067/

相关文章:

ios - 如何修复此错误 "Could not find module ' CzsBleSdk' for architecture 'x86_64' ;发现 : arm64, ARM “

android - AsyncTask onPreExecute 中的 ProgressDialog,但出现在 doInBackground 之后

java - 如何去除按钮周围的边框?

html - 对齐按钮内的文本/图标

android - ActivityManager : Warning: Activity not started, 它当前的任务已经被带到了前面

android - 无需扫描即可连接其他蓝牙 LE 设备?

android - java.net.UnknownHostException : graph. facebook.com 问题

android - 无法在Android中请求RECORD_AUDIO权限

php - Android Studio 尝试使用 volley 向 php 发送数据并检索 json 数组时出现错误

java - 小程序按钮填充区