如何在android中刷新ble广告商和ble扫描程序?

时间:2017-01-26 19:58:10

标签: android refresh bluetooth-lowenergy

我的应用程序是在android.it中使用ble进行广告和扫描需要在每天大约1小时(可能更少)的循环中进行,但现在,对于测试我试图让它更有效。 过了一段时间,我意识到它不会在没有错误的情况下永远运行。 我得到错误号2和4(一个用于广告,一个用于扫描)。 在我能看到日志中的错误之前,总会有一行如下:

  

01-26 19:59:09.509 D / BluetoothLeScanner:onClientRegistered() - status = 133 clientIf = 21

clientIf附近的数字一直在变大,直到出现错误,所以我的猜测是扫描仪/广告商正在收集有关扫描/广告设备的数据。我看到了解决方案的一些选项,例如在操作之间等待一段时间或制作mBluetoothGatt.close(); 等待没有帮助,我的应用程序没有使用gatt(我没有在应用程序中进行任何配对),所以我想也许我会再次初始化BluetoothLeScanner / BluetoothLeAdvertiser,但它并没有阻止clientIf附近的数量提高,当然错误再次发生。 有没有人有任何想法我如何“归零”clientIf附近的数字,或当然其他方式来防止错误发生?谢谢 ! 我读到的最相关的问题是:

Android stops finding BLE devices: onClientRegistered() - status=133 clientIf=0

编辑:这里我将添加ble的代码。 这是扫描码:

public class StDataCentralBle {
    private static final String TAG = ScannerFragment.class.getSimpleName();
    private static final long SCAN_PERIOD = 15000;
    private BluetoothAdapter mBluetoothAdapter;
    public BluetoothLeScanner mBluetoothLeScanner;
    private ScanCallback mScanCallback;
    private Vector<String> ScanResultArray;//intended to replace the adapter
    private Context context;

private Handler mHandler;
//todo open this:
public StDataCentralBle(Context context){
    this.context=context;


public void setBluetoothAdapter(BluetoothAdapter btAdapter) {
    this.mBluetoothAdapter = btAdapter;
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
}
public void startScanning() {
    if (mScanCallback == null && mBluetoothAdapter!=null && mBluetoothAdapter.isEnabled()) {
        Log.d(TAG, "Starting Scanning");

        // Will stop the scanning after a set time.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.v("ble_process","BT state is: "+mBluetoothAdapter.getScanMode());
                stopScanning();
            }
        }, SCAN_PERIOD);

        // Kick off a new scan.
        mScanCallback = new SampleScanCallback();
        mBluetoothLeScanner.startScan(buildScanFilters(), buildScanSettings(), mScanCallback);

        Toast.makeText(context, "started scan", Toast.LENGTH_SHORT).show();
    } else {
        Log.v("ble_process","didnt start scan");

    }
}

/**
 * Stop scanning for BLE Advertisements.
 */
public void stopScanning() {
    // Stop the scan, wipe the callback.
    if(mBluetoothLeScanner!=null && mBluetoothAdapter.isEnabled() && mBluetoothAdapter.getState()==21) {
        mBluetoothLeScanner.stopScan(mScanCallback);
        Log.v("ble_process", "stopping scanning");
    }
    mScanCallback = null;
    if(mBluetoothAdapter!=null)
        mBluetoothLeScanner=mBluetoothAdapter.getBluetoothLeScanner();
    Toast.makeText(context, "stoped scan", Toast.LENGTH_SHORT).show();
}
/**
 * Return a List of {@link ScanFilter} objects to filter by Service UUID.
 */
private List<ScanFilter> buildScanFilters() {
    List<ScanFilter> scanFilters = new ArrayList<>();

    ScanFilter.Builder builder = new ScanFilter.Builder();
    // Comment out the below line to see all BLE devices around you
    builder.setServiceUuid(Constants.Service_UUID);
    scanFilters.add(builder.build());

    return scanFilters;
}

/**
 * Return a {@link ScanSettings} object set to use low power (to preserve battery life).
 */
private ScanSettings buildScanSettings() {
    ScanSettings.Builder builder = new ScanSettings.Builder();
    builder.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
    return builder.build();
}

/**
 * Custom ScanCallback object - adds to adapter on success, displays error on failure.
 */
private class SampleScanCallback extends ScanCallback {
    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        super.onBatchScanResults(results);

        for (ScanResult result : results) {

        }

    }

    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        super.onScanResult(callbackType, result);
        Log.v("onscanResult","onscanRes");
        Toast.makeText(context.getApplicationContext(), "scan resulted ! ", Toast.LENGTH_SHORT).show();
        //get the data from advertise:
        byte[] Service_Data_bytes=result.getScanRecord().getServiceData(Constants.Service_UUID);
        if(Service_Data_bytes!=null) {
            String Service_Data = new String(Service_Data_bytes);
            Log.v("scanRes:", Service_Data);

            createNewcross(result);
        }
    }
    public void createNewcross(ScanResult scanResult){
        Log.v("createNewcross ", "new cross " );
        byte[] Service_Data_bytes=scanResult.getScanRecord().getServiceData(Constants.Service_UUID);//gilad
        String Service_Data = new String(Service_Data_bytes);


        String other_user=Service_Data ;

    }

    @Override
    public void onScanFailed(int errorCode) {
        super.onScanFailed(errorCode);
        Log.v("ble_process","scan_failed_error_code: "+errorCode);
        Toast.makeText(context.getApplicationContext(), "scan failed! ", Toast.LENGTH_SHORT).show();

        }


    }

}
}

这是操作扫描的代码:

public void start_central_ble(Context context){//scaning service
    Log.v("ble_process","start_central_ble");
    if (mBluetoothAdapter.isEnabled())
        central.setBluetoothAdapter(mBluetoothAdapter);
    central.startScanning();
}

操作它的计时器:

private class StartCentralTask extends TimerTask {
    Context ServiceContext;

    private StartCentralTask(Context context) {
        ServiceContext = context;
    }

    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {

                start_central_ble(ServiceContext);

            }
        });
    }
}

0 个答案:

没有答案