Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLE Scan Failed

I am using MI note 4(Android 7.0) and Moto x play (Android 7.1.1)

I am doing BLE scan in sperate service. While scanning I am getting scan response as "scan failed" Turning ON/OFF Bluetooth is not affecting in scan response. Turning ON/OFF Wifi is also not affecting in scan response.

(But in this case android inbuilt(from Settings->Bluetooth) Bluetooth scanning was working fine).

I used BLE scanner app also but that app is also not detecting BLE advertisement!

I tried with Turn ON/OFF airplane mode with this and my device is able to scan without fail.

Scan Function:

mLeScanner.startScan(filters, scanSettings, mScanCallback);

ScanCallback:

ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
               Log.e("TAG","onScanResult");
        }

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

ScanSettings:

   scanSettings = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                .build();

filters:

List<ScanFilter> filters = new ArrayList<>();
ScanFilter filter = new ScanFilter.Builder().setDeviceAddress("device address").build();
filters.add(filter);

Beacon Scan filter

ScanFilter.Builder builder = new ScanFilter.Builder();
      builder.setManufacturerData(0x004c, new byte[]{}); 

Anyone have an idea why it only worked with switching airplane mode?

will network affect for BLE scanning?

like image 615
Parth Avatar asked Jan 10 '18 10:01

Parth


1 Answers

The error code 0x02 means SCAN_FAILED_APPLICATION_REGISTRATION_FAILED(Fails to start scan as app cannot be registered). This means, before moving to scan we need to initialize Bluetooth adapter

 /**
     * Initialize BluetoothAdapter
     * Check the device has the hardware feature BLE
     * Then enable the hardware,
     */
    public boolean init(Context context) {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        return mBluetoothAdapter != null && context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
    }

Then register receiver

**
     * Register GATT update receiver
     */
    private void registerServiceReceiver() {
        this.registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
        registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
    }

The service initialization method also including in the answer. The Service creation is optional.

  /**
     * Initialize Bluetooth service.
     */
    public void initBLEService(Context context) {
        try {
            this.mContext = context;

            if (mBLEService == null) {
                Intent gattServiceIntent = new Intent(mContext, BLEService.class);

                if (this.mContext != null) {
                    isBind = mContext.bindService(gattServiceIntent, mServiceConnection, mContext.BIND_AUTO_CREATE);
                }

            }

        } catch (Exception e) {
            AppLog.logError(TAG, e.getMessage());
        }
    }

I hope you have already added permission in the manifest given below

<uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />

I hope this will help you.

like image 73
Nithinjith Avatar answered Oct 14 '22 16:10

Nithinjith