Before 2022/Android

안드로이드 블루투스 지원 여부, 현재 상태 체크 및 활성화, 비활성화

Eljoe 2018. 12. 10. 16:53
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.util.Log;

public class BluetoothService {
    private static Context mContext;

    private static BluetoothAdapter mBluetoothAdapter;

    public BluetoothService(Context context, BluetoothAdapter bluetoothAdapter){
        mContext = context;
        mBluetoothAdapter = bluetoothAdapter;
    }

    //디바이스가 블루투스를 지원하는가?
    public static boolean deviceSupport(){
        if(mBluetoothAdapter != null){
            return true;
        }
        return false;
    }

    // 블루투스가 현재 활성화 되어있는가?
    public static boolean connectState(){
        if(mBluetoothAdapter.isEnabled()){
            return true;
        }
        return false;
    }

    // 블루투스 활성화
    public static void enableBluetooth(){
        if(!mBluetoothAdapter.isEnabled()){
            mBluetoothAdapter.enable();
            Log.d("Bluetooth", "On");
        }
    }

    // 블루투스 비활성화
    public static void disableBluetooth(){
        if(mBluetoothAdapter.isEnabled()){
            mBluetoothAdapter.disable();
            Log.d("Bluetooth", "Off");
        }
    }
}