Android的BLE startDiscovery()回调,不因地点的权限发射 [英] Android BLE startDiscovery() callback is not fired even with Location permissions
问题描述
该应用程序是针对22最低版本18和按预期工作的棒棒糖。启动棉花糖的应用需要,要求精和粗权限BLE。我已经添加了他们,但回调不激发。
The app is targeted to 22 with minimum version 18 and works as expected on Lollipop. Starting Marshmallow apps need to require 'fine' and 'coarse' permissions for BLE. I've added them but the callback is not fired.
AndroidManifest:
AndroidManifest:
...
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<!-- ble -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<!-- for Android M BLE requires the following permissions too -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Discovery与UUID过滤完成的:
Discovery is done with UUID filtering:
this.adapter.startLeScan(new UUID[]{this.serviceUUID}, this.discoveryListener);
回调:
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.d(TAG, "Bluetooth device found: " + device);
foundBluetoothDevices.add(device);
}
我可以看到设备被发现,但回调不会触发:
I can see the devices are found but callback is not fired:
12-03 12:34:19.266 9003-9003/com.company.project D/FindCamerasTask﹕ Discovery started
12-03 12:34:19.266 9003-9003/com.company.project D/BluetoothAdapter﹕ startLeScan(): [Ljava.util.UUID;@dd832eb
12-03 12:34:19.268 9003-9003/com.company.project D/BluetoothAdapter﹕ STATE_ON
12-03 12:34:19.270 4362-4375/? D/BtGatt.GattService﹕ registerClient() - UUID=92d098d2-0da1-4908-8194-14c504680fae
12-03 12:34:19.271 4362-4382/? D/BtGatt.GattService﹕ onClientRegistered() - UUID=92d098d2-0da1-4908-8194-14c504680fae, clientIf=5
12-03 12:34:19.271 9003-9064/com.company.project D/BluetoothLeScanner﹕ onClientRegistered() - status=0 clientIf=5
12-03 12:34:19.271 4362-4401/? D/BtGatt.GattService﹕ start scan with filters
12-03 12:34:19.273 4362-4385/? D/BtGatt.ScanManager﹕ handling starting scan
12-03 12:34:19.288 4362-4382/? D/BtGatt.GattService﹕ onScanFilterEnableDisabled() - clientIf=5, status=0, action=1
12-03 12:34:19.288 4362-4382/? D/BtGatt.ScanManager﹕ callback done for clientIf - 5 status - 0
12-03 12:34:19.288 4362-4385/? D/BtGatt.ScanManager﹕ addFilterToController: 2
12-03 12:34:19.302 4362-4382/? D/BtGatt.GattService﹕ onScanFilterConfig() - clientIf=5, action = 0 status = 0, filterType=2, availableSpace=47
12-03 12:34:19.302 4362-4382/? D/BtGatt.ScanManager﹕ callback done for clientIf - 5 status - 0
12-03 12:34:19.302 4362-4385/? D/BtGatt.ScanManager﹕ configureFilterParamter 500 10000 1 0
12-03 12:34:19.316 4362-4382/? D/BtGatt.GattService﹕ onScanFilterParamsConfigured() - clientIf=5, status=0, action=0, availableSpace=15
12-03 12:34:19.316 4362-4382/? D/BtGatt.ScanManager﹕ callback done for clientIf - 5 status - 0
12-03 12:34:19.316 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=1
12-03 12:34:19.317 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=2 mLastConfiguredScanSetting=-2147483648
12-03 12:34:19.317 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams - scanInterval = 8000configureRegularScanParams - scanWindow = 8000
12-03 12:34:19.318 4362-4382/? D/BtGatt.GattService﹕ onScanParamSetupCompleted : 0
12-03 12:34:19.466 4362-4382/? D/bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Device-00W15380138 len=17 dev_type=2
12-03 12:34:19.701 4362-4382/? D/bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Device-20154300041 len=17 dev_type=2
12-03 12:34:21.021 4362-4382/? D/bt_btif_gattc﹕ btif_gattc_update_properties BLE device name=Device-20154300015 len=17 dev_type=2
12-03 12:34:39.278 9003-9003/com.company.project W/BleRpcConnectionFactory﹕ Discovery timeout fired (20000)
12-03 12:34:39.279 9003-9003/com.company.project D/BluetoothAdapter﹕ stopLeScan()
12-03 12:34:39.324 4362-4382/? D/BtGatt.GattService﹕ onScanFilterParamsConfigured() - clientIf=5, status=0, action=1, availableSpace=16
12-03 12:34:39.324 4362-4382/? D/BtGatt.ScanManager﹕ callback done for clientIf - 5 status - 0
12-03 12:34:39.324 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue=0
12-03 12:34:39.324 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - ScanSetting Scan mode=-2147483648 mLastConfiguredScanSetting=2
12-03 12:34:39.324 4362-4385/? D/BtGatt.ScanManager﹕ configureRegularScanParams() - queue emtpy, scan stopped
我已经检查了Android的>设置>应用程序>权限授予位置的权限。测试在Nexus 9。 有什么想法?
I've checked 'Location' permission granted in Android > Settings > Apps > Permissions. Testing on Nexus 9. Any thoughts?
PS。我试过API 21 adapter.getBluetoothLeScanner()startScan(过滤器,scanSettings,internalScanCallbackAPI21);
,仍然没有运气 - 类似的日志,但没有回调被触发。
PS. I've tried API 21 adapter.getBluetoothLeScanner().startScan(filters, scanSettings, internalScanCallbackAPI21);
and still no luck - similar log but no callback is fired.
PPS。我做了它的工作,如果设置报告延迟:
PPS. I made it working if setting report delay:
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(discoveryDelay) // 0 for immediate callback (not working for me), > 0 for batch mode
.build();
如果设置 0
立即回调仍然没有得到听众解雇了。
if setting 0
for immediate callback still not getting listener fired.
推荐答案
我必须设置报告延迟= 1回调不解雇0(立即回拨):
I had to set report delay = 1 as callback is not fired for 0 (immediate callback):
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setReportDelay(1); // '0' or not setting any value will lead to callback not fired!
.build();
随时提出更好的解决方案,而不是这个肮脏的黑客。
Feel free to suggest better solution instead of this dirty hack.
这篇关于Android的BLE startDiscovery()回调,不因地点的权限发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!