一光年lost 2016-08-08 03:06 采纳率: 57.1%
浏览 3320
已采纳

关于蓝牙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条回答 默认 最新

  • 一光年lost 2016-08-11 05:22
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬取豆瓣电影相关处理
  • ¥15 手机淘宝抓清除消息接口
  • ¥15 C#无selenium
  • ¥15 LD衰减计算的结果过大
  • ¥15 用机器学习方法帮助保险公司预测哪些是欺诈行为
  • ¥15 计算300m以内的LD衰减
  • ¥15 数据爬取,python
  • ¥15 怎么看 cst中一个面的功率分布图,请说明详细步骤。类似下图
  • ¥15 为什么我的pycharm无法用pyqt6的QtWebEngine
  • ¥15 FOR循环语句显示查询超过300S错误怎么办