如何检查蓝牙是否以编程方式启用?

时间:2011-10-06 09:17:41

标签: android bluetooth

我想检查是否定期在任何Android设备上启用蓝牙。有没有使用BroadcastReceiver可以捕获的意图,还是有其他方法可以做到这一点?

7 个答案:

答案 0 :(得分:146)

你去了:

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
} else {
    if (!mBluetoothAdapter.isEnabled()) {
        // Bluetooth is not enable :)
    }
}

uses-permission

 <uses-permission android:name="android.permission.BLUETOOTH"  android:required="false" />

答案 1 :(得分:6)

在这里,我有其他选择作为这个问题的答案。

首先在清单文件中添加以下行。

<uses-feature android:name="android.hardware.BLUETOOTH" android:required="false"/>

现在,您要检查蓝牙支持功能,请使用以下代码。

boolean isBluetoothSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

答案 2 :(得分:6)

public boolean isBluetoothEnabled()
    {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        return mBluetoothAdapter.isEnabled();

    }

使用清单文件中的权限:

<uses-permission android:name="android.permission.BLUETOOTH" />

答案 3 :(得分:3)

以编程方式检查蓝牙状态,ON或OFF:

    BluetoothAdapter btAdapter = ((Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
           ?((BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter()
               :(BluetoothAdapter.getDefaultAdapter()));

       if(btAdapter==null){
        return;
       }
       if(btAdapter.getState()==BluetoothAdapter.STATE_ON){
            //Bluetooth is ON
       }

您也可以听取意图行动:

  

BluetoothAdapter.ACTION_STATE_CHANGED

答案 4 :(得分:2)

使用可以使用

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

用于检查bt已连接

mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED

用于检查bt已断开连接

mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_DISCONNECTED

答案 5 :(得分:1)

这是我在@ xjaphx的答案的帮助下完成的,稍微简化的版本:

 private boolean getBlueToothOn(){
    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
      return btAdapter != null && btAdapter.isEnabled();
    }

 <uses-permission android:name="android.permission.BLUETOOTH" />

答案 6 :(得分:1)

首先允许清单中的权限。

<uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

public class MainActivity extends AppCompatActivity {
    
    private BluetoothAdapter mAdapter;
    private BluetoothDevice mDevice;
    private static final int ACCESS_REQUEST_CODE=100;
    private static final int REQUEST_ENABLE_CODE=1;
    private AudioRecord mRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (mAdapter!=null && mAdapter.isEnabled()){

        }
        else {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_ENABLE_CODE){
            if (resultCode==RESULT_OK){
                println("Bluettoth Enabled");
            }
            else if (resultCode==RESULT_CANCELED){
                println("Bluetooth Not permitted");
                finish();
            }
        }
    }
}