bdawn 2015-01-26 10:20 采纳率: 0%
浏览 1619

android BLE onServicesDiscovered返回的status=129

最近在做ble的项目,发现service的时候,onServicesDiscovered方法返回的status值为129,有的手机status是0,有的一会是0一会是129,遇到这个问题的朋友有没有已经解决的

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-03 07:37
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题可能是因为BLE设备正在连接或启动过程中。当设备与服务器建立连接时,它可能会返回一个错误状态码(例如,status=129),这可能是由于BLE设备无法与服务器正确通信。

    如果你想要处理这种情况,你可以尝试以下几种方式:

    1. 在你的服务类中添加一些异常处理代码来捕获这个错误,并提供适当的反馈给用户。
    try {
        // ...
    } catch (IOException e) {
        Log.e("BLE", "Error connecting to server: ", e);
    }
    
    1. 如果你使用的是Android Studio,你可以在项目的build.gradle文件中添加一个applicationVariants配置项,以便在不同的环境下运行应用时选择不同的配置。这样,你就可以根据设备的状态来决定是否应该显示错误信息或者如何处理错误。
    apply plugin: 'com.android.application'
    
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    dependencies {
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.google.android.gms:play-services-ble-tracker:16.0.0'
    }
    
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (output.outputFile.extension == '.apk') {
                configurations.compile.get().runtimeClasspath.add fileTree(dir: rootProject.buildDir, include: ['*.jar'])
            } else {
                configurations.compile.get().runtimeClasspath.add fileTree(dir: rootProject.buildDir, include: ['*.jar'], exclude: '*.aar')
            }
        }
    
        android {
            defaultConfig {
                applicationId "com.example.yourpackage"
                minSdkVersion 15
                targetSdkVersion 28
                versionCode 1
                versionName "1.0"
    
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            }
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                }
            }
        }
    
        productFlavors {
            flavor "debug" {}
            flavor "release" {
                manifestPlaceholders = [TAG: "TAG"]
            }
        }
    }
    
    1. 如果你使用的是Java,你可以创建一个新的类来处理这个错误并提供适当的反馈。
    public class BleConnectionManager {
    
        private static final int CONNECTION_TIMEOUT_MILLIS = 10000;
    
        public void connectToServer(String host, int port) throws IOException {
            try {
                // Connect to the server
                // ...
    
                // Wait for a response from the server
                // ...
            } finally {
                // Close the connection when done
                closeConnection();
            }
        }
    
        private void closeConnection() throws IOException {
            // Close the connection here
            // ...
        }
    }
    

    希望这些解决方案能帮助到你!

    评论

报告相同问题?