【发布时间】:2011-07-01 09:27:21
【问题描述】:
在我的应用程序中,我需要通过单击按钮打开设备的蓝牙。我怎样才能做到这一点?一个例子将非常有帮助。另外,我需要在我的 mainfest.xml 中包含哪些权限?
【问题讨论】:
在我的应用程序中,我需要通过单击按钮打开设备的蓝牙。我怎样才能做到这一点?一个例子将非常有帮助。另外,我需要在我的 mainfest.xml 中包含哪些权限?
【问题讨论】:
以下是android documentation on Bluetooth的代码摘录
在清单文件中获取权限:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
开启蓝牙的源码
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
如果启用蓝牙成功,您的Activity 将在onActivityResult() 回调中收到RESULT_OK 结果代码。如果由于错误(或用户回答“否”)未启用蓝牙,则结果代码将为RESULT_CANCELED。
【讨论】:
BLUETOOTH_ADMIN 权限; 2) 在 Android SDK 文档中强烈建议不要使用 enable(),除非您实现自己的 UI 以征得用户同意,或者如果您正在实现系统设置管理器应用或类似应用。
在Android中开启蓝牙并不难。但是你必须注意一些细节。在Android中打开蓝牙有3种方法。
1.强制打开蓝牙。
为了得到默认的蓝牙适配器,即使用BluetoothAdapter.getDefaultAdapter();您需要此权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
为了强制打开蓝牙,即使用BluetoothAdapter.enable();您需要此权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
这是一个代码示例
/**
* Bluetooth Manager
*
* @author ifeegoo www.ifeegoo.com
*
*/
public class BluetoothManager
{
/**
* Whether current Android device support Bluetooth.
*
* @return true:Support Bluetooth false:not support Bluetooth
*/
public static boolean isBluetoothSupported()
{
return BluetoothAdapter.getDefaultAdapter() != null ? true : false;
}
/**
* Whether current Android device Bluetooth is enabled.
*
* @return true:Bluetooth is enabled false:Bluetooth not enabled
*/
public static boolean isBluetoothEnabled()
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (bluetoothAdapter != null)
{
return bluetoothAdapter.isEnabled();
}
return false;
}
/**
* Force to turn on Bluetooth on Android device.
*
* @return true:force to turn on Bluetooth success
* false:force to turn on Bluetooth failure
*/
public static boolean turnOnBluetooth()
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (bluetoothAdapter != null)
{
return bluetoothAdapter.enable();
}
return false;
}
}
上述开启蓝牙的方法不会告诉用户你开启蓝牙成功与否。当你调用方法enable()时,你不会100%开启蓝牙。由于某些安全应用的原因拒绝你这样做等等。你需要注意方法enable()的返回值。
enable()方法的官方api告诉我们:
未经用户直接同意,切勿启用蓝牙。如果要打开蓝牙以创建无线连接,则应使用 ACTION_REQUEST_ENABLE Intent,这将引发一个对话框,请求用户允许打开蓝牙。 enable() 方法仅适用于包含用于更改系统设置的用户界面的应用程序,例如“电源管理器”应用程序。
所以不适合你通过 enable() 方法开启蓝牙,除非你让用户知道你会做什么。
2.使用 System Alert 提醒用户你将通过 startActivityForResult 开启蓝牙。
this.startActivityForResult(requestBluetoothOn, REQUEST_CODE_BLUETOOTH_ON)
need this permission:
<uses-permission android:name="android.permission.BLUETOOTH" />
public class MainActivity extends Activity
{
/**
* Custom integer value code for request to turn on Bluetooth,it's equal
*requestCode in onActivityResult.
*/
private static final int REQUEST_CODE_BLUETOOTH_ON = 1313;
/**
* Bluetooth device discovery time,second。
*/
private static final int BLUETOOTH_DISCOVERABLE_DURATION = 250;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
if ((BluetoothManager.isBluetoothSupported())
&& (!BluetoothManager.isBluetoothEnabled()))
{
this.turnOnBluetooth();
}
}
/**
* Use system alert to remind user that the app will turn on Bluetooth
*/
private void turnOnBluetooth()
{
// request to turn on Bluetooth
Intent requestBluetoothOn = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
// Set the Bluetooth discoverable.
requestBluetoothOn
.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
// Set the Bluetooth discoverable time.
requestBluetoothOn.putExtra(
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
BLUETOOTH_DISCOVERABLE_DURATION);
// request to turn on Bluetooth
this.startActivityForResult(requestBluetoothOn,
REQUEST_CODE_BLUETOOTH_ON);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE_BLUETOOTH_ON)
{
switch (resultCode)
{
// When the user press OK button.
case Activity.RESULT_OK:
{
// TODO
}
break;
// When the user press cancel button or back button.
case Activity.RESULT_CANCELED:
{
// TODO
}
break;
default:
break;
}
}
}
}
这是您在 Android 上打开蓝牙的更好方法。
3.引导用户进入系统蓝牙设置自行开启蓝牙。
// Guide users to system Bluetooth settings to turn on Bluetooth by himselves.
this.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
在我的应用程序中。考虑到代码逻辑和用户体验,我使用以下步骤打开蓝牙:
1.使用enable()打开蓝牙。但是我会告诉用户我会打开蓝牙,如果他们允许我这样做,我会调用enable()打开蓝牙。这比调用系统提醒,因为我们可以控制提醒用户的内容。 需要注意的是 enable() 并不是100%开启蓝牙,某些安全应用或者其他原因拒绝你这样做。但是我们可以通过方法的返回值来估计我们是否开启蓝牙成功启用()。
2.如果enable()方法返回false,那么我们使用startActivityForResult来提醒用户我们将开启蓝牙。
3.如果由于某些原因第2步不起作用,您可以引导用户进入系统蓝牙设置自行开启蓝牙。
补充:从Android 6.0开始我们需要处理蓝牙的权限。
【讨论】:
此解决方案将帮助您Android Bluetooth。但有关 Android 蓝牙使用的详细信息以及 manifest.xml,您需要查看此Android Bluetooth。
您需要的许可。
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
【讨论】: