首页
学习
活动
专区
圈层
工具
发布

5.0崩溃
EN

Stack Overflow用户
提问于 2015-06-19 14:40:11
回答 2查看 1.4K关注 0票数 2

我对android和BLE有问题,每当我扫描我的应用程序崩溃时,我都不知道原因。我使用的是startLeScan(),这是原因吗?下面是我的代码示例。

这里是我初始化api 18的地方。

代码语言:javascript
复制
//API 18
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

    @Override
    public void onLeScan(final BluetoothDevice device, int rssi,
            byte[] scanRecord) {

        Sensortag sensortag = new Sensortag(device, SensortagScanner.this,
                mSensortagScannerCallback);

        synchronized (this) {
            for (Sensortag other : mDiscoveredDevices) {
                if (sensortag.getName().equals(other.getName())) {
                    Log.i("SensortagScanner",
                            "Discovered duplicate device: "
                                    + other.getName());
                    return;
                }
            }
        }

        mDiscoveredDevices.add(sensortag);
        Log.i("SensortagScanner",
                "Discovered a device named " + device.getName() + ".");
        mCallback.onSensorDiscovered(sensortag);
    }
};

下面是我初始化ScanCall的地方,以及看起来错误的地方-- java //API 21

代码语言:javascript
复制
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void initScanCallback() {

    if (scanCallback != null)
        return;
    this.scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            // Do whatever you want
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
            Log.d(TAG, "Batch scan results: ");
            for (ScanResult result : results) {
                Log.d(TAG, "Batch scan result: " + result.toString());
                // Do whatever you want
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            // scanListener.onScanFinished();
            Log.d(TAG, "Scan failed");
        }
    };
}

这是我开始扫描的地方

代码语言:javascript
复制
    @Override
    public void startScan(SensorScannerCallback callback) {

    mCallback = callback;

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        callback.onScannerUnavailable(this);
        return;
    }

    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            stopScan();
        }
    }, SCAN_PERIOD);

    Log.i("SensortagScanner", "Starting scan...");

    mScanning = true;

    // ////////////POUR API 21//////////////
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
    }

    // /////////POUR API <18////////////////
    else {
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-19 15:10:08

尝试使用Android 5的方法,如下面的片段所示。也许能帮上忙。如果没有,请显示更多的代码和崩溃发生时的错误。

首先,您需要回调,因此根据android版本正确初始化它。

代码语言:javascript
复制
private void initScanCallback(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initCallbackLollipop();
    }else{
        initScanCallbackSupport();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void initCallbackLollipop(){
    if(scanCallback != null) return;
    this.scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            //Do whatever you want
        }

        @Override
        public void onBatchScanResults(List<ScanResult> results) {
            super.onBatchScanResults(results);
            Log.d(TAG, "Batch scan results: ");
            for (ScanResult result: results){
                Log.d(TAG, "Batch scan result: " + result.toString());
                //Do whatever you want
            }
        }

        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
            Log.d(TAG, "Scan failed");
        }
    };
}

private void initScanCallbackSupport(){
    if(callback != null) return;
    this.callback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
            String address = device.toString();
            String name = device.getName();

            // parse uuids according to another stackoverflow post
            //List<UUID> uuids = parseUuids(scanRecord);

            // TODO Compare detected UUIDs with the uuidFilter
            //for(UUID discUUID : uuids)
            //    for(UUID uuid : BLEScanService.this.uuidFilter)
            //        if(discUUID.equals(uuid))
                        BLEScanService.this.scanListener.onDeviceScanned(address, name, uuids, rssi);
        }
    };
}

然后,您可以根据android版本启动扫描:

代码语言:javascript
复制
public void startScanWithServices(List<String> uuidFilters){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        scanLollipop(uuidFilters);
    }else{
        scanSupport(uuidFilters);
    }

    // Store filters for later filtering
    int size = uuidFilters.size();
    this.uuidFilter = new ArrayList<UUID>();
    for(int i = 0; i<size; i++){
        this.uuidFilter.add(UUID.fromString(uuidFilters.get(i)));
    }

    if(BuildConfig.DEBUG) Log.d(TAG, "BLE Scan started");
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void scanLollipop(List<String> uuidFilters){
    if(scanCallback == null)
        initCallbackLollipop();

    List<ScanFilter> filters = new ArrayList<ScanFilter>();

    ScanSettings settings = new ScanSettings.Builder()
            .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // or BALANCED previously
            .setReportDelay(0)
            .build();

    bluetoothAdapter.getBluetoothLeScanner().startScan(filters, settings, scanCallback);
    //bluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
}

private void scanSupport(List<String> uuidFilters){
    if(callback == null)
        initScanCallbackSupport();

    //start scan
    //boolean success = bluetoothAdapter.startLeScan(uuids, callback);

    boolean success = bluetoothAdapter.startLeScan(callback);

    //check scan success
    if(!success) {
        if(BuildConfig.DEBUG) Log.d(TAG, "BLE Scan failed");
        scanListener.onScanFinished();
    }
}

然后你可以像这样停止扫描:

代码语言:javascript
复制
public void stopScan(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Stop scan and flush pending scan
        bluetoothAdapter.getBluetoothLeScanner().flushPendingScanResults(scanCallback);
        bluetoothAdapter.getBluetoothLeScanner().stopScan(scanCallback);
    }else{
        bluetoothAdapter.stopLeScan(callback);
    }

    if(BuildConfig.DEBUG) Log.d(TAG, "BLE Scan stopted");
}
票数 5
EN

Stack Overflow用户

发布于 2015-06-19 15:32:57

检查你的清单文件。您应该拥有以下权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30940936

复制
相关文章

相似问题

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