ble device bluetoothdevice.getname() returns null [duplicate]

折月煮酒 提交于 2019-12-12 04:59:53

问题


i am using the android's Bluetoothgatt sample application. the device is connected but the bluetoothdevice.getname() returns null. this happens only on micromax mobile. (it is having Kitkat os and bluetooth 4.0 support). is there any solution to read the data from the device? i am able to get the device name and other characterstics on other phone.


回答1:


Try below code.

public class BLEStateReceiver extends BroadcastReceiver {
String action = intent.getAction();
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if(device!= null && device.getType() != BluetoothDevice.DEVICE_TYPE_LE) 
        return;
    if(action.equalsIgnoreCase(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
        int extra = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 345);
        switch (extra) {
        case BluetoothDevice.BOND_BONDED:
            BluetoothDevice bondedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(bondedDevice!= null) {
                Log.d("TAG", "Bonded device name = " + bondedDevice.getName() + " Bonded Device address ="+bondedDevice.getAddress());
            }
            break;
        }
    }


来源:https://stackoverflow.com/questions/32241184/ble-device-bluetoothdevice-getname-returns-null

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