Unable to scan BLE Bluetooth device above API 21

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Feb 26, 2019, 7:25:16 PM via Website

Hello sir,
I am designing and developing BLE Bluetooth based Android Application I am displaying data on screen of mobile phone the data which I get from INVERTER after scanning BLE Bluetooth device.I am unable to scan BLE Bluetooth device above API 21 My source code and BLE library only work on below API 21 when I use target SDK 26 in gradle my application does not find any BLE Bluetooth device So please guide me how can I scan any BLE Bluetooth device above API 21 IF BLE supported library is available so please provide me so that i can add this library in my code.

this is my code which is given below
private String formatStatusMessage(int formatResId, Object obj) {
String deviceName = (String) obj;
return getString(formatResId, deviceName);
}

@Override
protected void onDestroy() {
    stopDownloading();
    super.onDestroy();
}

public void onRefresh() {
    Global.Devices.clear();
    Global.selectedDevice = null;
    try {
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);



            return;
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mStatusView.setText("Searching");
                    ivBack.setImageResource(R.drawable.grid);
                    ivBack1.setImageResource(R.drawable.batterygenus);
                    ivBack2.setImageResource(R.drawable.surja);
                    ivBack3.setImageResource(R.drawable.solargenus);
                    ivBack4.setImageResource(R.drawable.bulb);
                    progress.setTitle("Searching");
                    progress.setMessage("Scanning devices...");
                    progress.show();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        startScan();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

void stopDownloading() {
    bDownloadRunning = false;
    if (t != null) {
        if (t.isAlive()) {
            t.interrupt();
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    t = null;
}

@SuppressWarnings("deprecation")
private void startScan() {
    mBluetoothAdapter.startLeScan(leScanCallback);

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


@SuppressWarnings("deprecation")
private void stopScan() {
    mBluetoothAdapter.stopLeScan(leScanCallback);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                mStatusView.setText("Not Connected");
                But.setBackgroundResource(R.drawable.button_white_bg);
                progress.dismiss();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    if (Global.Devices.size() > 0) {
        Intent deviceIntent = new Intent(MainActivity.this, DeviceListActivity.class);
        startActivityForResult(deviceIntent, REQUEST_CONNECT_DEVICE);
    } else {
        Toast.makeText(getApplicationContext(), R.string.none_found, Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            But.setBackgroundResource(R.drawable.button_white_bg);
            if (resultCode == Activity.RESULT_OK) {
                if (Global.selectedDevice == null) return;
                mStatusView.setText("Connected");
                ivBack.setImageResource(R.drawable.grid);
                ivBack1.setImageResource(R.drawable.batterygenus);
                ivBack2.setImageResource(R.drawable.surja);
                ivBack3.setImageResource(R.drawable.solargenus);
                ivBack4.setImageResource(R.drawable.bulb);
                MediaPlayer mp1 = MediaPlayer.create(MainActivity.this, R.raw.connected);
                But.setBackgroundResource(R.drawable.button_white_bg);
                mp1.start();
                //Download Data here
                bDownloadRunning = true;
                t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        DownloadData();
                        //parameters();
                    }
                });
                t.start();
            }
            break;
    }
}

;
Be the first to answer