本文介绍了在leScanCallback中,device.getName()在少数设备上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查了以下帖子,但对我的情况没有帮助:

Checked following post but not helpful on my case :

BluetoothDevice始终在getName()上返回null

我的问题

我正在开发一个应用程序,该应用程序扫描附近的蓝牙设备并显示在主屏幕中.但是在少数设备(例如moto G,三星note2)上,leScanCallback上的设备名称为空.

I am developing an application that scans the nearby Bluetooth device and displays in the home screen.But on few devices(i.e moto G,samsung note2) the device name is null on leScanCallback.

我的代码

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi,
                         byte[] scanRecord) {
       if (device.getName() != null){//<<- Here it null on few device
           //my process
        }
  }
};

注意:如果它返回null,则需要引发警报,但由于扫描持续进行,因此我无法在onlescan内执行此操作.在此之前,我需要根据蓝牙版本向用户发出警报,这是可能的,为什么在少数设备上我无法获得设备名称?预先感谢.

Note:I need to throw an alert if it return null but i can't do it inside the onlescan because the scanning continuously. Before that i need to throw alert to user based on the bluetooth version is it possible and why on few devices i wont get the device name ? Thanks in advance.

推荐答案

第一次检测到广告时不会填充名称字段,因为它来自单独的ScanResponse数据包,该数据包要等到广告回调之后才到达得到.

The name field is not populated on the first detections of the advertisement because it comes from a separate ScanResponse packet, which does not arrive until later than the advertisement callback you get.

这意味着您必须保留对扫描返回的BluetoothDevice对象的引用,并等待几秒钟以查看是否填充了该名称.

This means you must hold a reference to the BluetoothDevice object returned by the scan and wait a few seconds to see if the name is populated.

这篇关于在leScanCallback中,device.getName()在少数设备上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:15