问题
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