我正在尝试在我的设备上使用 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;
}
最佳答案
欢迎来到既开源又封闭源代码的 Android 世界!您对上述开源BluetoothLeAdvertiser
代码的分析是正确的。如果该代码在您的移动设备上运行,您将看不到顶部代码 fragment 中的测试显示的输出。 结论:第二个 fragment 中显示的代码一定不是设备上的代码。
Android 设备 OEM 可以自由 fork 源代码并对其进行修改以使其与他们的硬件一起使用。在这种情况下,我知道摩托罗拉在这段代码中为他们的 Moto X 和 Moto G 设备做了同样的事情。这些设备返回 BluetoothLeAdvertiser
,尽管 isMultipleAdvertisementSupported()
返回 false。一位摩托罗拉工程师向我解释说,他们改变了这一点,因为他们想要支持广告,尽管使用一次只能支持一个广告的 BLE 芯片。事实上,我已经验证了摩托罗拉设备可以做广告,但是如果你试图让两个广告同时播放,它就会失败。
关于android - 为什么 isMultipleAdvertisementSupported() 在 getBluetoothLeAdvertiser 返回对象时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092902/