«

Android 蓝牙BLE 4.0 属性判断

时间:2024-3-2 17:19     作者:韩俊     分类: Android


本文简单介绍如何判断蓝牙BLE4.0的 BluetoothGattCharacteristic 属性


1、先看UUID定义;


2、BluetoothGattCharacteristic的属性定义;

/**

  • Characteristic proprty: Characteristic is broadcastable.
  • 可以广播,二进制0001

    */

    public static final int PROPERTY_BROADCAST = 0x01;




    /**
  • Characteristic property: Characteristic is readable.
  • 可读,二进制0010

    */

    public static final int PROPERTY_READ = 0x02;




    /**
  • Characteristic property: Characteristic can be written without response.
  • 只可写,二进制0100

    */

    public static final int PROPERTY_WRITE_NO_RESPONSE = 0x04;




    /**
  • Characteristic property: Characteristic can be written.
  • 可写,二进制1000

    */

    public static final int PROPERTY_WRITE = 0x08;



    /**
  • Characteristic property: Characteristic supports notification
  • 支持通知,二进制0001 0000

    */

    public static final int PROPERTY_NOTIFY = 0x10;




    /**
  • Characteristic property: Characteristic supports indication
  • 支持指示,二进制0010 0000

    */

    public static final int PROPERTY_INDICATE = 0x20;




    /**
  • Characteristic property: Characteristic supports write with signature
  • 支持写签名,二进制0100 0000

    */

    public static final int PROPERTY_SIGNED_WRITE = 0x40;




    /**
  • Characteristic property: Characteristic has extended properties
  • 可扩展属性,二进制1000 0000

    */

    public static final int PROPERTY_EXTENDED_PROPS = 0x80;


3、由二进制得出其属性由八个标志位表示,拥有的属性值是相加的,所以可以用 & 方法判断其属性;

       // 可读
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {

        }
        // 可写,注:要 & 其可写的两个属性
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0 
                || (charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {

        }
        // 可通知,可指示
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0 
                || (charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {

        }


欢迎加入“六度交流” QQ群:174254613



标签: android

热门推荐