如何申请的Android用户通过点击启用蓝牙? [英] How to request Android user to enable Bluetooth through a click?
问题描述
http://developer.android.com/guide/topics/connectivity /bluetooth.html 我知道,我需要以下要求用户启用了BT:
如果(!mBluetoothAdapter.isEnabled())
{
意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
但问题是如何在一个类中使用它?为什么我的code碰撞时点击该活动按钮:
公共类反对者扩展活动
{
私人最终静态INT REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); @覆盖
保护无效的onCreate(捆绑savedInstancesState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.opponents); 最终按钮按钮1 =(按钮)findViewById(R.id.button1);
button1.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(查看视图)
{
如果(!mBluetoothAdapter.isEnabled())
{
意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
}
}
});
}
你设置你的的AndroidManifest.xml
文件适当的权限?
可以肯定,你将需要蓝牙
的权限。
<清单...>
<使用许可权的android:NAME =android.permission.BLUETOOTH/>
...
< /清单>
此外,文件说:
如果您希望您的应用程序来启动设备发现或操纵
蓝牙设置,您还必须声明BLUETOOTH_ADMIN
许可。
块引用>如果您希望启用这些功能,您将需要以下code之一:
<清单...>
<使用许可权的android:NAME =android.permission.BLUETOOTH/>
<使用许可权的android:NAME =android.permission.BLUETOOTH_ADMIN/>
...
< /清单>from http://developer.android.com/guide/topics/connectivity/bluetooth.html i know that i need to the following to request the user to enable his BT:
if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); }
but the question is how to use it in a class? why does my code crash whenever click on the button of this activity:
public class Opponents extends Activity { private final static int REQUEST_ENABLE_BT=1; BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); @Override protected void onCreate(Bundle savedInstancesState) { super.onCreate(savedInstanceState); setContentView(R.layout.opponents); final Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if(!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } } }); }
解决方案Did you set proper permissions in your
AndroidManifest.xml
file? For sure, you will needBLUETOOTH
permission.<manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> ... </manifest>
In addition, as documentation says:
If you want your app to initiate device discovery or manipulate Bluetooth settings, you must also declare the
BLUETOOTH_ADMIN
permission.If you want to enable one of these features you will need the following code:
<manifest ... > <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> ... </manifest>
这篇关于如何申请的Android用户通过点击启用蓝牙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!