4

我正在尝试在我的设备上使用 BLE 传输。

这是我使用的代码和输出:

// check BLE support
Log.i(TAG, "BLE supported: " + getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)); // true

// check BLE transmission support
final BluetoothManager bluetoothManager =
        (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();

Log.i(TAG, "isMultipleAdvertisementSupported: " + mBluetoothAdapter.isMultipleAdvertisementSupported()); // false
Log.i(TAG, "isOffloadedFilteringSupported: " + mBluetoothAdapter.isOffloadedFilteringSupported()); // false
Log.i(TAG, "isOffloadedScanBatchingSupported: " + mBluetoothAdapter.isOffloadedScanBatchingSupported()); // false

BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
Log.i(TAG, mBluetoothLeAdvertiser.toString()); //android.bluetooth.le.BluetoothLeAdvertiser@1c51f789

// check BLE transmission support
// android-beacon-library, https://github.com/AltBeacon/android-beacon-library
int result = BeaconTransmitter.checkTransmissionSupported(getApplicationContext());
Log.i(TAG, "ABL checkTransmissionSupported: " + result); // 0

我不明白为什么mBluetoothLeAdvertiser不是null,因为mBluetoothLeAdvertiser验证它不是false

package android.bluetooth;
// ...

public BluetoothLeAdvertiser getBluetoothLeAdvertiser() {
    if (getState() != STATE_ON) {
        return null;
    }
    if (!isMultipleAdvertisementSupported()) {
        return null;
    }
    synchronized(mLock) {
        if (sBluetoothLeAdvertiser == null) {
            sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService);
        }
    }
    return sBluetoothLeAdvertiser;
}

// ...

public boolean isMultipleAdvertisementSupported() {
    if (getState() != STATE_ON) return false;
    try {
        return mService.isMultiAdvertisementSupported();
    } catch (RemoteException e) {
        Log.e(TAG, "failed to get isMultipleAdvertisementSupported, error: ", e);
    }
    return false;
}
4

1 回答 1

14

欢迎来到同时开放源代码和封闭源代码的 Android 世界!您对上面开源BluetoothLeAdvertiser代码的分析是正确的。如果该代码在您的移动设备上运行,您将看不到顶部代码片段中的测试显示的输出。结论:第二个片段中显示的代码一定不是设备上的代码。

Android 设备 OEM 可以自由分叉源代码并对其进行修改以使其适用于他们的硬件。在这种情况下,我知道摩托罗拉在这段代码中为他们的 Moto X 和 Moto G 设备做了同样的事情。BluetoothLeAdvertiser尽管isMultipleAdvertisementSupported()返回 false ,这些设备仍返回 a 。一位摩托罗拉工程师向我解释说,他们改变了这一点,因为他们想要支持广告,尽管使用的 BLE 芯片一次只能支持一个广告。事实上,我已经验证了摩托罗拉设备可以做广告,但是如果你试图让两个广告同时出现,它就会失败。

于 2015-08-19T13:01:32.057 回答