问题描述
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
这将返回null.我在API 21和API 23设备上进行了尝试,但结果相同.我不知道我在想什么?
This returns null. I have tried on an API 21 and on an API 23 device, but with the same result. I have no idea what I am missing?
该应用程序的构建和运行正常,直到使用广告客户并且该应用程序崩溃为止.
The app builds and runs just fine, until of course the advertiser is used and the app crashes.
感谢您提供的任何帮助! :)
I appreciate any help provided! :)
推荐答案
如果您检查了开发人员文档,请链接此处.您会看到在以下情况下返回了空对象:
If you check the developer docs, link here.You'll see that the null object is returned in the following case:
如果不确定设备是否完全支持蓝牙,则应检查系统返回的BluetoothAdapter
是否为null
If you are unsure whether the device supports Bluetooth at all you should check if the BluetoothAdapter
returned by the system is null
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
然后他们建议您致电isMultipleAdvertisementSupported()
,看看是否首先受支持.
Then they advice you to call isMultipleAdvertisementSupported()
to see if it is supported first.
if(!mBluetoothAdapter.isMultipleAdvertisementSupported()){
//Device does not support Bluetooth LE
}
如果它支持BLE,则必须检查以查看是否启用了蓝牙,如果没有启用,请使用户了解并解决.
If it supports BLE you must check to see if Bluetooth is enabled and if not, make the user aware and resolve it.
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
大多数情况下,适配器是null
That should cover most of the times the adapter is null
这篇关于getBluetoothLeAdvertiser()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!