Skip to content Skip to sidebar Skip to footer

Android Bluetoothdevice.getname() Return Null

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 remoteDev

Solution 1:

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
     */privateLeScanCallbackmScanCb=newLeScanCallback() {
        @OverridepublicvoidonLeScan(final BluetoothDevice device, finalint rssi,
            byte[] scanRecord) {
            finalBleAdvertisedDatabadata= BleUtil.parseAdertisedData(scanRecord);
            StringdeviceName= device.getName();
            if( deviceName == null ){
                deviceName = badata.getName();
            }
    }


////////////////////// Helper Classes: BleUtil and BleAdvertisedData ///////////////finalpublicclassBleUtil {        
        privatefinalstatic String TAG=BleUtil.class.getSimpleName();
        publicstatic BleAdvertisedData parseAdertisedData(byte[] advertisedData) {      
            List<UUID> uuids = newArrayList<UUID>();
            Stringname=null;
            if( advertisedData == null ){
                returnnewBleAdvertisedData(uuids, name);
            }

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

                bytetype= buffer.get();
                switch (type) {
                    case0x02: // Partial list of 16-bit UUIDscase0x03: // Complete list of 16-bit UUIDswhile (length >= 2) {
                            uuids.add(UUID.fromString(String.format(
                                    "%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
                            length -= 2;
                        }
                        break;
                    case0x06: // Partial list of 128-bit UUIDscase0x07: // Complete list of 128-bit UUIDswhile (length >= 16) {
                            longlsb= buffer.getLong();
                            longmsb= buffer.getLong();
                            uuids.add(newUUID(msb, lsb));
                            length -= 16;
                         }
                        break;
                    case0x09:
                        byte[] nameBytes = newbyte[length-1];
                        buffer.get(nameBytes);
                        try {
                            name = newString(nameBytes, "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        buffer.position(buffer.position() + length - 1);
                        break;
                    }
                }
            returnnewBleAdvertisedData(uuids, name);
        }
    }


    publicclassBleAdvertisedData {
        private List<UUID> mUuids;
        private String mName;
        publicBleAdvertisedData(List<UUID> uuids, String name){
            mUuids = uuids;
            mName = name;
        }

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

        public String getName(){
            return mName;
        }
    }

Solution 2:

BluetoothDevice.getName() may return null if the name could not be determined. This could be due to any number of factors. Regardless, the name is the friendly name of the device, and shouldn't be used to distinguish it from other devices. Instead, use the hardware address through getAddress().

Solution 3:

I know this is old but this more spec-oriented answer may help answer some cases.

In Bluetooth Low Energy, advertisement and scan-response data is only required to have the Bluetooth Address. Advertisement data is how a client BTLE endpoint discovers a service device. A client can request a scan response and get more data. The device name is optional in this data. However, the BTLE spec requires that all Bluetooth Low Energy endpoints support the Generic Access service which is required to support the Device Name characteristic. Unfortunately, to read that characteristic the Android must first connect and do service discovery. If the advertisement/scan response does not provide the information, I do not believe Android connects to the device to get the name. At least I have never seen any indication of connecting without the app specifically requesting a connection. This is not what you want to be required to do if you want to make a decision to connect.

Fortunately, most BTLE devices I have worked with do provide their name in the advertisement or scan response.

Another possibility is that the device may place the name in the scan response part of the advertisement. Depending upon how one has set up Android's BTLE scanner, one might get only the advertisement data and not the scan response. In this case the name will not be found if the device puts it in the scan response. The default scanner settings, however, are such that a scan response must be received before the scan data is passed up to the app.

Solution 4:

On Marshmallow, utilize ScanRecord.getDeviceName() to retrieve the local name embedded in the scan record.

BluetoothDevice.getName() is unreliable if the local name is included in a scan response, rather than in the immediate advertising packet.

@OverridepublicvoidonScanResult(int callbackType, ScanResult scanResult) {
        super.onScanResult(callbackType, scanResult);

        // Retrieve device name via ScanRecord.StringdeviceName= scanResult.getScanRecord().getDeviceName();
    }

Solution 5:

I was trying to display name of my RN4020 Bluetooth module and faced the same issue. Found the problem in Microchip's forum:

If you enabled private service or MLDP, the maximum bytes of device name is 6 bytes, due to the 31 byte advertisement payload limitation.

I had set the device name to 9 characters. Setting the name to 4 bytes fixed the issue.

If you recognize the UUID's of your custom services so you know its your device you can also connect to the device and read its name (if its longer than 6 bytes in my case). This was also suggested in Microchips forum.

http://www.microchip.com/forums/m846328.aspx

Post a Comment for "Android Bluetoothdevice.getname() Return Null"