android BluetoothDevice.getName() 返回 null

标签 android bluetooth null device network-scan

有时,BluetoothDevice.getName() 返回 null。我该如何解决? remoteDeviceName 在以下代码中可能为 null。我需要通过 remoteDeviceName 区分我的设备和其他设备。

BluetoothAdapter.getDefaultAdapter().startLeScan(new LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi,
                    byte[] scanRecord) {
                    String remoteDeviceName = device.getName();
                  Log.d("Scanning", "scan device " + remoteDeviceName);
            });

最佳答案

最后,我找到了解决方案:

1.对于连接的设备:

从gatt特性中读取设备名称org.bluetooth.characteristic.gap.device_name服务org.bluetooth.service.generic_access .

2.对于未连接的设备:

    /**
     * Get device name from ble advertised data
     */
    private LeScanCallback mScanCb = new LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi,
            byte[] scanRecord) {
            final BleAdvertisedData badata = BleUtil.parseAdertisedData(scanRecord);
            String deviceName = device.getName();
            if( deviceName == null ){
                deviceName = badata.getName();
            }
    }


////////////////////// Helper Classes: BleUtil and BleAdvertisedData ///////////////
    final public class BleUtil {        
        private final static String TAG=BleUtil.class.getSimpleName();
        public static BleAdvertisedData parseAdertisedData(byte[] advertisedData) {      
            List<UUID> uuids = new ArrayList<UUID>();
            String name = null;
            if( advertisedData == null ){
                return new BleAdvertisedData(uuids, name);
            }

            ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
            while (buffer.remaining() > 2) {
                byte length = buffer.get();
                if (length == 0) break;

                byte type = buffer.get();
                switch (type) {
                    case 0x02: // Partial list of 16-bit UUIDs
                    case 0x03: // Complete list of 16-bit UUIDs
                        while (length >= 2) {
                            uuids.add(UUID.fromString(String.format(
                                    "%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
                            length -= 2;
                        }
                        break;
                    case 0x06: // Partial list of 128-bit UUIDs
                    case 0x07: // Complete list of 128-bit UUIDs
                        while (length >= 16) {
                            long lsb = buffer.getLong();
                            long msb = buffer.getLong();
                            uuids.add(new UUID(msb, lsb));
                            length -= 16;
                         }
                        break;
                    case 0x09:
                        byte[] nameBytes = new byte[length-1];
                        buffer.get(nameBytes);
                        try {
                            name = new String(nameBytes, "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        buffer.position(buffer.position() + length - 1);
                        break;
                    }
                }
            return new BleAdvertisedData(uuids, name);
        }
    }


    public class BleAdvertisedData {
        private List<UUID> mUuids;
        private String mName;
        public BleAdvertisedData(List<UUID> uuids, String name){
            mUuids = uuids;
            mName = name;
        }

        public List<UUID> getUuids(){
            return mUuids;
        }

        public String getName(){
            return mName;
        }
    }

关于android BluetoothDevice.getName() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290640/

相关文章:

android - 在 android 中与 ELM327 建立连接后无法向 ELM327 发送 AT 命令

java - 蓝牙设备连接问题

Android - 谷歌地图不显示

android - 使用 setMyLocationEnabled 时出现 Google Maps v2 错误

android - 后台扫描中的华为蓝牙行为

mysql - 将 MySQL 查询移植到 PostgreSQL

arrays - 将 Postgres ARRAY 中的 {NULL} 更改为 NULL

objective-c - NSError: 使用 nil 来检测 Error 实际上会关闭错误报告吗?

java - 如何在非 ICS android 设备上拥有 android ICS 组件

Android 如何在 ImageView 中显示 54 张卡片图像中的 1 张