在我的应用程序中,我需要在按钮点击时打开我的设备的蓝牙。我该如何实现?一个例子将非常有帮助。另外,我需要在我的mainfest.xml中包含哪些权限?
<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
。enable()
除非您实现了自己的用户同意UI或者您正在实现系统设置管理器应用程序或类似应用。 - JHH在安卓系统中打开蓝牙并不难,但你需要注意一些细节。有三种方法可以打开安卓系统的蓝牙。
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;
}
}
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));