作为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.实际上后来看到官网教程中的解释了:
The |
其是需要自己定义的局部常量。