一、权限和build.gradle配置并开启蓝牙(安卓蓝牙ble教程)

阳光穿透心脏的1/2处 2022-02-17 11:06 711阅读 0赞

1、AndroidManifest.xml配置

有4个基本权限需要配置,分别是:

  1. <uses-permission android:name="android.permission.BLUETOOTH"/>
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  4. <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

具体的AndroidManifest.xml配置文件如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="club.stm32">
  4. <uses-permission android:name="android.permission.BLUETOOTH"/>
  5. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  6. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  7. <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

除此之外,还需要动态申请 ACCESS_COARSE_LOCATION 定位权限

1、MainActivity.java

注:如果复制代码进项目时显示红色,请按ALT+ENTER键导包(import class)

  1. package club.stm32;
  2. import android.Manifest;
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.content.pm.PackageManager;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.content.ContextCompat;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14. public class MainActivity extends AppCompatActivity {
  15. private BluetoothAdapter bluetoothAdapter;
  16. private Button btnCheckPermission;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. getPermission(); //获取权限
  22. bluetoothInit(); //蓝牙初始化
  23. widgetInit(); //控件初始化
  24. widgetListener(); //控件监听
  25. }
  26. //获取权限
  27. private void getPermission() {
  28. //如果sdk版本大于23
  29. if (Build.VERSION.SDK_INT >=23){
  30. //如果没有权限
  31. if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED))
  32. {
  33. //动态申请权限
  34. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 10);
  35. }
  36. }
  37. }
  38. //控件初始化
  39. private void widgetInit() {
  40. //请自行提升到全局,原型是:Button startscan = findViewById(R.id.startscan);
  41. btnCheckPermission = findViewById(R.id.btnCheckPermission);
  42. }
  43. //控件监听
  44. private void widgetListener() {
  45. //测试权限按钮监听
  46. btnCheckPermission.setOnClickListener(new View.OnClickListener() {
  47. @Override
  48. public void onClick(View v) {
  49. int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
  50. if (permissionCheck == PackageManager.PERMISSION_GRANTED) {//如果有权限
  51. Toast.makeText(MainActivity.this, "hava this permission", Toast.LENGTH_SHORT).show();//toast信息
  52. Log.d("权限:","有定位权限");//在logcat上打印信息
  53. }else {
  54. getPermission();//获取权限
  55. Toast.makeText(MainActivity.this, "no this permission", Toast.LENGTH_SHORT).show();//toast信息
  56. Log.d("权限:","无定位权限");//在logcat上打印信息
  57. }
  58. }
  59. });
  60. }
  61. private void bluetoothInit() {
  62. //如果不支持蓝牙
  63. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE))
  64. {
  65. //提示不支持蓝牙
  66. Toast.makeText(this, "程序不支持该设备", Toast.LENGTH_SHORT).show();
  67. //退出程序
  68. finish();
  69. }
  70. //创建蓝牙适配器原型是BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  71. bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  72. //如果蓝牙适配器为空
  73. if (bluetoothAdapter == null)
  74. {
  75. //显示设备无蓝牙
  76. Toast.makeText(this, "设备无蓝牙", Toast.LENGTH_SHORT).show();
  77. //退出
  78. finish();
  79. }
  80. //如果蓝牙未开启
  81. if (!bluetoothAdapter.isEnabled())
  82. {
  83. //不提示,直接开启蓝牙
  84. bluetoothAdapter.enable();
  85. //提示开启蓝牙中
  86. Toast.makeText(this, "开启蓝牙中,如果未开启,请检查应用权限", Toast.LENGTH_SHORT).show();
  87. }
  88. }
  89. }

2、布局文件activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="300dp"
  12. android:text="Hello World!"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintLeft_toLeftOf="parent"
  15. app:layout_constraintRight_toRightOf="parent"
  16. app:layout_constraintTop_toTopOf="parent" />
  17. <LinearLayout
  18. android:orientation="horizontal"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content">
  21. <Button
  22. android:layout_weight="1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="测试权限"
  26. android:id="@+id/startscan"
  27. />
  28. </LinearLayout>
  29. </LinearLayout>

效果图

20190424140310351.gif

通过log.d打印的内容:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MjM0MDg3_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,711人围观)

还没有评论,来说两句吧...

相关阅读

    相关 android BLE

    直接上代码吧,完整的程序代码如下: 首先是MainActivity,本段代码完成了子模转换和保存数据的功能,并将要发送的数据整理成了和作者的小伙伴说好的格式。需要注意的是,