一光年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 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号