机器人BluetoothDevice.getName()返回NULL [英] android BluetoothDevice.getName() return null

查看:3798
本文介绍了机器人BluetoothDevice.getName()返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某个时候,BluetoothDevice.getName()返回NULL。我该如何解决呢? remoteDeviceName在以下code,也许空。我需要区分由remoteDeviceName我的设备和其他设备。

  BluetoothAdapter.getDefaultAdapter()。startLeScan(新LeScanCallback(){
            @覆盖
            公共无效onLeScan(最终BluetoothDevice类设备,最终诠释RSSI,
                    byte []的scanRecord){
                    串remoteDeviceName = device.getName();
                  Log.d(扫描,扫描设备+ remoteDeviceName);
            });
 

解决方案

最后,我找到了解决办法:

1.适用于设备连接的:

从关贸总协定特征<一个阅读设备名称href="https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.device_name.xml">org.bluetooth.characteristic.gap.device_name服务的<一个href="https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.generic_access.xml">org.bluetooth.service.generic_access.

2.对于设备没有连接:

  / **
     *获取设备名称从BLE发布的数据
     * /
    私人LeScanCallback mScanCb =新LeScanCallback(){
        @覆盖
        公共无效onLeScan(最终BluetoothDevice类设备,最终诠释RSSI,
            byte []的scanRecord){
            最后BleAdvertisedData badata = BleUtil.parseAdert​​isedData(scanRecord);
            字符串DEVICENAME = device.getName();
            如果(DEVICENAME == NULL){
                设备名称= badata.getName();
            }
    }


//////////////////////辅助类:BleUtil和BleAdvertisedData ///////////////
    最终的公共类BleUtil {
        私人最终静态字符串变量= BleUtil.class.getSimpleName();
        公共静态BleAdvertisedData parseAdert​​isedData(byte []的advertisedData){
            名单&LT; UUID&GT;的UUID =新的ArrayList&LT; UUID&GT;();
            字符串名称= NULL;
            如果(advertisedData == NULL){
                返回新BleAdvertisedData(的UUID,名称);
            }

            ByteBuffer的缓冲= ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
            而(buffer.remaining()→2){
                字节长度= buffer.get();
                如果(长度== 0)打破;

                字节类型= buffer.get();
                开关(类型){
                    案例0X02:// 16位的UUID部分名单
                    案例0×03:// 16位的UUID全部列表
                        而(长度&GT; = 2){
                            uuids.add(UUID.fromString(的String.Format(
                                    %08X-0000-1000-8000-00805f9b34fb,buffer.getShort())));
                            长度 -  = 2;
                        }
                        打破;
                    案值0x06:// 128位的UUID部分名单
                    案例0×07:// 128位的UUID全部列表
                        而(长度&GT; = 16){
                            长LSB = buffer.getLong();
                            长MSB = buffer.getLong();
                            uuids.add(新的UUID(MSB,LSB));
                            长度 -  = 16;
                         }
                        打破;
                    案例×09:
                        byte []的nameBytes =新的字节[长度1]。
                        buffer.get(nameBytes);
                        尝试 {
                            名称=新的String(nameBytes,UTF-8);
                        }赶上(UnsupportedEncodingException E){
                            e.printStackTrace();
                        }
                        打破;
                    默认:
                        buffer.position(buffer.position()+长度 -  1);
                        打破;
                    }
                }
            返回新BleAdvertisedData(的UUID,名称);
        }
    }


    公共类BleAdvertisedData {
        私人列表&LT; UUID&GT; mUuids;
        私人字符串MNAME;
        公共BleAdvertisedData(名单&LT; UUID&GT;的UUID,字符串名称){
            mUuids =的UUID;
            MNAME =名称;
        }

        公开名单&LT; UUID&GT; getUuids(){
            返回mUuids;
        }

        公共字符串的getName(){
            返回MNAME;
        }
    }
 

On sometime, BluetoothDevice.getName() return null. How can i fix it? remoteDeviceName maybe null in following code. And i need distinguish my device and other devices by 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);
            });

解决方案

Finally, i found out the solution:

1.For device connected:

Read device name from gatt characteristic org.bluetooth.characteristic.gap.device_name of service org.bluetooth.service.generic_access.

2.For device no connected:

    /**
     * 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;
        }
    }

这篇关于机器人BluetoothDevice.getName()返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