getBluetoothLeAdvertiser()返回null

Anf*_*aje 2 android bluetooth-lowenergy android-bluetooth

BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
Run Code Online (Sandbox Code Playgroud)

返回null.我已尝试使用API​​ 21和API 23设备,但结果相同.我不知道我错过了什么?

该应用程序构建和运行正常,直到当然使用广告客户和应用程序崩溃.

我感谢您提供的任何帮助!:)

Maz*_*zze 7

如果您查看开发人员文档,请在此处链接.在下列情况下,您将看到返回null对象:

返回Bluetooth LEAdvertiser对象,用于Bluetooth LE广告操作.如果蓝牙关闭或此设备不支持蓝牙LE广告,则返回null.

如果您不确定设备是否支持蓝牙,则应检查BluetoothAdapter系统是否返回null

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
    // Device does not support Bluetooth
}
Run Code Online (Sandbox Code Playgroud)

然后他们建议你先打电话询问isMultipleAdvertisementSupported()是否支持.

if(!mBluetoothAdapter.isMultipleAdvertisementSupported()){
    //Device does not support Bluetooth LE
}
Run Code Online (Sandbox Code Playgroud)

如果它支持BLE,您必须检查是否启用了蓝牙,如果没有,请让用户知道并解决它.

if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Run Code Online (Sandbox Code Playgroud)

这应该涵盖适配器的大部分时间 null