最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android的蓝牙实例代码中找不到REQUEST_ENABLE_BT

Bluetooth crifan 9330浏览 0评论

【背景】

折腾:

【记录】编写Android中的蓝牙模块驱动和底层HART设备

期间,参考:

http://developer.android.com/guide/topics/connectivity/bluetooth.html

的代码:

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

结果找不到其中的REQUEST_ENABLE_BT。

【解决过程】

1.参考:

Issue 25492 – android – where is REQUEST_ENABLE_BT – Android Open Source Project

说是:

BluetoothAdapter.ACTION_REQUEST_ENABLE

但是实际上BluetoothAdapter没有这个ACTION_REQUEST_ENABLE

2.参考:

Error checking if Bluetooth is enabled in Android (REQUEST_ENABLE_BT cannot be resolved to a variable) – Stack Overflow

是:

private final static int REQUEST_ENABLE_BT = 1;

即可。

3.后来看到这里:

關於藍牙程式開發問題 | 癮科技

也有解释:

那個值,自己定義就好,例如:

private static final int REQUEST_ENABLE_BT = 2;

在intent回call的時候,會丟回來給你,你可以藉此來知道回call的原由

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_ENABLE_BT)
        {   if(resultCode == RESULT_OK)
        }
    }

實際上,你可以參考sdk下sample裡的bluechat,它也是自己定義的,很多書的範例名字跟它一模一樣。

 

4.实际上后来看到官网教程中的解释了:

The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult() implementation as the requestCode parameter.

其是需要自己定义的局部常量。

 

【总结】

所以说,还是要自己认真看官网资料。

要稍微多往后面看看,如果找不到相关解释,再去找其他地方的解释。

转载请注明:在路上 » 【已解决】Android的蓝牙实例代码中找不到REQUEST_ENABLE_BT

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.170 seconds, using 22.11MB memory