一、权限和build.gradle配置并开启蓝牙(安卓蓝牙ble教程)
1、AndroidManifest.xml配置
有4个基本权限需要配置,分别是:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
具体的AndroidManifest.xml配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="club.stm32">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
除此之外,还需要动态申请 ACCESS_COARSE_LOCATION 定位权限
1、MainActivity.java
注:如果复制代码进项目时显示红色,请按ALT+ENTER键导包(import class)
package club.stm32;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private Button btnCheckPermission;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermission(); //获取权限
bluetoothInit(); //蓝牙初始化
widgetInit(); //控件初始化
widgetListener(); //控件监听
}
//获取权限
private void getPermission() {
//如果sdk版本大于23
if (Build.VERSION.SDK_INT >=23){
//如果没有权限
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED))
{
//动态申请权限
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 10);
}
}
}
//控件初始化
private void widgetInit() {
//请自行提升到全局,原型是:Button startscan = findViewById(R.id.startscan);
btnCheckPermission = findViewById(R.id.btnCheckPermission);
}
//控件监听
private void widgetListener() {
//测试权限按钮监听
btnCheckPermission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {//如果有权限
Toast.makeText(MainActivity.this, "hava this permission", Toast.LENGTH_SHORT).show();//toast信息
Log.d("权限:","有定位权限");//在logcat上打印信息
}else {
getPermission();//获取权限
Toast.makeText(MainActivity.this, "no this permission", Toast.LENGTH_SHORT).show();//toast信息
Log.d("权限:","无定位权限");//在logcat上打印信息
}
}
});
}
private void bluetoothInit() {
//如果不支持蓝牙
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
{
//提示不支持蓝牙
Toast.makeText(this, "程序不支持该设备", Toast.LENGTH_SHORT).show();
//退出程序
finish();
}
//创建蓝牙适配器原型是BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//如果蓝牙适配器为空
if (bluetoothAdapter == null)
{
//显示设备无蓝牙
Toast.makeText(this, "设备无蓝牙", Toast.LENGTH_SHORT).show();
//退出
finish();
}
//如果蓝牙未开启
if (!bluetoothAdapter.isEnabled())
{
//不提示,直接开启蓝牙
bluetoothAdapter.enable();
//提示开启蓝牙中
Toast.makeText(this, "开启蓝牙中,如果未开启,请检查应用权限", Toast.LENGTH_SHORT).show();
}
}
}
2、布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试权限"
android:id="@+id/startscan"
/>
</LinearLayout>
</LinearLayout>
还没有评论,来说两句吧...