也就是几行代码的事
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
activity: private BluetoothAdapter mBluetoothAdapter; /** * 监测蓝牙是否打开的状态 * 一般来说是在mainactivity里调用。 */ private void monitor() { mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { // Device does not support Bluetooth Toast.makeText(MainActivity.this, "该设备没有蓝牙", Toast.LENGTH_SHORT).show(); return; } if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 10); } }
如果设备没有打开蓝牙,会有提示,点击允许即可