一光年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]+"%"就可以成功获取电量并显示了

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料