android BluetoothDevice.getName() return null

前端 未结 7 613
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 11:37

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 re

7条回答
  •  时光说笑
    2020-11-28 11:57

    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 uuids = new ArrayList();
                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 mUuids;
            private String mName;
            public BleAdvertisedData(List uuids, String name){
                mUuids = uuids;
                mName = name;
            }
    
            public List getUuids(){
                return mUuids;
            }
    
            public String getName(){
                return mName;
            }
        }
    

提交回复
热议问题