Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从蓝牙设备获取数据_Android_Bluetooth Lowenergy_Android Bluetooth - Fatal编程技术网

Android 如何从蓝牙设备获取数据

Android 如何从蓝牙设备获取数据,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,我有一个支持蓝牙LE的蓝牙条形码扫描仪,我正试图在扫描时从中获取条形码信息 我可以很好地连接到它,在我的BluetoothGattCallback中调用了onServicesDiscoveryd,但我不确定从那里该做什么 使用经典的蓝牙连接,您将从BluetoothSocket获得InputStream,您只需等待read()向您提供数据,但我不确定它如何与蓝牙LE配合使用。我试着在BluetoothGattCharacteristic中循环检查属性,如果它是read属性,我调用gatt.rea

我有一个支持蓝牙LE的蓝牙条形码扫描仪,我正试图在扫描时从中获取条形码信息

我可以很好地连接到它,在我的
BluetoothGattCallback
中调用了
onServicesDiscoveryd
,但我不确定从那里该做什么

使用经典的蓝牙连接,您将从
BluetoothSocket
获得
InputStream
,您只需等待
read()
向您提供数据,但我不确定它如何与蓝牙LE配合使用。我试着在
BluetoothGattCharacteristic
中循环检查属性,如果它是read属性,我调用
gatt.readCharacteristic(characteristic)
那么如何从扫描仪中获取条形码数据呢


这是我拥有的扫描仪。BLE设备提供的数据称为特征。这些数据包是专门形成的、紧密压缩的字节数组,用于编码特定服务的特定值。您可以在官方蓝牙网站上查看。在这里,您可以找到定义的(权威的)GATT服务和所属特征

例如,你有一台能报告速度和节奏的自行车电脑。您可以在列表中查找该项。此条目包含服务的UUID(0x1816)和指向包含特征的数据表的链接。现在,如果您转到服务特征表,您将发现几个条目。您需要速度和节奏,因此您将打开(条目的类型字段)以进入特征的数据表。在这里,您将看到值字段表,该表定义了可以从特征中读取的特定值

这是蓝牙的一部分,现在回到Android。注意,为了从特征中获得值,您必须查找这些字段。我只是假设你已经有了你想要获取数据的特征。下面是一个快速示例,用于检索车轮和曲柄转速(如果可用)

需要检查
标志
字段中的特定位,因为设备可能不会报告每种类型的数据,例如,它不会计算车轮转数。所选特征表始终包含有关此字段的相关信息(如果存在)

还值得注意的是,文档中说

CSC测量特性(CSC指循环速度和节奏)是包含标志字段的可变长度结构,并且基于标志字段的内容,可以包含一个或多个附加字段[…]

这就是为什么不能假定累积曲柄转速值位于7字节(8+32+16位;分别为1+4+2字节)偏移量处,并且应在沿字段前进时计算偏移量



这是一个从可编程设备读取循环速度和节奏值的示例。您必须为要在应用程序中支持的每个设备(或者更确切地说是服务)查找这些可用字段和值。如果该设备是特殊设备,并且无法在此GATT目录中找到,则需要查阅该设备的手册、SDK或供应商以了解更多信息。

您是否尝试了该方法?我知道您的问题标题要求读取通用BLE数据,但您似乎对条形码扫描仪特别感兴趣,因此,这个SDK似乎是一个很好的起点,而不是读取原始数据。是的,我知道SDK可以工作,但我需要支持的不仅仅是这个扫描仪,这就是为什么我想推出我自己的蓝牙逻辑。那么,你到底对哪一部分有问题?如何从特征中获取有意义的值?是的,如何从LE deviceOk中获取值,而不考虑设备,因此我需要知道我正在寻找的特定特征,并执行
mGatt.setCharacteristicNotification(特征,真)
告诉设备我想知道什么时候设备发生了变化。然后我在onCharacteristicChanged中得到一个回调,当它发生变化时,这就是你上面的代码的来源?描述符在此过程中起到了什么作用?告诉堆栈您希望通过特性的更改得到通知是一回事,但您还必须告诉服务器(远程设备)您希望它向您发送通知/指示,这就是描述符的作用。请查看更多信息。示例
CLIENT\u CHARACTERISTIC\u CONFIG
描述符的UUID(
0x2902
)(以及其他UUID)可以在中找到。
BluetoothGattCharacteristic characteristic = ... ;

int offset = 0; // we define the offset that is to be used when reading the next field

// FORMAT_* values are constants in BluetoothGattCharacteristic
// these represent the values you can find in the "Value Fields" table in the "Format" column
int flags = characteristic.getIntValue(FORMAT_UINT8, offset);

offset += 1; // UINT8 = 8 bits = 1 byte

// we have to check the flags' 0th bit to see if C1 field exists 
if ((flags & 1) != 0) {
    int cumulativeWheelRevolutions = characteristic.getIntValue(FORMAT_UINT32, offset);
    offset += 4; // UINT32 = 32 bits = 4 bytes

    int lastWheelEventTime = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2; // UINT16 = 16 bits = 2 bytes
}

// we have to check the flags' 1st bit to see if C2 field exists 
if ((flags & 2) != 0) {
    int cumulativeCrankRevolutions = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2;

    int lastCrankEventTime = characteristic.getIntValue(FORMAT_UINT16, offset);
    offset += 2;
}