getBluetoothLeAdvertiser() returns 空
getBluetoothLeAdvertiser() returns null
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
本returns 无效。我试过 API 21 和 API 23 设备,但结果相同。我不知道我错过了什么?
应用程序构建和运行都很好,当然直到广告商被使用并且应用程序崩溃。
感谢您提供的任何帮助! :)
如果您查看开发者文档,link here。
您会看到在以下情况下返回了空对象:
Returns a BluetoothLeAdvertiser object for Bluetooth LE Advertising operations. Will return null if Bluetooth is turned off or if Bluetooth LE Advertising is not supported on this device.
如果您不确定设备是否支持蓝牙,您应该检查系统返回的 BluetoothAdapter
是否为 null
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
然后他们建议你先打电话isMultipleAdvertisementSupported()
看看是否支持。
if(!mBluetoothAdapter.isMultipleAdvertisementSupported()){
//Device does not support Bluetooth LE
}
如果它支持 BLE,您必须检查蓝牙是否已启用,如果未启用,请告知用户并解决它。
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
这应该涵盖适配器 null
的大部分时间
开始扫描前检查 mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
本returns 无效。我试过 API 21 和 API 23 设备,但结果相同。我不知道我错过了什么?
应用程序构建和运行都很好,当然直到广告商被使用并且应用程序崩溃。
感谢您提供的任何帮助! :)
如果您查看开发者文档,link here。 您会看到在以下情况下返回了空对象:
Returns a BluetoothLeAdvertiser object for Bluetooth LE Advertising operations. Will return null if Bluetooth is turned off or if Bluetooth LE Advertising is not supported on this device.
如果您不确定设备是否支持蓝牙,您应该检查系统返回的 BluetoothAdapter
是否为 null
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
然后他们建议你先打电话isMultipleAdvertisementSupported()
看看是否支持。
if(!mBluetoothAdapter.isMultipleAdvertisementSupported()){
//Device does not support Bluetooth LE
}
如果它支持 BLE,您必须检查蓝牙是否已启用,如果未启用,请告知用户并解决它。
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
这应该涵盖适配器 null
开始扫描前检查 mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()