【问题标题】:BLE trying to handle custom profile, getting Error receiving broadcast IntentBLE 尝试处理自定义配置文件,收到错误接收广播意图
【发布时间】:2016-04-14 15:54:33
【问题描述】:

所以我有一个带有自定义 UUID 的 BLE 外围设备,我可以很好地连接到它。我可以使用以下方式读取自定义特征:

 if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        int flag = characteristic.getProperties();
        int format = -1;
        if ((flag & 0x01) != 0) {
            format = BluetoothGattCharacteristic.FORMAT_UINT16;
            Log.d(TAG, "Heart rate format UINT16.");
        } else {
            format = BluetoothGattCharacteristic.FORMAT_UINT8;
            Log.d(TAG, "Heart rate format UINT8.");
        }
        final int heartRate = characteristic.getIntValue(format, 1);
        Log.d(TAG, String.format("Received heart rate: %d", heartRate));
        intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
    }
    else {
        if(characteristic.getValue()!=null){
            final int data2 = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);

            intent.putExtra("Right",String.valueOf(data2));}
    }
    sendBroadcast(intent);
}

但如果我想指定如何处理特定特征:

public final static UUID chara = UUID.fromString("custom 128bit UUID");

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    if(chara.equals(characteristic.getUuid())){
        if(characteristic.getValue()!=null){
            final int data2 = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
   intent.putExtra("Right",String.valueOf(data2));}
    }
    sendBroadcast(intent);
}

我收到错误接收广播意图.....原因:java.lang.NumberFormatException: Invalid int: "null"

谁能告诉我我做错了什么?

【问题讨论】:

  • 我也面临同样的问题。你找到解决办法了吗?

标签: android bluetooth bluetooth-lowenergy


【解决方案1】:

getIntValue 返回 Integer 类型,它可能为 null,您是否对该行有尝试/异常。 而且,您不应该成功获取 data2 然后 sendBroadcast 但并不总是发送广播吗?

【讨论】:

    猜你喜欢
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多