本文主要是介绍蓝牙打开和关闭,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 蓝牙Android Api
实例化蓝牙对象
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
- 示例
public class BluetoothManager {private static BluetoothManager manager ;private BluetoothAdapter bluetoothAdapter;private BluetoothManager(){bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();}public BluetoothAdapter getBluetoothAdapter() {return bluetoothAdapter;}public static BluetoothManager newInstance(){if (null == manager){synchronized (BluetoothManager.class){if (null==manager){manager = new BluetoothManager();}}}return manager ;}/*** 是否支持蓝牙** @return*/public boolean isSupportBluetooth(){return bluetoothAdapter!=null;}/*** 蓝牙是否打开 true为打开* @return*/public boolean isBlueEnable() {return isSupportBluetooth() && bluetoothAdapter.isEnabled();}
是否支持蓝牙
/*** 是否支持蓝牙** @return*/public boolean isSupportBluetooth(){return bluetoothAdapter!=null;}
蓝牙是否打开 true为打开
public boolean isBlueEnable() {return isSupportBluetooth() && bluetoothAdapter.isEnabled();}
打开蓝牙
- 方式一
public void openBluetooth(){if (!BluetoothManager.newInstance().isBlueEnable()){BluetoothManager.newInstance().getBluetoothAdapter().enable();}}
- 方式二
public void openBluetooth1(){if (!BluetoothManager.newInstance().isBlueEnable()){Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);activity.startActivityForResult(intent, 100);}}
关闭蓝牙
public void closeBluetooth(){if (BluetoothManager.newInstance().isBlueEnable()){BluetoothManager.newInstance().getBluetoothAdapter().disable();}}
这篇关于蓝牙打开和关闭的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!