I am unable to scan BLE Bluetooth device above API 21 I have developed an android application for smart inverter based on BLE Bluetooth my application cannot find BLE Bluetooth device when I use target SDK above 21 in gradle so please provide BLE Bluetooth library which can work all APIs.or if any solution is available so please guide me.
I have given ble bluetooth code in below :-
@override
public void onStart() {
Log.d(TAG, "++onStart");
super.onStart();
if (!mBluetoothAdapter.isEnabled() && !pendingRequestEnableBt) {
pendingRequestEnableBt = true;
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else if (mBluetoothService == null) {
//startViewRecordOnList();
setupUserInterface();
}
}
/*@Override
public void onDestroy() {
super.onDestroy();
if (mBluetoothService != null) mBluetoothService.stop();
}*/
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");
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.s2);
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;
}
}
;
I have given ble bluetooth code in below :-
@override
public void onStart() {
Log.d(TAG, "++onStart");
super.onStart();
if (!mBluetoothAdapter.isEnabled() && !pendingRequestEnableBt) {
pendingRequestEnableBt = true;
Intent enableIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else if (mBluetoothService == null) {
//startViewRecordOnList();
setupUserInterface();
}
}
/*@Override
public void onDestroy() {
super.onDestroy();
if (mBluetoothService != null) mBluetoothService.stop();
}*/
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");
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.s2);
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;
}
}
;