Android Bluetooth, finds devices with empty names, on LG phone

邮差的信 提交于 2019-12-11 12:39:20

问题


I've written a small BluetoothReceiver, which handles most of the BT states.

The receiver works for most of the scenarios, I put it through, but in some cases the Devices found has an empty string for a name, and I cannot tell one device from another.

Here is the code snippet of the finding and a piece of Log to back it up:

NOTE: The BT adapter is been turned off and on before the second detection.

ANOTHER NOTE: This scenario happens exactly every second time. The first time the names are found, the second times there are no names

public class BT_Receiver
        extends BroadcastReceiver {
...
    @Override
    public void onReceive(Context context, Intent intent) {
        if (deviceDetectionListener.allDevicesFound())
            return;
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            state = BT_State.getInstanceForState(newState);
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            if (state == BT_State.On && enableAndDiscover)
                startDiscoveringDevices();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            state = BT_State.Discovering;
            Toast.makeText(activity, state.name(), Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            state = BT_State.On;
            Toast.makeText(activity, "Finished discovery", Toast.LENGTH_SHORT).show();
            return;
        }
        if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            int discoveryDuration = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, -1);
            state = BT_State.Advertising;
            Toast.makeText(activity, state.name() + ", duration: " + discoveryDuration, Toast.LENGTH_SHORT).show();
        }
        if (!BluetoothDevice.ACTION_FOUND.equals(action))
            return;
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
(Look at the LOG RESULT)       
                    Log.i(TAG, "Bluetooth device found: " + device.getName() + ", " + device.getBluetoothClass() + ", " + device.getAddress());
        deviceDetectionListener.newDeviceDetected(device);
        if (deviceDetectionListener.allDevicesFound())
            detectionCompleted();
    }

...
}

The Log with the devices names:

08-15 22:54:16.500: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [bbbb:Demo gizmo 2], 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:16.578: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: [aaaa:Demo gizmo 1], 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:33.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: Zomaar Iemand, 5a0204, 20:21:A5:C0:CF:6F

Another Log without the names:

08-15 22:54:34.304: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 00:26:E2:66:31:30
08-15 22:54:36.882: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a0204, 6C:0E:0D:77:B0:96
08-15 22:54:38.601: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 5a020c, 00:26:CC:81:AF:AD
08-15 22:54:39.820: INFO/SOD:BT_Receiver(16151): <!>com.nu.art.software.log.Log 69<!> Bluetooth device found: , 520204, 78:CA:04:83:45:CD

Thanks in advance,

Adam.


回答1:


This is probably a feature not a bug, I found a workaround for this, that in case the device does not have a name I don't save it, and if no device was found run the inquiry again.



来源:https://stackoverflow.com/questions/7070171/android-bluetooth-finds-devices-with-empty-names-on-lg-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!