Android的蓝牙实例代码中找不到REQUEST_ENABLE_BT

作为Android新手,最近在做关于蓝牙的项目,在参考别人的开源代码时遇到的问题,现在已经找到答案.

参考:

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.实际上后来看到官网教程中的解释了:

TheREQUEST_ENABLE_BTconstant passed tostartActivityForResult()is a locally defined integer (which must be greater than 0), that the system passes back to you in youronActivityResult()implementation as therequestCodeparameter.

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


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值