提交 c9126d29 编写于 作者: DCloud-yyl's avatar DCloud-yyl

开源:uni.getSystemSetting

上级 78c7d4d5
{
"id": "uni-getSystemSetting",
"displayName": "uni-getSystemSetting",
"version": "1.0.0",
"description": "uni-getSystemSetting",
"keywords": [
"uni-getSystemSetting"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"uni-ext-api": {
"uni": {
"getSystemSetting": {
"name": "getSystemSetting",
"app": {
"js": false,
"kotlin": true,
"swift": true
}
}
}
},
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uni-getSystemSetting
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS 原生插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts/-/tree/dev)
\ No newline at end of file
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 "";
}
}
\ No newline at end of file
import { UTSAndroid } from "io.dcloud.uts";
import { DeviceUtil } from './device/DeviceUtil.uts';
import { GetSystemSetting, GetSystemSettingResult } from '../interface.uts'
import Exception from 'java.lang.Exception';
export const getSystemSetting : GetSystemSetting = () : GetSystemSettingResult => {
let context = UTSAndroid.getAppContext();
let result : GetSystemSettingResult = {
deviceOrientation : DeviceUtil.deviceOrientation(context!),
locationEnabled : DeviceUtil.locationEnable(context!),
};
try {
let blueToothEnable = DeviceUtil.blueToothEnable(context!);
result.bluetoothEnabled = blueToothEnable;
} catch (e : Exception) {
result.bluetoothError = "Missing permissions required by BluetoothAdapter.isEnabled: android.permission.BLUETOOTH";
}
try {
result.wifiEnabled = DeviceUtil.wifiEnable(context!);
} catch (e : Exception) {
result.wifiError = "Missing permissions required by WifiManager.isWifiEnabled: android.permission.ACCESS_WIFI_STATE";
}
return result;
}
\ No newline at end of file
import { GetSystemSetting, GetSystemSettingResult } from '../interface.uts'
import { UTSiOS } from "DCloudUTSFoundation";
export const getSystemSetting : GetSystemSetting = () : GetSystemSettingResult => {
let setting : Map<string, any> = UTSiOS.getSystemSetting();
let result : GetSystemSettingResult = {
deviceOrientation: "portrait",
locationEnabled : false
};
if (setting.has("bluetoothEnabled")) {
result.bluetoothEnabled = setting.get("bluetoothEnabled") as boolean;
}
if (setting.has("bluetoothError")) {
result.bluetoothError = setting.get("bluetoothError") as string;
}
if (setting.has("locationEnabled")) {
result.locationEnabled = setting.get("locationEnabled") as boolean;
}
if (setting.has("wifiEnabled")) {
result.wifiEnabled = setting.get("wifiEnabled") as boolean;
}
if (setting.has("deviceOrientation")) {
result.deviceOrientation = setting.get("deviceOrientation") as string;
}
return result;
}
\ No newline at end of file
export type GetSystemSettingResult = {
/**
* 蓝牙是否开启
*/
bluetoothEnabled?: boolean,
/**
* 蓝牙的报错信息
*/
bluetoothError?: string,
/**
* 位置是否开启
*/
locationEnabled : boolean,
/**
* wifi是否开启
*/
wifiEnabled?: boolean,
/**
* wifi的报错信息
*/
wifiError?: string,
/**
* 设备方向
*/
deviceOrientation : string
}
export type GetSystemSetting = () => GetSystemSettingResult
export interface Uni {
/**
* GetSystemSetting()
* @description
* 获取系统设置
* @return {object}
* @tutorial https://uniapp.dcloud.net.cn/api/system/getsystemsetting.html
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4",
* "uniVer": "√",
* "unixVer": "3.9+"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "√",
* "unixVer": "3.9+"
* }
* }
* }
* @example
```typescript
uni.getSystemSetting()
```
*/
getSystemSetting(): GetSystemSettingResult;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册