DeviceUtil.uts 2.2 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import WifiManager from 'android.net.wifi.WifiManager';
import Configuration from 'android.content.res.Configuration';
import Context from 'android.content.Context';
import LocationManager from 'android.location.LocationManager';
import BluetoothManager from 'android.bluetooth.BluetoothManager';
import PackageManager from 'android.content.pm.PackageManager';
import Manifest from 'android.Manifest';
import Build from 'android.os.Build';
import Settings from 'android.provider.Settings';
export class DeviceUtil{
	/**
	 * 设备蓝牙是否开启
	 * @param context .
	 * @return .
	 * @throws Exception 如果没开启蓝牙权限就有异常
	 */
	public static blueToothEnable(context: Context): boolean {
		if (Build.VERSION.SDK_INT >= 23 && context.checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_DENIED) {
			throw new Exception();
		}
		let bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager;
		let defaultAdapter = bluetoothManager.getAdapter();
		return defaultAdapter.isEnabled();
	}
	
	
	
	public static locationEnable(context: Context): boolean {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
			let locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager;
			if (locationManager == null) {
				return false;
			}
			return locationManager.isLocationEnabled();
		}else{
			const mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
                Settings.Secure.LOCATION_MODE_OFF);
			return (mode != Settings.Secure.LOCATION_MODE_OFF);
		}
	}
	
	public static wifiEnable(context: Context): boolean {
		let wifiManager = context.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager;
		let wifiState = wifiManager.getWifiState();
		return wifiState == WifiManager.WIFI_STATE_ENABLED;
	}
	
	
	public static deviceOrientation(context: Context): string {
		let configuration = context.getResources().getConfiguration();
		let orientation = configuration.orientation;
	
		if (orientation == Configuration.ORIENTATION_PORTRAIT) {
			return "portrait";
		} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
			return "landscape";
		}
		return "";
	}
}