小编jpo*_*o38的帖子

Android上的实时蓝牙SPP数据流仅工作5秒

我有一个自制的蓝牙设备,测量500Hz的心电图:设备每2 ms发送9个字节的数据(标头,ECG测量,页脚).所以这大约是9*500 = 4.5kbytes/s的数据流.

我有一个C++ Windows程序能够连接设备并检索数据流(用Qt/qwt显示).在这种情况下,我使用Windows控制面板来绑定设备,然后使用boost serial_port接口通过虚拟COM端口连接它.这非常有效,而且我实时接收数据流:我每2ms左右得到一个测量点.

我通过QtCreator(Qt 5.3.2)在Android上移植了整个C++程序.我有实时问题.数据流在前5秒内处于"实时"状态,然后性能将大幅降低(请参阅如何使用Java Android SDK进行良好的实时数据流).

因为我认为问题可能是由于C++/Qt,我使用Eclipse编写了一个完全空白的纯Java/Android项目.它有同样的问题!

问题是:这段代码有问题吗?为什么我只在5秒内实时接收数据?在Android平台上使用5秒强化BT之后会发生什么?为什么它会减慢BT数据的接收速度?

这是我的Java程序:

BluetoothHelper.java(具有连接/断开/读取和写入数据的功能:

package com.example.helloworld;

import android.util.Log;
import android.content.Context;
import android.os.Bundle;
import java.util.Locale;
import java.util.concurrent.Semaphore;
import java.lang.String;
import java.lang.Thread;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.lang.InterruptedException;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothManager;
import android.util.SparseArray;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import java.util.UUID;
import java.util.Date;
import java.util.Calendar;
import java.util.Vector;
import java.util.Set;
import java.util.Arrays;

public class BluetoothHelper …
Run Code Online (Sandbox Code Playgroud)

java streaming android bluetooth

7
推荐指数
1
解决办法
9735
查看次数

应该在哪里!=运算符在类层次结构中定义?

这是一个非常简单的类层次结构:

class A
{
public:
    A( int _a ) : a( _a ) {}

    virtual bool operator==( const A& right ) const
    {
        return a == right.a;
    }

    virtual bool operator!=( const A& right ) const
    {
        return !( *this == right );
    }

    int a;
};

class B : public A
{
public:
    B( int _a, int _b ) : A( _a ), b( _b ) {}

    virtual bool operator==( const B& right ) const
    {
        return A::operator==( right …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading comparison-operators

7
推荐指数
2
解决办法
398
查看次数

计数元素低于std :: set中的给定值

我需要找到多少元素低于给定的元素std::set.

我认为使用正确的函数是std::lower_bound将迭代器返回到第一个元素,该元素大于或等于给定的一个....所以这个迭代器的索引是我正在寻找的...但我不能从迭代器中找到索引:

#include <iostream>
#include <algorithm>
#include <set>

int main()
{
    std::set<int> mySet;
    mySet.insert( 1 );
    mySet.insert( 2 );
    mySet.insert( 3 );
    mySet.insert( 4 );

    std::set<int>::const_iterator found = std::lower_bound( mySet.begin(), mySet.end(), 2 );

    if ( found != mySet.end() )
        std::cout << "Value 2 was found at position " << ( found - mySet.begin() ) << std::endl;
else
        std::cout << "Value 2 was not found" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这不编译:

16:63: error: no match for 'operator-' (operand types …
Run Code Online (Sandbox Code Playgroud)

c++ stl set

7
推荐指数
1
解决办法
1926
查看次数

如何检查两个元组的所有成员是否不同?

std::tuple<...>::operator!=如果两个比较元组中至少有一个成员不同,则返回true .

如果两个比较元组的所有成员不同,我需要一个返回true的函数:

template <class... Args>
bool areAllMembersDifferent( const std::tuple<Args...>& left, const std::tuple<Args...>& right )
{
    bool allDiff = true;

    // iterate through the tuples are set allDiff to false if one member's is different than other's

    return allDiff;
}
Run Code Online (Sandbox Code Playgroud)

受到我在网络上发现的启发,我写了这个(改编了一个打印元组内容的函数):

template <std::size_t N, std::size_t, class = make_index_sequence<N>>
struct CheckTupleLoop;

template <std::size_t N, std::size_t J, std::size_t... Is>
struct CheckTupleLoop<N, J, index_sequence<Is...>> {
    template <class Tup>
    int operator()(bool& allDiff, const Tup &left,const Tup &right) {
        if …
Run Code Online (Sandbox Code Playgroud)

c++ tuples c++11 stdtuple

7
推荐指数
1
解决办法
284
查看次数

Doxygen:如何引用函数,但带有参数值

当我生成此类的文档时:

class MyClass
{
    /** Some description
     * \param inhibit some description
     */
    virtual void inhibitSaving( bool inhibit = true ) = 0;

    /** \return true if @ref inhibitSaving with parameter set to true has been called previously */
    virtual bool isSavinginhibited() const = 0;
};
Run Code Online (Sandbox Code Playgroud)

isSavinginhibited的描述有一个超链接到inhibitSaving.

但是,如果我将描述写成如下:

/** \return true if @ref inhibitSaving(true) has been called previously */
virtual bool isSavinginhibited() const = 0;
Run Code Online (Sandbox Code Playgroud)

isSavinginhibited的描述没有指向 的超链接inhibitSaving

考虑到这个讨论,它应该有效。为什么我没有获得超链接。我究竟做错了什么?

c++ doxygen

7
推荐指数
1
解决办法
1万
查看次数

将 std::ostream 转换为 std::string

我维护的旧代码将文本收集到std::ostream. 我想将其转换为std::string.

我发现转换的例子std::sstringstreamstd::ostringstream等来std::string却没有明确std::ostreamstd::string

我怎样才能做到这一点?(仅限古代 C++ 98,请不要提升)

c++ std

7
推荐指数
1
解决办法
1万
查看次数

我应该使用哪种助推器来存储人类年龄

我必须存储用户的年龄(年,月,日......可能是小时,分钟,秒).我正在使用C++和boost.

我不确定我应该使用哪类boost::posix_time(或boost::date_time).

我试过了boost::posix_time::time_duration,但这并不明显,因为没有构造函数计算一年,只有几个小时,所以我做了:

boost::posix_time::time_duration age = boost::posix_time::hours(24*365*ageInYears);
Run Code Online (Sandbox Code Playgroud)

但我不确定这是一个好策略,因为所有年份都没有365天;-)

我也尝试过boost::gregorian::date,但这很棘手,因为这个版本不允许year在1400之前存储(这会存储日期,而不是持续时间).

  • 我不想存储用户的出生日期,因为我需要在我的程序运行时存储它的年龄(医疗数据).
  • 我不想存储常规int因为它不够准确(24岁+ 11个月差不多25岁).
  • 我不想存储一个,float因为我不想重新发明轮子与浮动到年龄转换我将不得不做...

真的没有课程可以很容易地存储多年,并且可选择一些月份和日期来提升吗?

理想情况下,对于一个30岁半的人,我希望能够创建一个这样的对象:boost::....... theAge( 30, 6, 0 );然后:

  • 有一个年龄的功能:theAge.years()返回30(忽略几个月)
  • 可能有一个转换浮动,这将给我30.5作为一个年龄

c++ boost

6
推荐指数
2
解决办法
287
查看次数

当库静态链接时,静态变量会发生什么

假设我有(A)实现单例模式的库(它的实现中有一个静态变量).

(A) 库被编译为静态库.

现在,让我说我的问题:

  • (B),另一个静态链接静态库(A).
  • (C),另一个静态链接静态库(A).
  • (D),一个顶层程序与(B)(C).

最后,我的单身人士真的是单身人士(我的变量真的是静态的)吗?是(B)(C)(A)(它是unic)相同的静态变量?或者(A)是静态链接两次嵌入式(A)代码的事实是否最终导致我的静态变量(A)在最终二进制代码中出现两次?那么如果(B)修改静态变量值,(C)会不会看到变化?

注意:我在更改要静态链接而不是动态链接的项目库时遇到过这种情况.我只是想知道我是否做错了什么,或者这是否是一个正常的已知行为.

c++ static static-libraries static-linking

6
推荐指数
1
解决办法
2790
查看次数

如何安全地偏移位而没有未定义的行为?

我正在编写一个函数,它将bitset转换为int/uint值,因为bitset可能比目标类型的位数少.

这是我写的函数:

template <typename T,size_t count> static T convertBitSetToNumber( const std::bitset<count>& bitset )
{
    T result;
    #define targetSize (sizeof( T )*CHAR_BIT)
    if ( targetSize > count )
    {
        // if bitset is 0xF00, converting it as 0x0F00 will lose sign information (0xF00 is negative, while 0x0F00 is positive)
        // This is because sign bit is on the left.
        // then, we need to add a zero (4bits) on the right and then convert 0xF000, later, we will divide by 16 …
Run Code Online (Sandbox Code Playgroud)

c++ std-bitset

6
推荐指数
1
解决办法
225
查看次数

如何迭代可变参数模板参数来创建可变数量的局部变量?

我有许多非常相似的函数,但使用不同数量和类型的本地对象运行:

template <class T> T* create1( const std::vector<std::string>& names )
{
    A a( names[0] );
    B b( names[1] );
    C c( names[2] );

    if ( a.valid() && b.valid() && c.valid() )
        return new T( a, b, c );
    else
        return NULL;
}

template <class T> T* create2( const std::vector<std::string>& names )
{
    D d( names[0] );
    E e( names[1] );

    if ( d.valid() && e.valid() )
        return new T( d, e );
    else
        return NULL;
}

create1<ABC>( { "nameA", "nameB", …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

6
推荐指数
1
解决办法
1926
查看次数