【问题标题】:Bluetooth Low Energy get characteristic description低功耗蓝牙获取特性描述
【发布时间】:2013-12-12 11:05:09
【问题描述】:

我有一个蓝牙 4.0 传感器,它向我发送了许多服务和特性,例如这个草稿示例:

<service uuid="ServiceUUID" advertise="true">
  <description>Test Service</description>
     <characteristic uuid="CharacteristUUID" id="test_rate">
         <properties read="true" notify = "true"/>
            <value length="1" type="hex"></value>
            <description>Test Rate</description>
     </characteristic>
</service>

我从 android 示例 BluetoothLEGatt 开始,我想动态获取特征描述名称,例如(测试速率),因为在示例中他们使用 SampleGattAttributes 类以编程方式获取描述。

有人知道这怎么可能吗?

问候。

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy android-4.4-kitkat android-4.3-jelly-bean


    【解决方案1】:

    对于 BluetoothGatt 动态数据,您必须按照以下步骤创建回调方法

    1. 创建蓝牙设备对象,设备地址就是你连接后得到的那个

       BluetoothManager m = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
              m_adapter = m.getAdapter();
      BluetoothDevice device = m_adapter.getRemoteDevice(deviceAddress);
      
    2. 创建 BluetoothGatt 对象

          private BluetoothGatt m_gatt;
          m_gatt = m_device.connectGatt(m_context, false, m_callback);
      

      3.回调实现

        private final BluetoothGattCallback m_callback = new BluetoothGattCallback()
       {        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
          String testid = characteristic.getDescriptor(characteristic.getUuid()).getCharacteristic().getStringValue(0);
      };
                      }
      

    确保蓝牙权限和其他次要变量声明。

    【讨论】:

    • 首先感谢您的宝贵时间,但我正确获取了数据,我想获取标签 内的文本,即“测试率”,动态显示给用户没有在我的代码中之前声明描述 UUID 是“测试率”。
    • 你说的是解析 XML 文件吗?你收到了哪个?
    • 在我的代码中,我想做类似 gattCharacteristic.getDescriptor(gattCharacteristic.getUuid()).getDescription();直接从对象获取测试率字符串。我不确定我是否从传感器 xml 收到...
    • 已编辑答案,请查看并告诉我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多