I'm trying to make an Android app that will scan for a certain Bluetooth device as a background service. Once the phone is within a certain range of the Bluetooth device, measured by reading the RSSI, the background service will start an Activity displayed to the user. If the phone is moved out of range of the Bluetooth device, (after the RSSI value is beyond a certain threshold,) the Activity should be killed.
Here is the code for the background service:
public class BeaconScanService extends Service implements BluetoothAdapter.LeScanCallback {
private Service self = this;
public static final String UNLOCK = "unlock";
public static final String STATUS = "status";
public static final String SIGNAL = "signal";
public static final String GET_SIGNAL = "get_signal";
//desired device to find
private static final String targetMAC = "E1:BE:A8:1A:8B:A0";
//class to handle saving RSSI values and detecting Bluetooth proximity
private proxDetector proxy;
//boolean to determine if phone is in BT range
static boolean inProx = false;
private BluetoothAdapter mBluetoothAdapter;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Proximity Detector
proxy = new proxDetector();
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
/*
* We need to enforce that Bluetooth is first enabled, and take the
* user to settings to enable it if they have not done so.
*/
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
//Bluetooth is disabled
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(enableBtIntent);
}
/*
* Check for Bluetooth LE Support. In production, our manifest entry will keep this
* from installing on these devices, but this will allow test devices or other
* sideloads to report whether or not the feature exists.
*/
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "No LE Support.", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
//Begin scanning for LE devices
startScan();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
//Cancel any scans in progress
mHandler.removeCallbacks(mStopRunnable);
mHandler.removeCallbacks(mStartRunnable);
mBluetoothAdapter.stopLeScan(this);
}
private Runnable mStopRunnable = new Runnable() {
#Override
public void run() {
stopScan();
}
};
private Runnable mStartRunnable = new Runnable() {
#Override
public void run() {
startScan();
}
};
private void startScan() {
Toast.makeText(this, "Scanning", Toast.LENGTH_SHORT).show();
//Scan for Bluetooth device with specified MAC
mBluetoothAdapter.startLeScan(this);
mHandler.postDelayed(mStopRunnable, 5000);
}
private void stopScan() {
Toast.makeText(this, "Not Scanning", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.stopLeScan(this);
mHandler.postDelayed(mStartRunnable, 2500);
}
/* BluetoothAdapter.LeScanCallback */
#Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
/*
* Create a new beacon and pass it up to the main thread
*/
Toast.makeText(self, "OnLeScan", Toast.LENGTH_SHORT).show();
BT_Beacon beacon = new BT_Beacon(device.getAddress(), rssi);
mHandler.sendMessage(Message.obtain(null, 0, beacon));
}
/*
* We have a Handler to process scan results on the main thread
*/
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
Toast.makeText(self, "Handlering", Toast.LENGTH_SHORT).show();
BT_Beacon beacon = (BT_Beacon) msg.obj;
if(beacon.getAddress().equals(targetMAC)){//Only look for target device
Toast.makeText(self, String.format("%ddBm", beacon.getSignal()), Toast.LENGTH_SHORT).show();
//HANDLE PROXIMITY DETECTION
Intent i1 = new Intent(UNLOCK);
proxy.processProx(beacon.getSignal());
Intent i2 = new Intent(SIGNAL);
i2.putExtra(GET_SIGNAL, proxy.prox);
sendBroadcast(i2);
if(proxy.crossedLine()){
i1.putExtra(STATUS, 1);
sendBroadcast(i1);
inProx = true;
Intent i = new Intent(self,MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}else{
i1.putExtra(STATUS, 0);
sendBroadcast(i1);
inProx = false;
}
}
}
};
}
Ultimately I want this background service to be started on boot, but for testing purposes, I am able to create or destroy this service from an Activity that should also display the RSSI value:
public class MainActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnScan = (Button) findViewById(R.id.btnScan);
Button btnUnscan = (Button) findViewById(R.id.btnUnscan);
btnScan.setOnClickListener(this);
btnUnscan.setOnClickListener(this);
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, new IntentFilter(BeaconScanService.SIGNAL));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnScan:
Intent i = new Intent(this,BeaconScanService.class);
startService(i);
break;
case R.id.btnUnscan:
Intent i = new Intent(this,BeaconScanService.class);
stopService(i);
break;
}
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
int sig = bundle.getInt(BeaconScanService.GET_SIGNAL);
TextView rssiView = (TextView) findViewById(R.id.text_rssi);
rssiView.setText(String.format("%ddBm", sig));
}
}
};
}
This is the layout for for MainActivity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="#+id/btnScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan"/>
<Button
android:id="#+id/btnUnscan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Scan"
android:layout_below="#+id/btnScan"/>
<TextView
android:id="#+id/text_rssi"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceListItem"
android:text="dBm"
android:layout_below="#+id/btnUnscan"/>
</RelativeLayout>
in the activity that is started and ended by the BeaconScanService, the BroadcastReceiver is registered like this:
#Override
protected void onResume() {
super.onResume();
registerReceiver(beaconScanReceiver, new IntentFilter(BeaconScanService.UNLOCK));
}
and the BroadcastReceiver looks like this:
private final BroadcastReceiver beaconScanReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
int status = bundle.getInt(BeaconScanService.STATUS);
if(status == 0) {
finish();
}
}
}
};
When I run this code, I can start and stop the BeaconScanService fine, and I know the service is scanning for the Bluetooth device due to debug Toasts being displayed. As far as I can tell, the code breaks down somewhere in the Handler of the service. No RSSI value is displayed in the TextView, and MyActivity is not started when the phone is brought close enough to the Bluetooth device.
I'm not too familiar with the interprocess communication aspects of Android programming, so I'm probably doing something wrong there. Any thoughts?
Related
I'm learning to code for Android, my first project uses bluetooth so I thought I'd start there. Basically I'm trying to scan for the other available bluetooth device with the press of a button, and display its name in a textview.
I register a BroadcastReceiver (mReceiver) with an ACTION_FOUND filter. I know I'm starting discovery correctly since other devices can see me, but I can't get their names. Debugging through AS seems to show that my instance for mReceiver is null.
public class MainActivity extends AppCompatActivity {
private Scanner_Bluetooth mainBLScanner;
public BroadcastReceiver mReceiver;
public int DISCOVERY_REQUEST = 1;
public static final String[] runtimePermissions = {};
public static final int LOCATION_PERMISSION_IDENTIFIER = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonConnect = (Button) findViewById(R.id.buttonBT);
final TextView tv_name =(TextView)findViewById(R.id.tv_name);
mainBLScanner = new Scanner_Bluetooth(this, 5000, -75);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
mReceiver = new BroadcastReceiver(getApplicationContext());
registerReceiver(mReceiver, filter);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
mainBLScanner.Start();
tv_name.setText(mReceiver.getDeviceName());
}
};
buttonConnect.setOnClickListener(listener);
}
#Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
#Override
protected void onStop() {
unregisterReceiver(mReceiver);
super.onStop();
}
And this is how I implemented BroadcastReceiver
public class BroadcastReceiver extends android.content.BroadcastReceiver {
private String deviceName = "DUMMY";
private String deviceHardwareAddress;
Context activityContext;
private static final String TAG = "BroadcastReceiver";
public BroadcastReceiver(Context activityContext) {
this.activityContext = activityContext;
}
public String getDeviceName(){
return deviceName;
}
public String getAddress(){
return deviceHardwareAddress;
}
//this detects when bluetooth is on/off
#Override
public void onReceive(Context context, Intent intent) {
final String sAction = intent.getAction();
Log.d(TAG, "start onReceive: " + sAction);
if (sAction.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_LONG).show();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_LONG).show();
break;
case BluetoothAdapter.STATE_ON:
Toast.makeText(context, "Bluetooth is on", Toast.LENGTH_LONG).show();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Toast.makeText(context, "Bluetooth is turning on", Toast.LENGTH_LONG).show();
break;
default:
break;
}
if (BluetoothDevice.ACTION_FOUND.equals(sAction)) {
Log.d(TAG, " Action_Found");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context,"Device Found!!",Toast.LENGTH_LONG).show();
this.deviceName = device.getName();
this.deviceHardwareAddress = device.getAddress(); // MAC address
}
}
}}
In my manifest I have both Bluetooth and BluetoothAdmin permissions. Also Coarse & Fine access.
I'm trying to develop a simple app using Wifi Direct. The problem is I can't get a list of available peers using onPeersAvailable method. I tried the solutions mentioned here and here but no luck.There is nothing at logs, tried using Toast instead of log but nothing showed up on the screen either. Here is my Main and BroadCastReceiver classes.
Main Class:
public class MainActivity extends AppCompatActivity {
private final String TAG = this.getClass().toString();
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
Button btn_discover = (Button) findViewById(R.id.btn_discover);
btn_discover.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
/*Toast.makeText(getApplicationContext(), "Discovery is a success.",
Toast.LENGTH_SHORT).show();*/
//startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
#Override
public void onFailure(int reasonCode) {
Toast.makeText(getApplicationContext(), "Discovery is a failure "+reasonCode,
Toast.LENGTH_SHORT).show();
}
});
}
});
}
/* register the broadcast receiver with the intent values to be matched */
#Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}}
BroadCastReceiver class:
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private final String LOG_TAG = this.toString();
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
MainActivity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.i(LOG_TAG, "Wifi Direct is enabled");
} else {
Log.i(LOG_TAG, "Wifi Direct is not enabled");
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (mManager != null) {
mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
#Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
Log.i(LOG_TAG, "Found some peers!!! "+wifiP2pDeviceList.getDeviceList().size());
}
});
}
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}}
I am sure my device (2012 Nexus 7 running Android 4.4.4) supports Wifi Direct.
I think the issue is because you are creating new "PeerListListener" inside the "WiFiDirectBroadcastReceiver".
Try to add it to your main activity instead:
public class MainActivity extends AppCompatActivity implements WifiP2pManager.PeerListListener
And then add new method to you main activity to listen to available peers:
#Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
Log.i(LOG_TAG, "Found some peers!!! " + peerList.getDeviceList().size());
}
Note: Don't forget to create new listener variable instead "WiFiDirectBroadcastReceiver" and pass the main activity as a reference to it.
Hope this helps.
Currentlly I am implementing pattern lock application and I want to set limit when user enter wrong pattern many time.Example,If user enter wrong pattern at that time set limit(3 or 4 time limit) and set delay 30 second and after 30 second give permission to enter pattern.
So,If anyone know how i can do this please give idea of that.
Here this my Reciever
public class LockScreenReceiver extends DeviceAdminReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Action...","..."+action);
//If the screen was just turned on or it just booted up, start your Lock Activity
if(action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
public void onPasswordFailed(Context ctxt, Intent intent) {
DevicePolicyManager mgr = (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
int no = mgr.getCurrentFailedPasswordAttempts();
if (no >= 3) {
context.startActivity(new Intent(context,ChangeActivity.class));
}
}
}
Service
public class LockScreenService extends Service {
DeviceAdminReceiver receiver;
#Override
public IBinder onBind(Intent intent) {
return null;
}
// Register for Lockscreen event intents
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
startForeground();
return START_STICKY;
}
// Run service in foreground so it is less likely to be killed by system
private void startForeground() {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setTicker(getResources().getString(R.string.app_name))
.setContentText("Running")
.setContentIntent(null)
.setOngoing(true)
.build();
startForeground(9999,notification);
}
#Override
#SuppressWarnings("deprecation")
public void onCreate() {
KeyguardManager.KeyguardLock key;
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
//This is deprecated, but it is a simple way to disable the lockscreen in code
key = km.newKeyguardLock("IN");
key.disableKeyguard();
//Start listening for the Screen On, Screen Off, and Boot completed actions
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//Set up a receiver to listen for the Intents in this Service
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
super.onCreate();
}
#Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
Activity
public class MainActivity extends ActionBarActivity {
private Lock9View lock9View;
private static String MY_PREFS_NAME = "PatternLock";
private static String PATTERN_KEY;
SharedPreferences prefs;
Button btnChange;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(MainActivity.this, LockScreenService.class));
makeFullScreen();
setContentView(R.layout.activity_main);
btnChange = (Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(MainActivity.this,ChangeActivity.class);
startActivity(in);
}
});
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
lock9View = (Lock9View) findViewById(R.id.lock_9_view);
lock9View.setCallBack(new Lock9View.CallBack() {
#Override
public void onFinish(String password) {
PATTERN_KEY = prefs.getString("Pattern", "invalid");
if (PATTERN_KEY.equals("invalid")) {
Toast.makeText(MainActivity.this, "Options --> Create new Pattern", Toast.LENGTH_LONG).show();
} else {
if (password.equals(PATTERN_KEY)) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}else{
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.layout_toast3, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.BOTTOM,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
}
}
});
}
private void makeFullScreen() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if(Build.VERSION.SDK_INT < 19) { //View.SYSTEM_UI_FLAG_IMMERSIVE is only on API 19+
this.getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
} else {
this.getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
#Override
public void onBackPressed() {
return; //Do nothing!
}
public void unlockScreen(View view) {
//Instead of using finish(), this totally destroys the process
android.os.Process.killProcess(android.os.Process.myPid());
}
}
So,How i can achieve this...
Have a int field like failedCounter and increment it each time user inputs invalid patterns check if reached the limit then disable the input interface and have a handler to reset the value after the time delay.
int failedCount = 0;
final static int LIMIT = 5; //set your limit here
private void invalidPattern() {
if (++failedCount == LIMIT) {
//disable the input
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//reset the failed count
faildCount = 0;
//Enable the input interface here
}
}, 30000); // 30Sec delay
}
}
Use this two methods -
ScheduledThreadPoolExecutor c1;
private void IncorrectCallCounter() {
if (failedCounter>=0)
{
c1.shutdownNow();
LockScreenFor30Second();
}else
{
if (c1!=null)
c1.shutdownNow();
}
c1 = new ScheduledThreadPoolExecutor(1);
c1.schedule(new Runnable() {
#Override
public void run() {
failedCounter=0;
c1.shutdownNow();
}
}, 15, TimeUnit.SECONDS);
}
ScheduledThreadPoolExecutor c2;
private void LockScreenFor30Second() {
//Lock Screen Here
c2 = new ScheduledThreadPoolExecutor(1);
c2.schedule(new Runnable() {
#Override
public void run() {
//Unlock Screen Here
c2.shutdownNow();
}
}, 30, TimeUnit.SECONDS);
}
Declare failedCounter globally
private int failedCounter=0;
And call this method when you detect wrong pattern -
failedCounter=failedCounter+1;
IncorrectCallCounter();
If user enter wrong pattern 4 times in 15 seconds then this will call LockScreenFor30Second method. and inside LockScreenFor30Second add your code.
I am building app regarding battery indicator and i am using code from this post.
Getting battery status even when the application is closed
it is working fine when app is closed, but when an app is active or on foreground it did not work or did not send any broadcast.
This is main activity from i start service
public class Main extends Activity {
private MyService service;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (service == null) {
Intent i = new Intent(this, MyService.class);
startService(i);
}
finish();
}
}
Following is the service code.
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand");
// do not receive all available system information (it is a filter!)
final IntentFilter battChangeFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
// register our receiver
this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
return super.onStartCommand(intent, flags, startId);
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
checkBatteryLevel(intent);
}
};
private void checkBatteryLevel(Intent batteryChangeIntent) {
// some calculations
final int currLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_LEVEL, -1);
final int maxLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_SCALE, -1);
final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);
if(percentage==100)
{
Intent intent = new Intent(getBaseContext(), Last.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
// do not forget to unregister
unregisterReceiver(batteryChangeReceiver);
} }
And when following activity start i did not receive any broadcast.
public class Last extends Activity {
Button btnCancel;
Uri notification;
Ringtone r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);
notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
btnCancel = (Button) findViewById(R.id.stopsound);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
r.stop();
}
});
} }
As I understood,
when you start application in first time, you see nothing, just service is started and a broadcast receiver is registered. When battery level will be changed, the method checkBatteryLevel() is calling and the broadcast receiver will be unregistered. As result you have never received a new changing of battery level.
i am listing all paired devices,and it well work but now want to get Bluetooth signal strength of paired devices...i know it will get by use rssi but cannot implement get it continuously in my app..
plz me by giving suitable code as my code...my code is here...
public class Security extends Fragment implements OnClickListener{
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ArrayList<String> mylist = new ArrayList<String>();
//private Object ImageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.security, null);
BA = BluetoothAdapter.getDefaultAdapter();
/* starting the bluetooth*/
on(v);
pairedDevices = BA.getBondedDevices();
//length=4;
// int j=1;
for(BluetoothDevice bt : pairedDevices) {
mylist.add(bt.getName());
length=j;
j++;
bt.getBondState();
}
return v;
}
#Override
public void onResume() {
super.onResume();
// Toast.makeText(getActivity(), "On resume", Toast.LENGTH_LONG).show();
}
/*************************Bluetooth function****************************/
public void on(View view){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getActivity(),"Turned on"
,Toast.LENGTH_LONG).show();
} else{
// Toast.makeText(getActivity(),"Already on",
// Toast.LENGTH_LONG).show();
}
}
public void Discovery(View view) {
if(BA.isDiscovering()) {
BA.cancelDiscovery();
}
}
public void list(View view){
Toast.makeText(getActivity(),"Showing Paired Devices",
Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v) {
for(int j=0;j<length;j++) {
if(v.getId()==j)
Toast.makeText(getActivity(), mylist.get(j), Toast.LENGTH_LONG).show();
//hand.update(run,1000);
}
}
}
You can get the signal from following code.
Code your activity
#Override
public void onCreate(Bundle savedInstanceState) {
.....
// Registering Broadcast. this will fire when Bluetoothdevice Found
registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
}
private final BroadcastReceiver BroadcastReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
String mIntentAction = intent.getAction();
if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(mIntentAction)) {
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String mDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
}
}
};
This broadcast will execute when your device will be connected to the remote device.
There are several other action on which you can fire this broadcast. have a look at here(BluetoothDevice)
Check following link for constant reading of RSSI
Tutorial to continuously measure the Bluetooth RSSI of a connected Android device (Java)
Output of link :
Then if you want to do it in continuously, you need to run it in a service or in a thread. So that you can even add time slots or sleep (waits) measuring the RSSI.