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

相关内容

热门资讯

最新技巧!海螺众娱辅助脚本,w... 最新技巧!海螺众娱辅助脚本,wepoker有机器人吗,德州教程(竟然有挂)是一款可以让一直输的玩家,...
来一盘!桂麻圈辅助,hhpok... 来一盘!桂麻圈辅助,hhpoker德州牛仔视频,AI教程(有挂存在);来一盘!桂麻圈辅助,hhpok...
9分钟了解!蜀渝牌乐汇挂机软件... 9分钟了解!蜀渝牌乐汇挂机软件,wepoker代打辅助机器人,我来教教你(有挂教学);是一款可以让一...
发现玩家!哈局八张有没有透视挂... 发现玩家!哈局八张有没有透视挂,hhpoker有后台操控吗,曝光教程(详细教程)是一款可以让一直输的...
揭秘关于!仙桃晃晃辅助器,we... 揭秘关于!仙桃晃晃辅助器,wepoker辅助器安装包,黑科技教程(有挂教程);揭秘关于!仙桃晃晃辅助...
最新研发!微信小程序功夫川辅助... 最新研发!微信小程序功夫川辅助,wepokerplus脚本,科技教程(有挂秘诀)是一款可以让一直输的...
技巧知识分享!来几局辅助器开挂... 技巧知识分享!来几局辅助器开挂,hhpoker辅助挂是真的吗,揭秘教程(有挂秘诀)是一款可以让一直输...
玩家必用!哥哥打大a辅助神器,... 玩家必用!哥哥打大a辅助神器,hhpoker真的有透视吗,透明挂教程(有挂工具)是一款可以让一直输的...
发现玩家!贪玩互娱辅助,wej... 发现玩家!贪玩互娱辅助,wejoker内置辅助,第三方教程(新版有挂)是一款可以让一直输的玩家,快速...
揭秘关于!小程序微乐游戏辅助器... 揭秘关于!小程序微乐游戏辅助器,wepoker轻量版有透视吗,黑科技教程(有挂头条);1、很好的小程...