Android bluetooth创建GATT连接并读取设备信息

一 GATT简介

蓝牙分为经典蓝牙和低功耗蓝牙(BLE),我们常用的蓝牙遥控器就是低功耗蓝牙
低功耗蓝牙(BLE)连接都是建立在 GATT (Generic Attribute Profile) 协议之上。
GATT全称Generic Attribute Profile(直译即:通用属性协议),是一个在蓝牙连接之上的发送和接收较短的数据段的通用规范,这些很短的数据段被称为属性(Attribute)。

二 GATT的结构

GATT的结构简单的讲,如下图:

  • gatt是多个service的集合,gatt包含多个不同的service
  • service下包含多个不同的Charcteristic(特征)
  • Charcteristic又包含value和Descriptor
image.png

每个Service和Charcteristic有一个唯一标识UUID
一般我们读取BLE设备的信息,就是读取Charcteristic下的Value值
所以我们需要知道service和Charcteristic的UUID,就能拿到这个Charcteristic下的值

三 如何知道指定的service和Charcteristic的UUID

一般使用BLE测试工具即可
我这里使用的BLE5.1ScanDemo apk,上传到网盘供大家使用
https://pan.baidu.com/s/1TdsznZsdhPPDcKbkJknyug
提取码:6666

  • 把BLE5.1ScanDemo apk安装到手机
  • 遥控器或其他BLE设备与手机蓝牙配对连接
  • 打开apk界面会显示已配对的蓝牙设备


    image.png
  • 点击击打开设备后,可以看到这个设备GATT下所有的service和对应的UUID


    image.png
  • 打开service后,可以看到service下所有的Charcteristic的和对应的UUID
    点击Charcteristic后,在上面可以看到其Value值(如下图,读取到的是设备型号SCCN001)


    image.png

四 如何建立GATT连接及如何读取Charcteristic下的数据

android 4.0以后添加了BLE的支持,在系统BluetoothDevice.java源码中已经提供了Gatt连接的接口函数
那么我们只需要找到指定的蓝牙设备获取它的BluetoothDevice实例,然后调用connectGatt函数即可


image.png
1、获取指定的蓝牙设备BluetoothDevice实例
image.png
2、建立GATT连接

使用上一步获取到的设备实例,调用connectGatt函数建立连接


image.png
3、重写GattCallback回调

在第2步建立GATT连接连接时,需要传入gattcallback实例
所以我们要先实例BluetoothGattCallback类并重写其回调函数,如图
这几个回调函数后续会用到


image.png
4、GATT连接成功,onConnectionStateChange()函数回调

gatt连接成功或失败,会回调gattCallback下的onConnectionStateChange()函数
接下来调用mBluetoothGatt.discoverServices()函数(功能是查询已连接的gatt下的service)


image.png
5、onServicesDiscovered()函数回调,获取指定Service和Characteristic

上一步调用mBluetoothGatt.discoverServices()函数后,系统会回调gattCallback下的onServicesDiscovered()函数,这表明我们已经可以通过指定的UUID来获取指定的Service实例了


image.png

如下图,在onServicesDiscovered()函数回调后,通过UUID先获取service
然后再使用获取到的service和UUID获取Characteristic
最后mBluetoothGatt.readCharacteristic(mVIDPIDCharacteristic);读取这个Characteristic


image.png
6、onCharacteristicRead()函数回调,读取Characteristic的value值

上一步调用readCharacteristic()后,系统会回调gattCallback下的onCharacteristicRead()
此时我们使用回参characteristic直接getValue()即可读取到数值


image.png
7、value值转换

有的蓝牙设备value值使用的ASCII码,需要转为VID和PID16进制值
下图为例
ASCII码:] 转为ASCII值=93 转为16进制值VID=5D
ASCII码:q 转为ASCII值=113 转为16进制值VID=71
转换过程见底部整体代码示例


image.png

五 整体代码示例

以读取泰凌芯片蓝牙遥控器的VID和PID值为例
整体代码示例如下,代码中有详细步骤注释

package com.gatt.demo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.Set;
import java.util.UUID;


/**
 * 作者:libeibei
 * 日期:20201222
 * 类功能说明:读取指定名称遥控器的VID、PID
 */

public class MainActivity extends Activity {

    public static String TAG = "BLE_READ";
    public static String BLE_NAME = "川流TV";
    private Context mContext;
    private BluetoothManager bluetoothManager;
    private BluetoothAdapter bluetoothAdapter;
    protected BluetoothDevice mSelectedDevice;
    private BluetoothGatt mBluetoothGatt;
    private BluetoothGattCharacteristic mVIDPIDCharacteristic;
    //已配对的设备
    Set<BluetoothDevice> pairedDevices;

    //GATT service UUID
    public static final UUID DEVICE_INFO_SERVICE_UUID = UUID.fromString("0000180a-0000-1000-8000-00805f9b34fb");
    //Charcteristic UUID
    public static final UUID VID_PID_CHARACTERISTIC_UUID = UUID.fromString("00002a50-0000-1000-8000-00805f9b34fb");

