eclipse 打开和关闭蓝牙?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5735053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toggling Bluetooth on and off?
提问by Christian
Is there a simple tutorial or dose anyone have code to toggle Bluetooth on and off using a Toggle-button in eclipse building for android?
是否有一个简单的教程或任何人都有代码可以在 Eclipse 构建中使用 Toggle-button 打开和关闭蓝牙?
If anyone can help that will be greatly appreciated.
如果有人可以提供帮助,将不胜感激。
-Thanks in advance.
-提前致谢。
回答by Ben Williams
You'll need
你需要
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
in your manifest file, and variables like:
在您的清单文件和变量中,例如:
private final integer REQUEST_ENABLE_BT = 1;
and
和
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean hasBluetooth = (mBluetoothAdapter == null);
so that in your OnCreate you can do something like:
以便在您的 OnCreate 中您可以执行以下操作:
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
togglebutton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks
if (togglebutton.isChecked())
{
if (hasBluetooth && !mBluetoothAdapter.isEnabled())
{
// prompt the user to turn BlueTooth on
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
else
{
if (hasBluetooth && mBluetoothAdapter.isEnabled())
{
// you should really prompt the user for permission to turn
// the BlueTooth off as well, e.g., with a Dialog
boolean isDisabling = mBluetoothAdapter.disable();
if (!isDisabling)
{
// an immediate error occurred - perhaps the bluetooth is already off?
}
}
}
}
});
where the user response to the "turn bluetooth on" prompt is caught in
用户对“打开蓝牙”提示的响应被捕获
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == RESULT_OK))
{
boolean isEnabling = mBluetoothAdapter.enable();
if (!isEnabling)
{
// an immediate error occurred - perhaps the bluetooth is already on?
}
else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON)
{
// the system, in the background, is trying to turn the Bluetooth on
// while your activity carries on going without waiting for it to finish;
// of course, you could listen for it to finish yourself - eg, using a
// ProgressDialog that checked mBluetoothAdapter.getState() every x
// milliseconds and reported when it became STATE_ON (or STATE_OFF, if the
// system failed to start the Bluetooth.)
}
}
}
回答by Abhilash
Take a look at http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
看看http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter != null) {
if(adapter.getState() == BluetoothAdapter.STATE_ON) {
adapter.disable();
} else if (adapter.getState() == BluetoothAdapter.STATE_OFF){
adapter.enable();
} else {
//State.INTERMEDIATE_STATE;
}
}
回答by Joolah
try this
尝试这个
public void disableBluetooth(Context context, Boolean bool) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(bool)
wifiManager.setWifiEnabled(false);
else
wifiManager.setWifiEnabled(true);
}
回答by Johnny
BluetoothAdapter mBluetooth = BluetoothAdapter.getDefaultAdapter();
Integer bluetooth = 1; // Turn on
Object nada = (bluetooth == 1 ? mBluetooth.enable() : mBluetooth.disable());
Manifest:
显现:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
回答by Siddhpura Amit
download this example this will help you
下载这个例子这将帮助你