我知道这个问题很奇怪,但它适用于一个研究项目。我从Google Play商店下载的任何允许蓝牙LE扫描的应用都不起作用:没有结果。
我继续开发自己的应用程序,以快速测试执行LE扫描的两种方式:
1)使用Prelollipop扫描
final BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
final boolean result = btAdapter.startLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Toast.makeText(getApplicationContext(), "Result = " + device.getName(), Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(this, "result is = " + result, Toast.LENGTH_LONG).show();
执行此代码后打印的日志:
D / BluetoothAdapter:startLeScan():null
D / BluetoothAdapter:STATE_ON
D / BluetoothLeScanner:onClientRegistered() - status = 0 clientIf = 5
2)使用Lollipop +扫描:
final BluetoothLeScanner leScanner = btAdapter.getBluetoothLeScanner();
final ScanSettings scanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(0).build();
leScanner.startScan(null, scanSettings, new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Toast.makeText(getApplicationContext(), "Result = " + result.getDevice().getName(), Toast.LENGTH_SHORT).show();
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Toast.makeText(getApplicationContext(), "Failed = " + errorCode, Toast.LENGTH_SHORT).show();
}
});
执行此代码后打印的日志:
D / BluetoothAdapter:STATE_ON
D / BluetoothLeScanner:onClientRegistered() - status = 0 clientIf = 5
在所有情况下,应用程序在运行时请求ACCESS_COARSE_LOCATION
,我的Android TV运行Marshmallow。在Android设置中启用了位置(高精度),我使用Google地图使用Google Play服务获取我的位置(从而在通知栏上显示位置图标)。
最终的结果是我根本没有得到任何结果。是否有可能不支持LE?