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() +

相关内容

热门资讯

安装Pillow时遇到了问题:... 遇到这个问题,可能是因为缺少libwebpmux3软件包。解决方法是手动安装libwebpmux3软...
安装React Native时... 当安装React Native时出现构建错误的情况,可以尝试以下解决方法:确保已经安装了最新版本的C...
安装Python库"... 安装Python库"firedrake"的解决方法如下:打开终端或命令提示符(Windows系统)。...
安装Rails时构建webso... 在安装Rails时,如果构建websocket-driver时发生错误,可以尝试以下解决方法:更新系...
安装react-native-... 要安装react-native-onesignal并在应用关闭时仍能接收通知,可以按照以下步骤进行:...
按转换模式过滤日志【%t】。 要按照转换模式过滤日志,可以使用正则表达式来实现。下面是一个示例代码,使用Java语言的Patter...
Apache Nifi在Kub... Apache Nifi可以在Kubernetes上运行,并且已经准备好用于生产环境。下面是一个使用H...
安装React-Scripts... 这是因为React-Scripts使用Facebook工具包中的一些脚本。 joinAdIntere...
安装React Native时... 安装React Native时可能会出现各种错误,下面是一些常见错误和解决方法的代码示例:Error...
安装ug未能链接到许可证服务器 安装UG未能链接到许可证服务器是UG用户在安装软件时常遇到的问题之一。该问题的解决方法需要技术向的知...