Android Studio蓝牙BLE读特征
创始人
2024-08-17 18:30:04
0

要使用Android Studio读取蓝牙BLE特征,你可以遵循以下步骤:

  1. 确保你的设备支持BLE功能,并且已经开启了蓝牙。

  2. 在你的Android Studio项目中,添加以下依赖项到你的app级别的build.gradle文件中:

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:multidex:1.0.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'no.nordicsemi.android:ble:2.1.0'
  1. 在你的AndroidManifest.xml文件中,添加以下权限:


  1. 创建一个新的Activity或者在你的现有Activity中添加以下代码来读取BLE特征:
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.util.UUID;

public class BluetoothActivity extends AppCompatActivity {
    private static final String TAG = "BluetoothActivity";
    private static final int REQUEST_ENABLE_BT = 1;
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 2;

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothGatt mBluetoothGatt;
    private BluetoothGattCharacteristic mCharacteristic;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 检查设备是否支持蓝牙
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "设备不支持蓝牙", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 检查是否已经开启了蓝牙
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {
            checkLocationPermission();
        }
    }

    private void checkLocationPermission() {
        // 检查是否已经获取了位置权限,这是必要的权限来扫描蓝牙设备
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
        } else {
            startBluetoothDeviceScan();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_COARSE_LOCATION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startBluetoothDeviceScan();
            } else {
                Toast.makeText(this, "没有位置权限,无法扫描蓝牙设备", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    private void startBluetoothDeviceScan() {
        // 在这里开始扫描蓝牙设备
        // mBluetoothAdapter.startLeScan(mLeScanCallback);
    }

    private final BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
            Log.d(TAG, "onLeScan: " + device.getName() +

相关内容

热门资讯

一分钟快速了解!Aapoker... 一分钟快速了解!Aapoker app,小程序雀神麻将来牌技巧,荔枝竞技有辅助吗(有挂细节);暗藏猫...
新手必备!菠萝德州偷偷看功能,... 新手必备!菠萝德州偷偷看功能,雀神小程序辅助app下载,多乐跑胡子是否有外挂(有挂细节)1、多乐跑胡...
技术分享!微扑克ai机器人打德... 技术分享!微扑克ai机器人打德州,广东雀神辅助器怎么买,快玩炸翻天大小牌怎么赢(有挂工具)1、在快玩...
热门推荐!gg扑克发牌问题,微... 热门推荐!gg扑克发牌问题,微信雀神广东麻将提高胜率技巧,友聚麻将有挂吗(有挂插件)友聚麻将有挂吗辅...
推荐十款!aapoker系统机... 推荐十款!aapoker系统机制,小程序雀神广东麻将辅牌器,潮汕暗宝真的能看穿吗(有挂规律)1、构建...
六分钟了解!微扑克智能辅助器,... 六分钟了解!微扑克智能辅助器,广东雀神游戏辅助神器,广西棋牌十三张有挂出千吗(有挂方法)1、点击下载...
总算清楚!微扑克大厅都是机器人... 总算清楚!微扑克大厅都是机器人,广东雀神外挂视频,人民棋牌可以调胜率吗(有挂黑科技)1、构建自己的人...
一分钟了解!wepoke辅助技... 一分钟了解!wepoke辅助技巧,微信小程序雀神广东麻将助赢神器,佛手在线机制(有挂规律)雀神广东麻...
终于清楚!aa扑克能用模拟器,... 终于清楚!aa扑克能用模拟器,微信雀神小程序控制台,欢乐达人辅助器(有挂攻略);1、欢乐达人辅助器透...
必备教程!gg扑克实战,全民雀... 必备教程!gg扑克实战,全民雀神怎么开挂,博雅红河棋牌有没有挂(有挂方法);1、博雅红河棋牌有没有挂...