首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么BluetoothLEScanner不调用它的ScanCallback

为什么BluetoothLEScanner不调用它的ScanCallback
EN

Stack Overflow用户
提问于 2018-05-08 21:51:52
回答 2查看 3.7K关注 0票数 5

我现在正试图在一个基本的android应用程序中实现一个BLE扫描仪。我一直在跟踪:startLeScan replacement to current apihttps://developer.android.com/guide/topics/connectivity/bluetooth-le

不幸的是,我的BLEScanCallback类中的回调函数似乎没有被调用。我只是想看看他们中是否有人接到电话。

我调试了这个应用程序,应用程序获得了bluetoothLeScanner.stopScan(scanCallback);在run()函数中。

我在清单中添加了蓝牙、BLUETOOTH_ADMIN和ACCESS_COARSE_LOCATION。

我不太清楚我在这里做错了什么。如果能在这方面提供任何帮助,我将不胜感激。

谢谢

有关守则如下:

代码语言:javascript
复制
public class DiscoverActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;
private boolean scanning;
private Handler handler;
static int REQUEST_ENABLE_BT = 1001;
private static final long SCAN_PERIOD = 10000;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discover);
    handler = new Handler();
    scanning = false;

    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();
    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

public boolean onOptionsItemSelected(MenuItem item) {
    boolean ret;
    int id = item.getItemId();

    switch (id){
        case R.id.scan:
            Log.e("test", "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
            scanBLEDevices(true);
            ret = true;
            break;
        default:
            ret = super.onOptionsItemSelected(item);
    }

    return ret;
}

private void scanBLEDevices(final boolean enable){
    final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();

    final BLEScanCallback scanCallback = new BLEScanCallback();
    if (enable){
        // Stops scanning after a pre-defined scan period.
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                scanning = false;
                bluetoothLeScanner.stopScan(scanCallback);
            }
        }, SCAN_PERIOD);

         scanning = true;
         bluetoothLeScanner.startScan(scanCallback);
    }else{
        scanning = false;
        bluetoothLeScanner.stopScan(scanCallback);
    }
}

public class BLEScanCallback extends ScanCallback{
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        Log.e("Scan Success", "Scan Success");
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);
        Log.e("Scan Success", "Scan Success Batch");
    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Log.e("Scan Failed", "Error Code: " + errorCode);
    }
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-09 15:45:12

事实证明,尽管我从清单中添加了ACCESS_COURSE_LOCATION权限,但这对API 23+来说还不够。我认为在较低的api版本中,清单中的请求就足够了。

我不得不补充:

代码语言:javascript
复制
if (Build.VERSION.SDK_INT >= 23) {
        if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("This app needs location access");
            builder.setMessage("Please grant location access so this app can detect peripherals.");
            builder.setPositiveButton(android.R.string.ok, null);
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onDismiss(DialogInterface dialog) {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            });
            builder.show();
        }
    }
票数 10
EN

Stack Overflow用户

发布于 2022-10-27 06:54:35

如果将来有人有同样的问题,Android 12+需要Manifest.permission.ACCESS_FINE_LOCATIONManifest.permission.BLUETOOTH_SCAN扫描其他设备。Manifest.permission.ACCESS_COARSE_LOCATION已经不够用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50242755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档