在我的应用程序中,我需要在按钮单击时打开我的设备的蓝牙.我怎样才能做到这一点?一个例子将非常有用.另外,我需要在mainfest.xml中包含哪些权限?
以下是关于蓝牙的Android文档的代码摘录
在权限清单文件中:
<manifest ... >
<uses-permission android:name="android.permission.BLUETOOTH" />
...
</manifest>
Run Code Online (Sandbox Code Playgroud)
启用蓝牙的源代码
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);
}
Run Code Online (Sandbox Code Playgroud)
如果启用蓝牙成功,您Activity
将RESULT_OK
在onActivityResult()
回调中收到结果代码.如果由于错误而未启用蓝牙(或用户回答"否"),则结果代码将为RESULT_CANCELED
.
在Android中打开蓝牙并不困难.但是你必须注意一些细节.在Android中有三种打开蓝牙的方法.
1.强行打开蓝牙.
为了获得默认的蓝牙适配器,即使用BluetoothAdapter.getDefaultAdapter()
; 您需要此权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
Run Code Online (Sandbox Code Playgroud)
为了强制打开蓝牙,即使用BluetoothAdapter.enable()
; 您需要此权限:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Run Code Online (Sandbox Code Playgroud)
这是一个代码示例
/**
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
上面打开蓝牙的方法不会告诉用户你是否打开蓝牙成功.当你调用方法启用()时,你不会100%打开蓝牙.因为某些安全应用程序拒绝你的原因这样做等等.你需要注意方法enable()的返回值.
方法enable()的官方api告诉我们:
未经用户同意,不得启用蓝牙.如果要打开蓝牙以创建无线连接,则应使用ACTION_REQUEST_ENABLE Intent,这将引发一个请求用户打开蓝牙权限的对话框.enable()方法仅适用于包含用于更改系统设置的用户界面的应用程序,例如"power manager"应用程序.
因此,除非您让用户知道您将做什么,否则不适合您通过方法enable()打开蓝牙.
2.使用系统警报提醒用户您将通过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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是在Android上打开蓝牙的更好方法.
3.指导用户使用系统蓝牙设置自行打开蓝牙.
// Guide users to system Bluetooth settings to turn on Bluetooth by himselves.
this.startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
Run Code Online (Sandbox Code Playgroud)
在我的app.Conside代码逻辑和用户体验,我使用以下步骤打开蓝牙:
1.使用enable()打开蓝牙.但是我会告诉用户我会打开蓝牙,如果他们允许我这样做,我会调用enable()来打开蓝牙.这比调用系统要好提醒,因为我们可以控制内容来提醒用户.您必须注意,通过启用(),某些安全应用程序或其他原因拒绝您执行此操作不是100%打开蓝牙.但我们可以通过方法的返回值估计我们是否打开蓝牙成功使能().
2.如果方法enable()返回false,那么我们使用startActivityForResult来提醒用户我们将打开蓝牙.
3.如果步骤2由于某些原因不起作用,您可以引导用户进入系统蓝牙设置以自行打开蓝牙.
添加更多:自Android 6.0以来我们需要处理蓝牙的许可.