BA.startDiscovery() 返回一些设备名称为 “null”。

huangapple go评论55阅读模式
英文:

BA.startDiscovery() returns "null" for some device names

问题

我是一名初级的Android应用程序员,正在开发一个与微控制器(ESP32)通信的BLE应用程序。

我已经成功启用了BLE和位置权限,并且已经能够扫描附近的BLE设备。

我注意到,当我开始发现附近的BLE设备时,一些设备被检测为 "null"。

点击 "SCAN" 按钮时的调试日志:

E/SCAN BUTTON: Clicked
E/CHECK: CHECKING LOCATION PERMISSION
E/INFO: LOCATION PERMISSION ALREADY GRANTED:
E/INFO: LOCATION PERMISSION IS OK, START BLE DISCOVERY
I/BluetoothAdapter: startDiscovery
E/CALLBACK: onReceive
E/BroadcastReceiver: SCANNING IN PROGRESS
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = DESKTOP-KPO80BS
I/System.out: DEVICE MAC FOUND = 64:6E:69:78:B9:54
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 5E:52:CD:CD:EB:EA
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = [LG] webOS TV UP75003LF
I/System.out: DEVICE MAC FOUND = 4C:BC:E9:4E:62:17
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 7F:CA:0B:28:8E:86
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = CGATES-DV8947
I/System.out: DEVICE MAC FOUND = 10:76:36:B5:7C:A4
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = [LG] webOS TV UP75003LF
I/System.out: DEVICE MAC FOUND = 7C:5E:DC:65:EB:E9
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = JBL Bar 5.1_5883
I/System.out: DEVICE MAC FOUND = 7E:96:FC:6A:9F:A9
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 4E:AE:EC:2B:D9:52
E/CALLBACK: onReceive
E/BroadcastReceiver: DISCOVERY FINISHED

在我的手机上的显示情况(在我的物理三星Flip 2上模拟)。

我的 build.gradle(您可以在其中看到目标 SDK 和 minSDK):

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.location_test_v2'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.location_test_v2"
        minSdk 25
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation "androidx.fragment:fragment:1.5.6"
    implementation "androidx.activity:activity:1.7.0"
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

configurations.implementation {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
}

当我调用 BA.startDiscovery() 时执行的回调函数:

private final BroadcastReceiver devicesFoundReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        Log.e("CALLBACK", "onReceive");
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String deviceName = device.getName();
            String deviceHardwareAddress = device.getAddress(); // MAC address

            //Log.e("BroadcastReceiver", "DEVICE FOUND: ",deviceName);
            System.out.print("DEVICE NAME FOUND = ");
            System.out.println(deviceName);
            System.out.print("DEVICE MAC FOUND = ");
            System.out.println(deviceHardwareAddress);
            list.add(deviceName);

        }
        else if(BA.ACTION_DISCOVERY_FINISHED.equals(action)){
            Log.e("BroadcastReceiver", "DISCOVERY FINISHED ");
            ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list);
            listview.setAdapter(adapter);
        }
        else if(BA.ACTION_DISCOVERY_STARTED.equals(action)){
            listview.setAdapter(null);
            Log.e("BroadcastReceiver", "SCANNING IN PROGRESS");
        }
    }
};

我发现一些其他人也在抱怨这个问题,但没有明确的解决方案:

https://issuetracker.google.com/issues/240485116

我希望能够得到一些建议,关于如何处理这个问题。提前感谢您。

英文:

I am a beginner Android App programmer and I am working on BLE app to communicate with the microcontroller (ESP32).

I have managed to get the APP going (enabling the BLE and Location permissions) and got to the point where I am able to scan nearby BLE devices.

I have noticed that when I start discovering nearby BLE devices, some devices are detected as "null"

The debug logs when I click "SCAN" button:

E/SCAN BUTTON: Clicked 
E/CHECK: CHECKING LOCATION PERMISSION 
E/INFO: LOCATION PERMISSION ALREADY GRANTED: 
E/INFO: LOCATION PERMISSION IS OK, START BLE DISCOVERY 
I/BluetoothAdapter: startDiscovery
E/CALLBACK: onReceive
E/BroadcastReceiver: SCANNING IN PROGRESS
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = DESKTOP-KPO80BS
I/System.out: DEVICE MAC FOUND = 64:6E:69:78:B9:54
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 5E:52:CD:CD:EB:EA
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = [LG] webOS TV UP75003LF
I/System.out: DEVICE MAC FOUND = 4C:BC:E9:4E:62:17
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 7F:CA:0B:28:8E:86
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = CGATES-DV8947
I/System.out: DEVICE MAC FOUND = 10:76:36:B5:7C:A4
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = [LG] webOS TV UP75003LF
I/System.out: DEVICE MAC FOUND = 7C:5E:DC:65:EB:E9
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = JBL Bar 5.1_5883
I/System.out: DEVICE MAC FOUND = 7E:96:FC:6A:9F:A9
E/CALLBACK: onReceive
I/System.out: DEVICE NAME FOUND = null
I/System.out: DEVICE MAC FOUND = 4E:AE:EC:2B:D9:52
E/CALLBACK: onReceive
E/BroadcastReceiver: DISCOVERY FINISHED 

How it looks on my phone (Simulating on my Physical Samsung Flip 2).
BA.startDiscovery() 返回一些设备名称为 “null”。

My build. gradle (Here you can see the target SDK and minSDK:

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.location_test_v2'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.location_test_v2"
        minSdk 25
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation "androidx.fragment:fragment:1.5.6"
    implementation "androidx.activity:activity:1.7.0"
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

configurations.implementation {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
}

The callback function that is being executed when I call BA.startDiscovery() :

    private final BroadcastReceiver devicesFoundReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Log.e("CALLBACK", "onReceive");
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address

                //Log.e("BroadcastReceiver", "DEVICE FOUND: ",deviceName);
                System.out.print("DEVICE NAME FOUND = ");
                System.out.println(deviceName);
                System.out.print("DEVICE MAC FOUND = ");
                System.out.println(deviceHardwareAddress);
                list.add(deviceName);
                
            }
            else if(BA.ACTION_DISCOVERY_FINISHED.equals(action)){
                Log.e("BroadcastReceiver", "DISCOVERY FINISHED ");
                ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,list);
                listview.setAdapter(adapter);
            }
            else if(BA.ACTION_DISCOVERY_STARTED.equals(action)){
                listview.setAdapter(null);
                Log.e("BroadcastReceiver", "SCANNING IN PROGRESS");
            }
        }
    };

I have found some other people complaining about this but no clear solution has been provided:
https://issuetracker.google.com/issues/240485116

I hope to get some advice regarding how to handle this. Thanks in advance.

答案1

得分: 2

A Bluetooth LE device can transmit its name in the advertisement message, but it does not have to. If "DEVICE NAME" is null, use "DEVICE MAC" instead.

英文:

A Bluetooth LE device can transmit its name in the advertisement message, but it does not have to. If "DEVICE NAME" is null, use "DEVICE MAC" instead.

huangapple
  • 本文由 发表于 2023年6月13日 02:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定