    TextView tv;
    String VID = "";
    String PID = "";

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0x1:
                    Toast.makeText(mContext, "VID、PID读取成功", Toast.LENGTH_LONG).show();
                    tv.setText("VID=" + VID + " PID=" + PID);
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv_vid_pid);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //第一步,初始化各工具
        init();
        //第二步,根据名称,获取指定的蓝牙设备:mSelectedDevice
        getTargetBLEDevice();

        //第三步,声明mGattCallback,并重写回调函数,见下面step 3

        //第四步,通过上两步获取的mSelectedDevice和mGattCallback建立GATT连接
        connectGatt();

        //第五步,建立gatt连接后,会回调mGattCallback下的onConnectionStateChange()函数
        //在onConnectionStateChange()函数中调用mBluetoothGatt.discoverServices();
        //见下面step 5

        //第六步,调用mBluetoothGatt.discoverServices()后
        // 会回调mGattCallback下的onServicesDiscovered()函数
        // 在该函数下
        // 1、获取DeviceInfoService 见下面step 6-1
        // 2、通过拿到的service,获取VIDPIDCharacteristic 见下面step 6-2
        // 3、读取获取到的这个VIDPIDCharacteristic 见下面step 6-3

        //第七步,读取VIDPIDCharacteristic后
        // 会回调mGattCallback下的onCharacteristicRead()函数
        // step 7-1:在这个函数下将读取出的value值
        // step 7-2:转码即可
        //(ascii字符转ascii值,再将十进制ascii值转为十六进制字符,即为VID和PID)
    }

    //step 1
    private void init() {
        mContext = MainActivity.this;
        if (bluetoothManager == null)
            bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (bluetoothAdapter == null)
            bluetoothAdapter = bluetoothManager.getAdapter();
        pairedDevices = bluetoothAdapter.getBondedDevices();
    }

    //step 2
    private void getTargetBLEDevice() {
        if (pairedDevices != null && pairedDevices.size() > 0) {
            for (BluetoothDevice bluetoothDevice : pairedDevices) {
                String name = bluetoothDevice.getName();
                Log.i(TAG, "bluetoothDevice name  " + name);
                if (bluetoothDevice != null && name.equalsIgnoreCase(BLE_NAME)) {
                    Log.i(TAG, "已找到指定蓝牙设备,该设备MAC=" + bluetoothDevice.getAddress());
                    mSelectedDevice = bluetoothDevice;
                    break;
                }
            }
        }
    }


    //step 3
    BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            Log.i(TAG, "onConnectionStateChange newstate:" + newState + " status:" + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (newState == BluetoothProfile.STATE_CONNECTED) {
                    Log.i(TAG, "============>GATT Connect Success!!<=============");
                    //step 5
                    mBluetoothGatt.discoverServices();
                } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                    if (mBluetoothGatt != null) {
                        mBluetoothGatt.close();
                        mBluetoothGatt = null;
                    }
                }
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered(), status = " + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                //step 6-1:获取DeviceInfoService
                BluetoothGattService mDeviceInfoService = gatt.getService(DEVICE_INFO_SERVICE_UUID);
                if (mDeviceInfoService == null) {
                    Log.i(TAG, "Device Info Service is null ,disconnect GATT...");
                    gatt.disconnect();
                    gatt.close();
                    return;
                }
                //step 6-2:获取遥控器VIDPID Characteristic
                mVIDPIDCharacteristic = mDeviceInfoService.getCharacteristic(VID_PID_CHARACTERISTIC_UUID);
                if (mVIDPIDCharacteristic == null) {
                    Log.e(TAG, "read mModelCharacteristic not found");
                    return;
                } else {
                    //step 6-3:读取遥控器VIDPID特性
                    mBluetoothGatt.readCharacteristic(mVIDPIDCharacteristic);
                }
            } else {
                Log.i(TAG, "onServicesDiscovered status false");
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                String value = "";
                if (characteristic.getUuid().equals(VID_PID_CHARACTERISTIC_UUID)) {
                    //step 7-1:读取出characteristic的value值
                    value = new String(characteristic.getValue()).trim().replace(" ", "");
                    Log.i(TAG, "=====>读取到 value =" + value);
                    //step 7-2:此处为ascii表字符,需转换为十进制ascii值
                    //再将十进制ascii值,转换为十六进制
                    VID = changeAsciiTo16(value.charAt(0));
                    PID = changeAsciiTo16(value.charAt(value.length() - 1));
                    //设备VID、PID读取成功,handle更新主线程界面UI
                    handler.sendEmptyMessage(0x1);

                }
            } else {
                Log.i(TAG, "onCharacteristicRead status wrong");
                if (mBluetoothGatt != null)
                    mBluetoothGatt.disconnect();
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite:" + characteristic.getUuid().toString());
        }
    };

    //step 4
    private void connectGatt() {
        if (mSelectedDevice != null)
            mBluetoothGatt = mSelectedDevice.connectGatt(mContext, false, mGattCallback);
        else
            Toast.makeText(mContext, "没有找到指定的蓝牙设备,无法建立GATT", Toast.LENGTH_LONG).show();
    }


    private String changeAsciiTo16(char a) {
        Log.i(TAG, "change from a =" + a);
        String value = "";
        int val = (int) a;
        Log.i(TAG, "change to 10进制ASCII值 val =" + val);
        //ascii值到
        value = Integer.toHexString(val).toUpperCase();
        Log.i(TAG, "change to 16进制字符串 value =" + value);
        return value;
    }
}

最终通过GATT读取到的遥控器VID和PID显示到界面上如下


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容