小编aer*_*xr1的帖子

如何在每个开放时将我的应用程序重新连接到同一个蓝牙低功耗设备?

我正在开发一款Android应用.在开始时,此应用程序向用户显示蓝牙设备列表,当他选择其中一个打开其他活动时.

我的目标是:在用户重新打开应用程序的某一天之后,应用程序必须记住ble设备,并且必须尝试重新连接到它.

现在我已经通过这种方式获得了目标:

  • 我优先记住所选设备的mac地址
  • 当用户重新打开应用程序时,应用程序会执行隐藏扫描并尝试重新连接到具有相同MAC地址的设备.
  • 为了检索mac地址,我使用函数getAddress()(当我尝试连接到android外设时,这个函数给我一些问题,因为设备在每次启动时更改了他的MAC地址:/)

有一种获得同样东西的最佳方法吗?

关闭主题:ScanResult中的函数onScanResult有时会返回给我null设备,这是正常的吗?我使用了startLeScan(UUID [] serviceUuids,BluetoothAdapter.LeScanCallback回调),在棒棒糖之后我使用了新版本的startLeScan和Scanfilter.这可能是问题吗?

android bluetooth bluetooth-lowenergy

10
推荐指数
1
解决办法
1240
查看次数

Kotlin 和 Jackson:“没有足够的信息来推断运算符 fun <T : > JsonNode!> set(p0: String!, p1: JsonNode!) 中的参数 T”

我正在从 jackson 库 2.9 更新到 2.11。但在一些 kotlin 类中我收到以下错误:

“没有足够的信息来推断运算符 fun set 中的参数 T(p0: String!, p1: JsonNode!): T!请明确指定。”

这是一个损坏的代码示例:

fun test() {
    var objectMapper = ObjectMapper()
    var testObjectNode = objectMapper.createObjectNode()
    var result = objectMapper.createArrayNode()
    testObjectNode.set("test",testObjectNode)
}
Run Code Online (Sandbox Code Playgroud)

在 kotlin 中,我必须强制转换 set action 的结果或指定 T 类型才能使其工作。

testObjectNode.set<JsonNode>("test",testObjectNode)
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么在java上我必须不明确类型吗?

android jackson kotlin

7
推荐指数
0
解决办法
581
查看次数

bluetoothGatt writeCharacteristic 返回 false

我创建了一个具有 WRITE_TYPE_NO_RESPONSE 特性的外围设备。使用另一个应用程序,我写了特性,但我偶尔会遇到带有棒棒糖 5.1.1 的 samsung Galaxy nexus i9250 问题:有时函数 BluetoothGatt.writeCharacteristic 返回 false 并且写入未完成。

会是什么呢 ?会不会是 cyanogenmod 的错?我可以用什么方式修补这个问题?

在外围:

new BluetoothGattCharacteristic(
                UUID.fromString(characteristic),
                BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE ,
                BluetoothGattCharacteristic.PERMISSION_WRITE);
Run Code Online (Sandbox Code Playgroud)

编辑:这个问题只发生在 android >= 5.0.2

在kitkat和android 5.0.1上从来没有发生过,是偶然的吗?

android bluetooth-lowenergy android-ble

5
推荐指数
1
解决办法
3062
查看次数

从 FirebaseInstanceId.getInstance().getId() 迁移到 FirebaseInstallations.getInstance().getId()

我必须从已弃用的同步 api 迁移FirebaseInstanceId.getInstance().getId();到新的异步 firebase api:

FirebaseInstallations.getInstance().getId().addOnCompleteListener( task -> {
                                if (!task.isSuccessful()) {
                                    Log.w(TAG, "getInstanceId failed", task.getException());
                                    return;
                                }
                                // Get new Instance ID 
                                String id = task.getResult();
                            });
Run Code Online (Sandbox Code Playgroud)

我的问题是,在我的旧项目中,在我的代码的许多不同点中,已经调用了类似以下方法的方法,并且所有被调用者都期望在没有回调的情况下在线响应同步:

public static String getDeviceId() {
    if (deviceId == null) {
        initDeviceId();
    }
    return deviceId;
}

private static void initDeviceId() {
    deviceId = FirebaseInstanceId.getInstance().getId();
}
Run Code Online (Sandbox Code Playgroud)

如何在不重写所有项目的情况下迁移代码?我想过以这种方式编辑上述方法:

public static String getDeviceId() {
    if (deviceId == null) {
        deviceId = workerThread.submit(initDeviceId()).get()
    }
    return deviceId;
}

private fun initDeviceId():Callable<String?>{
    return Callable {
        val …
Run Code Online (Sandbox Code Playgroud)

java android kotlin firebase-cloud-messaging

5
推荐指数
0
解决办法
555
查看次数

Android:字节数组到十进制字符串

我有一个字节数组,我必须在textview中打印它,有两种不同的格式:十六进制字符串十进制字符串

对于十六进制字符串,我使用此函数(在stackoverflow上找到):

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
Run Code Online (Sandbox Code Playgroud)

而将字节数组转换为十进制字符串我使用这个:

   BigInteger bi = new BigInteger(value);

                    // Format to decimal
                    String s = bi.toString(); 
Run Code Online (Sandbox Code Playgroud)

对于字节到十六进制的转换,我很安全,它可以正常工作,但对于字节到十进制的字符串转换,我不太安全..

有更好的方法吗?

编辑:我的desidered输出是每个字节的十进制值

java android bytearray type-conversion

3
推荐指数
1
解决办法
2744
查看次数

TextInputEditText,如何将提示与上边距对齐

如果我使用最新的材质库版本com.google.android.material:material:1.2.0-alpha02,我在 TextInputEditText 和他的提示消息方面遇到了一些麻烦。我设置了 5 行可滚动 textInputEditText 并且我想显示提示消息与该文本输入的上边距对齐。在布局编辑器上出现在正确的位置,但是当我运行应用程序时,位置是垂直居中的。为什么 ?

相反,我没有遇到这个问题com.google.android.material:material:1.1.0-beta02

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/sendMailContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <TextView
            android:id="@+id/popupTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:text="@string/sendBusinessCard"
            android:textColor="@android:color/black"
            android:textSize="22sp" />

        <TextView
            android:id="@+id/subTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/sendBusinessCard"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:layout_below="@+id/popupTitle"
            android:layout_marginEnd="40dp"
            android:layout_marginStart="40dp"
            android:layout_marginTop="20dp"/>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/noteMessageContainer"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/subTitle"
            android:layout_marginEnd="40dp"
            android:layout_marginStart="40dp"
            android:layout_marginTop="10dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/noteMessage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/emailText"
                android:inputType="text|textMultiLine|textCapSentences"
                android:textColor="@android:color/black"
                android:textColorHint="@color/gray9B"
                android:textSize="16sp"
                android:isScrollContainer="true"
                android:scrollbars="vertical"
                android:textIsSelectable="true"
                android:minLines="5"
                android:maxLines="5"
                android:gravity="top"/>

        </com.google.android.material.textfield.TextInputLayout>

        <Button
            android:id="@+id/notePopupLeftButton"
            android:layout_width="180dp"
            android:layout_height="35dp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/noteMessageContainer"
            android:layout_marginBottom="20dp"
            android:layout_marginStart="20dp" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-textinputlayout android-textinputedittext material-components-android

3
推荐指数
1
解决办法
2845
查看次数