IT TIP

블루투스가 프로그래밍 방식으로 활성화되었는지 확인하는 방법은 무엇입니까?

itqueen 2020. 11. 22. 21:02
반응형

블루투스가 프로그래밍 방식으로 활성화되었는지 확인하는 방법은 무엇입니까?


정기적으로 Android 기기에서 블루투스가 활성화되어 있는지 확인하고 싶습니다. 그렇게하기 위해 BroadcastReceiver를 사용하여 잡을 수있는 인 텐트가 있습니까, 아니면 다른 방법이 있습니까?


됐습니다.

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" />

여기에이 질문에 대한 답으로 다른 대안이 있습니다.

먼저 Manifest 파일에 다음 행을 추가하십시오.

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

이제 Bluetooth 지원 가능성을 확인하려면 다음 코드를 사용하십시오.

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

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

    }

매니페스트 파일의 권한으로 :

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

Bluetooth 상태 (켜짐 또는 꺼짐)를 프로그래밍 방식으로 확인하려면 :

    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
       }

Intent action을들을 수도 있습니다.

BluetoothAdapter.ACTION_STATE_CHANGED


사용할 수 있습니다

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

연결 확인 용

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

연결 해제 된 수표

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

이것은 @xjaphx의 답변, 약간 단순화 된 버전의 도움으로 내가 한 방법입니다.

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

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

참고URL : https://stackoverflow.com/questions/7672334/how-to-check-if-bluetooth-is-enabled-programmatically

반응형