zjy323520
一光年lost
采纳率57.1%
2016-08-08 03:06 阅读 3.1k
已采纳

关于蓝牙4.0 BLE读取电量的问题

private void displayGattServices(List<BluetoothGattService> gattServices){
    if(gattServices == null) return;
    String uuid = null;
    for(BluetoothGattService gattService : gattServices){
        uuid = gattService.getUuid().toString();
        if(uuid.equalsIgnoreCase(BATTERY_SERVICE)){
            String uuid_charas = null;
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
            for(BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics){
                uuid_charas = gattCharacteristic.getUuid().toString();
                if(uuid_charas.equalsIgnoreCase(BATTERY_CHARATERISTIC)){
                    final int charaProp = gattCharacteristic.getProperties();
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                        if (mNotifyCharacteristic != null) {
                            mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
                            mNotifyCharacteristic = null;
                        }
                        mBluetoothLeService.readCharacteristic(gattCharacteristic);
                    }
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        mNotifyCharacteristic = gattCharacteristic;
                        mBluetoothLeService.setCharacteristicNotification(gattCharacteristic, true);
                    }
                }
            }
        }
    }
}

private void broadcastUpdate(String action,BluetoothGattCharacteristic characteristic){
final Intent intent = new Intent(action);
if(UUID_HEART_RATE_MEASUREMENT.equals(characteristic .getUuid())){
int flag = characteristic.getProperties();
int format = -1;
if((flag&0x01) != 0){
format = BluetoothGattCharacteristic.FORMAT_UINT16;
}else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
}
final int heartRate = characteristic.getIntValue(format,1);
intent.putExtra(EXTRA_DATA,String.valueOf(heartRate));
}else {
final byte[] data = characteristic.getValue();
if(data != null && data.length >0){
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar: data)
stringBuilder.append(String.format("%02X",byteChar));
intent.putExtra(EXTRA_DATA,new String(data)+"\n"+stringBuilder.toString());
}
}
sendBroadcast(intent);
}
读取到了电量的特征值,但是显示d 64,怎么让它解析成百分比的形式显示在text文本?求各位大侠拔刀相助!~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    zjy323520 一光年lost 2016-08-11 05:22

    自己解决了,直接用characteristic.getValue()[0]+"%"就可以成功获取电量并显示了

    点赞 评论 复制链接分享

相关推荐