【Android】请求打开蓝牙和定位功能
有时用户打开app后,无法接收到蓝牙广播,却不知是因为蓝牙和定位未开启,故需要在app中增加判断提示。
请求打开定位服务
private fun checkLocationSetting() {if (!LocationSetting.isEnabled(this)) {runOnUiThread {LocationSetting.askToActivate(this)}}}
import android.app.AlertDialog
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.Intent
import android.location.LocationManager
import android.os.Build
import android.provider.Settings
import android.provider.Settings.SettingNotFoundException
import android.util.Logobject LocationSetting {private val TAG: String = LocationSetting::class.java.simpleNamefun isEnabled(context: Context): Boolean {var enabled = falsetry {val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManagerenabled = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {locationManager.isLocationEnabled} else {Settings.Secure.getInt(context.contentResolver,Settings.Secure.LOCATION_MODE) != Settings.Secure.LOCATION_MODE_OFF}} catch (e: SettingNotFoundException) {Log.e(TAG, "Cannot check GPS status: " + e.message, e)}return enabled}fun askToActivate(context: Context) {AlertDialog.Builder(context).setTitle("GPS activation").setMessage("From Android 6, GPS needs to be enabled to find nearby BLE devices.").setCancelable(false).setPositiveButton("Fix") { _, _ ->val onGPS = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)context.startActivity(onGPS)}.show()}
}
请求打开蓝牙功能
var bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled) {// Method1
// bluetoothAdapter.enable()
// Method2val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)startActivity(enableBtIntent)
}
【Android】请求打开蓝牙和定位功能相关推荐
- Android项目集成高德地图定位功能
1.在Project的build.gradle文件中配置repositories,添加maven或jcenter仓库地址 Android Studio默认会在Project的build.gradle为 ...
- 微信小程序拒绝定位之后 如何再次开启以及判断是否打开了系统定位功能
Taro.getSystemInfo({success: function (res) {if (!res.locationEnabled || !res.locationAuthorized) {T ...
- android设置打开蓝牙时报错:java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission
我的手机是三星s10,Android12,遇到的这个问题,供参考 问题出现场景,我是最近在研究一下蓝牙相关的,找了文档学习但是触动mBluetoothAdapter.enable();开启或者打开蓝牙 ...
- Android 显示打开蓝牙方法(个人学习笔记)
创建的新项目需要用到蓝牙,在网络上参考了一堆方法但是已经不适用与新版Android 在使用bluetoothAdapter.enable();强制打开蓝牙或者利用Intent显示打开蓝牙时,在Andr ...
- android 10.0关闭gps定位功能
1.概述 在10.0的产品定制化开发中,有些是不需要gps定位功能的,所以就需要屏蔽掉gps上报定位数据的功能,而在framework层中是通过/frameworks/base/location/ja ...
- android 打开蓝牙语音拨号功能
修改mediatek/external/bluetooth/bt_cust/bt_cust_table.h文件,将HFPVoiceRecognition属性的值改为1. { . ...
- 如何打开计算机浏览器定位功能吗,如何在360浏览器中设置允许所有网站跟踪我的地理位置...
如何在360浏览器中设置允许所有网站跟踪我的地理位置 腾讯视频/爱奇艺/优酷/外卖 充值4折起 我们在使用360浏览器的时候,想要设置允许所有的网站跟踪我的位置,该怎么操作呢?今天就跟大家介绍一下如何 ...
- Android定位功能(二)
在前文Android定位功能(一)中,已经大致介绍了一下在Android平台中,和定位功能相关的类,并举例获取了位置信息.但是前文是基于Criteria定制了一个标准,通过getBestProvide ...
- Android 实现蓝牙打印的功能
第一步:首先需要一个蓝牙打印工具类 import android.bluetooth.BluetoothSocket; import android.graphics.Bitmap; import a ...
最新文章
- Zencart获取PayPal PDT Token参数教程方法
- 使用ASP.NET 2.0提供的WebResource管理资源
- 百万数据报表:分析以及解决办法
- UVA1374 Power Calcilus快速幂计算
- 第三章计算机网络,第三章-计算机网络及应用.ppt
- Linux各发行版本 优缺点 简介
- IIS故障问题(Connections_Refused)分析及处理【转】
- div 一段时间后自动隐藏
- 迷你迅雷+SqlServer2008r2下载
- GCJ-02转WGS84
- 计算机应用 行动计划范文,制定计算机学习计划范文3篇0001.docx
- uni-app时间格式转换
- 【Python】Python-OpenCV实时处理视频
- Filter Control for Visual Studio LightSwitch 本地化处理
- Easy-Pay一行代码解决支付宝微信支付功能
- QQ浏览器X5内核问题汇总 转
- 网址后面的:2328是什么意思?
- OpenGL 3.0,等得花儿都谢了
- 信息与计算机工程学院英文翻译,合肥工业大学各学院、专业名称及其英文翻译...
- 3种简单又好看的·按钮效果
热门文章
- Go | 字符串拼接方式总结和分析
- docker启动失败原因
- 黑马编程开发各种免费资源-安排一下
- LSTM对比Bi-LSTM的电力负荷时间序列预测(Matlab)
- 计算机科学引论2021英文,计算机科学引论= Computing Essentials : 英文
- 24 直面配分函数Confronting Partition Function
- 讲座回顾|2021/4/7|青源美团|CVPR 2021 预讲 · 美团专场,覆盖实例分割,图像分割,表情识别,特征选择和对齐...
- 计算机组成原理-机器数、浮点数
- Java反射的基本理解
- mysql手机号以135开头_135开头的手机号属于联通还是移动?