检查当前的蓝牙可发现状态

时间:2013-12-04 14:28:35

标签: android bluetooth android-button android-bluetooth

所以,我一直在讨论如何检查当前的蓝牙可发现状态。我想处理简单的问题,即: 当用户通过单击按钮启动设备上的蓝牙可发现性时,它会弹出日志,并具有启用蓝牙可发现性的权限。当用户同意时,它被启用并且其他按钮被设置为启用。但是当用户拒绝它时,下一个按钮被设置为启用,前一个按钮变为不可用。

当用户拒绝设备的蓝牙可发现性时,我应该在我的代码中进行哪些更改以启用启动discovarability按钮?

是否可以将当前状态返回到某个变量?如果是这样,怎么样?

我试图对一些依赖于BTDiscoverable()的变量进行操作;方法,但没有运气。

我查看了这个Android SDK documentation,但它只是说打开它,而不是关闭也不检查当前状态。

public void onClick(View v) {
    Button bttn = (Button) v;

    if(bttn.getId() == R.id.bt_server_start)
    {
        /*
         * If BluetoothAdapter name does not start with BT_ prefix, add it to the name, then proceed
         */
        if (!mBluetoothAdapter.getName().startsWith(PREFIX))
            mBluetoothAdapter.setName(PREFIX + mBluetoothAdapter.getName());
        /*
         * Check the connectability/discoverability, compare to the statemanet made by user that it is enabled.
         * If they are not equal, run BTDiscoverable method - set device to be discoverable by other devices
         */
        if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
            BTDiscoverable();

        /*
         * As the bluetooth is now on and the device can be connected/discovered, USER MUST START THE SERVER SITE ON DEVICE BY
         * CREATING NEW THREAD
         */

        DeviceServer = new AcceptThread();

        /*
         * Start the thread
         */
        DeviceServer.start();

        /*
         * Set the availability of the button
         */

        bttn.setEnabled(false);
        ((Button) this.findViewById(R.id.bt_server_stop)).setEnabled(true);

        } else if (bttn.getId() == R.id.bt_server_stop) 
        {
            DeviceServer.cancel();
            bttn.setEnabled(false);
            ((Button) this.findViewById(R.id.bt_server_start)).setEnabled(true);
            BTDeviceName();
        }
}

编辑:嗯,再次使用这个命令会提供我期望的结果。

if(mBluetoothAdapter.getScanMode()==BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)

1 个答案:

答案 0 :(得分:2)

你使用的是库,还是BTDiscoverable()你做的一个函数?

在Android中,使用意图

设置可发现性
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);

(见http://developer.android.com/guide/topics/connectivity/bluetooth.html#EnablingDiscoverability

因此,如果我理解正确,您可以做的是:启动此意图(或使用BTDiscoverable,如果它是等效的),然后返回。该按钮保持启用状态。如果用户接受了可发现性,下次他/她将按下按钮时,代码将执行else循环并启动服务器,否则它将再次请求权限

public void onClick(View v) {
    Button bttn = (Button) v;

    if(bttn.getId() == R.id.bt_server_start)
    {
        /*
         * If BluetoothAdapter name does not start with BT_ prefix, add it to the name, then proceed
         */
        if (!mBluetoothAdapter.getName().startsWith(PREFIX))
            mBluetoothAdapter.setName(PREFIX + mBluetoothAdapter.getName());
        /*
         * Check the connectability/discoverability, compare to the statemanet made by user that it is enabled.
         * If they are not equal, run BTDiscoverable method - set device to be discoverable by other devices
         */
        if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
        {
            (If your BTDiscoverable is this equivalent if this, you can use it)
            Intent discoverableIntent = new
            Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);

        }
        else
        {
            /*
             * As the bluetooth is now on and the device can be connected/discovered, USER MUST START THE SERVER SITE ON DEVICE BY
             * CREATING NEW THREAD
             */

             DeviceServer = new AcceptThread();

             /*
             * Start the thread
             */
             DeviceServer.start();

             /*
             * Set the availability of the button
             */

             bttn.setEnabled(false);
             ((Button) this.findViewById(R.id.bt_server_stop)).setEnabled(true);
        }
    } 
    else if (bttn.getId() == R.id.bt_server_stop) 
    {
        DeviceServer.cancel();
        bttn.setEnabled(false);
        ((Button) this.findViewById(R.id.bt_server_start)).setEnabled(true);
        BTDeviceName();
    }

}

如果您确实希望在用户选择“是”时立即禁用该按钮,那么您应该尝试使用startActivityForResult(disoverableIntent)启动此意图。然后将调用onActivityResult函数(您可以在活动中覆盖它),并且您应该能够在此处看到用户是否接受了可发现性。所以你会得到类似的东西:

@override
protected void onActivityResult(int code, int result, Intent intent)
{
    if(result == 1) // Here i'm not sure how to check that the user accepted the discoverability
    /*
     * As the bluetooth is now on and the device can be connected/discovered, USER MUST START THE SERVER SITE ON DEVICE BY
     * CREATING NEW THREAD
     */

      DeviceServer = new AcceptThread();

      /*
       * Start the thread
       */
       DeviceServer.start();

      /*
       * Set the availability of the button
       */

       bttn.setEnabled(false);
       ((Button) this.findViewById(R.id.bt_server_stop)).setEnabled(true);
}