在Android应用程序中连续读取ble中的数据

时间:2017-02-11 07:22:18

标签: android bluetooth bluetooth-lowenergy bluetooth-gatt

我想通过蓝牙在Android应用程序中连续读取ble硬件中的数据。 连接已经完成,我可以从应用程序发送数据到ble但不能从ble。

读取数据 从ble获取一些数据时必须调用

let path = // Path to messages (/message/<conversationId>/) let msgRef = // Reference to messages msgRef.child(id) .queryOrdered(byChild: "time") .observe(FIRDataEventType.value, with: { (snapshot) in let data = JSON(snapshot.value ?? []) var messages = [Message]() for msg in data { let holder = Message(data: msg.1) messages.append(holder) print("key: \(msg.0) time: \(holder.getTime())") } // then returns results via completion block // The right way: let data = snapshot.children var messages = [Message]() for msg in data.allObjects as! [FIRDataSnapshot] { let thisMsgData = JSON(msg.value ?? []) let holder = Message(data: thisMsgData) messages.append(holder) print("key: \(msg.key) time: \(holder.getTime())") } }) } 方法,但此回调方法不会调用。我正试着通知但是

message: {
    conversationId: {
        msgId: {
            // the rest of the message data
            "time": 20170210051745 <-- as an Int
            // format of date: yyyyMMddHHmmss
        }
    }
}
onCharactersticChanged
writeChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(BLEUtils.CLIENT_CHARACTERISTIC_CONFIG_UUID, BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

gatt.writeDescriptor(descriptor);

返回 // Enable local notifications gatt.setCharacteristicNotification(writeChar, true);

2 个答案:

答案 0 :(得分:2)

我建议您在订阅特征时设置ENABLE_NOTIFICATION_VALUE。这是我的代码:

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            List<BluetoothGattService> services = getSupportedGattServices();
            for (BluetoothGattService service : services) {
                if (!service.getUuid().equals(UUID_TARGET_SERVICE))
                    continue;

                List<BluetoothGattCharacteristic> gattCharacteristics =
                        service.getCharacteristics();

                // Loops through available Characteristics.
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                    if (!gattCharacteristic.getUuid().equals(UUID_TARGET_CHARACTERISTIC))
                        continue;

                    final int charaProp = gattCharacteristic.getProperties();

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        setCharacteristicNotification(gattCharacteristic, true);
                    } else {
                        Log.w(TAG, "Characteristic does not support notify");
                    }
                }
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

然后您可以通过NOTIFICATION订阅特征:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
    descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                                :BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

}

之后,您可以开始写入设备:

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (descriptor.getCharacteristic().getUuid().equals(UUID_TARGET_CHARACTERISTIC)) {
                Log.i(TAG, "Successfully subscribed");
            }

            byte[] data = <Your data here>;
            BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE);

            BluetoothGattCharacteristic charac = Service
            .getCharacteristic(UUID_TARGET_CHARACTERISTIC);

            charac.setValue(data);
            mBluetoothGatt.writeCharacteristic(charac);
        } else {
            Log.e(TAG, "Error subscribing");
        }
    }

您将收到回调onCharactersticChanged进行阅读,无需实际调用读取操作。

记住mBluetoohGatt一次只能处理1个操作,如果你执行另一个操作而前一个操作未完成,它将不会进入队列,但会返回false。

答案 1 :(得分:1)

如果返回false,通常意味着您已经有待处理的GATT操作。您需要构建代码,以便一次只有一个未完成的GATT操作。您需要等待相应的回调到达,直到您可以执行下一个操作。