Receive data and show them in a Textview - java

The data i receive with my smartphone through Bluetooth LE occurs in this method in my service class
public void onCharacteristicRead(BluetoothGattCharacteristic charac, int status)
{
UUID charUuid = charac.getUuid();
Bundle mBundle = new Bundle();
Message msg = Message.obtain(mActivityHandler, HRP_VALUE_MSG);
Log.i(TAG, "onCharacteristicRead");
if (charUuid.equals(BODY_SENSOR_LOCATION))
mBundle.putByteArray(BSL_VALUE, charac.getValue());
msg.setData(mBundle);
msg.sendToTarget();
}
The Handler in the activity class is contructed like this:
private Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HRPService.HRP_VALUE_MSG:
Log.d(TAG, "mHandler.HRP_VALUE_MSG");
Bundle data1 = msg.getData();
final byte[] bslval = data1.getByteArray(HRPService.BSL_VALUE);
runOnUiThread(new Runnable()
{
public void run()
{
if (bslval != null)
{
try
{
Log.i(TAG, "BYTE BSL VAL =" + bslval[0]);
TextView bsltv = (TextView) findViewById(R.id.BodySensorLocation);
bsltv.setText("\t" + mContext.getString(R.string.BodySensorLocation)
+ getBodySensorLocation(bslval[0]));
}
catch (Exception e)
{
Log.e(TAG, e.toString());
}
}
}
});
default:
super.handleMessage(msg);
}
}
};
Can someone tell med the relationship between those two methods ?. I receive an array of data from the remote device, and i want the data to be shown on the Textview "bsltv". How do i do this ?.
Thanks in advance

I created a class called BLEGattCallback which receives the updates. In this class I implemented the interface which you are mentioned. To receive and display the data I just
send and Intent with the needed informations put into a Bundle. Just create a BroadcastReceiver in your Activity and register it for the Action. Read all data from the Bundle and display it by calling TextView.setText(String).
The UUIDManager is just a class I created to translate the Characteristic into a readable String.
#Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if(status == BluetoothGatt.GATT_SUCCESS) {
Log.d(CLASSNAME, "onCharacteristicRead for Characteristic: " + characteristic.getUuid().toString());
Log.d(CLASSNAME, "onCharacteristicRead for: " + UUIDManager.getCharacteristic(characteristic.getUuid().toString()));
Intent intent = new Intent();
intent.setAction(ACTION_READ_DATA_AVAILABLE);
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data) {
stringBuilder.append(String.format("%02X ", byteChar));
}
Bundle extras = new Bundle();
extras.putString(EXTRA_CHARACTERISTIC_UUID, characteristic.getUuid().toString());
extras.putString(EXTRA_STRING_DATA, stringBuilder.toString());
extras.putByteArray(EXTRA_RAW_DATA, characteristic.getValue());
extras.putString(EXTRA_DEVICE_ADDRESS, gatt.getDevice().getAddress());
intent.putExtras(extras);
}
m_Context.sendBroadcast(intent);
} else {
Log.e(CLASSNAME, "onCharacteristicRead was not successfull!");
}
}

Related

How to pass data from Java Activity to react native?

I am unable to pass data from java activity to react native.
I am processing a card payment, and when the payment is done, the response is stored in a variable called message. I need to pass this message to my react native code.
// Java module, the data I want to pass is in "message"
public class HelloWorldModule extends ReactContextBaseJavaModule implements ActivityEventListener{
Activity activity;
ReactApplicationContext reactContext;
public HelloWorldModule(ReactApplicationContext reactContext,Activity activity) {
super(reactContext); //required by React Native
this.reactContext= reactContext;
this.activity= activity;
reactContext.addActivityEventListener(this); //Register this native module as Activity result listener
}
#Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// ReactApplicationContext reactContext = this.getReactNativeHost().getReactInstanceManager().getCurrentReactApplicationContext();
// reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
// reactContext
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit('message', message);
/*
* We advise you to do a further verification of transaction's details on your server to be
* sure everything checks out before providing service or goods.
*/
if (requestCode == RaveConstants.RAVE_REQUEST_CODE && data != null) {
String message = data.getStringExtra("response");
// Log.e("RAVE",message);
if (resultCode == RavePayActivity.RESULT_SUCCESS) {
Toast.makeText(activity, "SUCCESS " + message, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RavePayActivity.RESULT_ERROR) {
Toast.makeText(activity, "ERROR " + message, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RavePayActivity.RESULT_CANCELLED) {
Toast.makeText(activity, "CANCELLED " + message, Toast.LENGTH_SHORT).show();
}
}
// else {
// super.onActivityResult(activity, requestCode, resultCode, data);
// }
}
// #Override
// public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// Toast.makeText( activity , "hello", Toast.LENGTH_SHORT).show();
// }
#Override
public void onNewIntent(Intent intent) {
}
#Override
//getName is required to define the name of the module represented in JavaScript
public String getName() {
return "HelloWorld";
}
#ReactMethod
public void sayHi(Callback errorCallback, Callback successCallback) {
try{
int amount = 30;//call.argument("amount");
String narration = "Payment for soup";//call.argument("nara");
String countryCode = "NG"; //call.argument("countryCode");
String currency = "NGN"; //call.argument("currency");
String amountText = "50";//call.argument("amountText");
String email = "*****#yahoo.com";//call.argument("email");
String name = "Ubanna Danny";//call.argument("name");
String paymentId = "a98sjkhdjdu";//call.argument("paymentId");
String key ="FLWPUBK-****-X";
String encryptionKey = "****";
new RavePayManager(activity).setAmount(Double.parseDouble(String.valueOf(amount)))
.setCountry(countryCode)
.setCurrency(currency)
.setEmail(email)
.setfName(name)
.setlName("")
.setNarration(narration)
.setPublicKey(key)
.setEncryptionKey(encryptionKey)
.setTxRef(paymentId)
.acceptMpesaPayments(false)
.acceptAccountPayments(true)
.acceptCardPayments(true)
.acceptGHMobileMoneyPayments(false)
.onStagingEnv(false)
.allowSaveCardFeature(true)
.initialize();
} catch (IllegalViewOperationException e) {
errorCallback.invoke(e.getMessage());
}
}
}
// React native code
// async function to call the Java native method
async sayHiFromJava() {
HelloWorld.sayHi( (err) => {console.log(err)}, (msg) => {console.log(msg)} );
}
Please help.
use device emitter to send data from native to react native
in on activity result add following code
ReactContext context = this.getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit('message', message);
in react native add Dives emiter listener
import {DeviceEventEmitter} from 'react-native';
EmitterModule.addListener('message', (message) => {
console.log(message);
};

Already managing a GoogleApiClient with id 0 error despite implementing onPause()

When clicked on a Cardview, my application will display a start an activity which will display nearby places in a Recycler view. But whenever I clicked on the cardview I'm having the error shown below. Miraculously when I comment out nearByPlace("restaurant") in NearbyActivity.java the program does not crash. So is it possible because the nearByPlace("restaurant") is also using the same id and how do i fix it
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine/com.example.android.sunshine.MainActivity}: java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
Error Logcat
ItemOneFragment.Java
private void init() {
mGeoDataClient = Places.getGeoDataClient(getActivity(), null);
// Construct a PlaceDetectionClient.
mPlaceDetectionClient = Places.getPlaceDetectionClient(getActivity(), null);
mGoogleApiClient = new GoogleApiClient
.Builder(getActivity())
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(getActivity(), 0, this)
.build();
SessionData.setSessionId("0");
mSearchText.setOnItemClickListener(mAutocompleteClickListener);
placeAutocompleteAdapter = new PlaceAutocompleteAdapter(getActivity(), mGeoDataClient, LAT_LNG_BOUNDS, null);
mSearchText.setAdapter(placeAutocompleteAdapter);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
cardShopMall = (CardView) getView().findViewById(R.id.textViewShopMall);
cardShopMall.setOnClickListener(cardShopMallOnClickListener);
if (isServicesOK()) {
getLocationPermission();
}
if (mLocationPermissionsGranted) {
getDeviceLocation();
init();
}
}
CardView.OnClickListener cardShopMallOnClickListener = new CardView.OnClickListener(){
#Override
public void onClick(View view) {
String type = "shopping_mall";
Intent i = new Intent(getContext(),NearbyActivity.class);
i.putExtra("type",type);
i.putExtra("latitude",latitude);
i.putExtra("longitude",longitude);
startActivity(i);
}
};
NearbyActivity.Java
IGoogleAPIService mServiceNear;
private GoogleApiClient mGoogleApiClient;
public NearbyActivity() {
Retrofit retrofit1 = RetrofitClient2.getClient("https://maps.googleapis.com/");
mServiceNear = retrofit1.create(IGoogleAPIService.class);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, 2 ,this)
.build();
Intent intent = getIntent();
String type = intent.getExtras().getString("type");
latitude = intent.getExtras().getDouble("latitude");
longitude = intent.getExtras().getDouble("longitude");
try {
nearByPlace("restaurant");
} catch (Exception e) {
e.printStackTrace();
}
}
private void nearByPlace(final String type) {
String url = getUrl(latitude,longitude,type);
Log.d(TAG,"underURL# nearByPlace : " + url);
mServiceNear.getNearByPlaces(url)
.enqueue(new Callback<MyPlaces>() {
#Override
public void onResponse(Call<MyPlaces> call, retrofit2.Response<MyPlaces> response) {
try {
if (response.isSuccessful()){
Log.d(TAG,"responnse : ok");
try {
if (response.body() != null){
for (int i=0;i<response.body().getResults().length;i++){
Results googlePlace = response.body().getResults()[i];
String placeName = googlePlace.getName();
String vicinity = googlePlace.getVicinity();
Log.d(TAG,"nearByPlaces: " + placeName);
ListItem item = new ListItem(
googlePlace.getName(),
googlePlace.getVicinity()
);
listItems.add(item);
}
}else {
Toast.makeText(getApplicationContext(),"No Nearby " + type,Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
adapter = new MyAdapter(listItems,NearbyActivity.this);
recyclerView.setAdapter(adapter);
}
}catch (Exception e){
e.getMessage();
}
}
#Override
public void onFailure(Call<MyPlaces> call, Throwable t) {
}
});
}
private String getUrl(double latitude, double longitude, String restaurant) {
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location="+latitude+","+longitude);
googlePlacesUrl.append("&radius="+5000);
googlePlacesUrl.append("&type="+restaurant);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key="+getResources().getString(R.string.GoogleAPiKey));
Log.d(TAG,"getURL : " + googlePlacesUrl.toString());
return googlePlacesUrl.toString();
}
#Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.stopAutoManage(this);
mGoogleApiClient.disconnect();
}
}
Yes you have correctly implemented onPause for your Activity but not for your Fragment.
In your init method inside your Fragment you create a new GoogleApiClient. This differs from the one in your Activity, in which - as mentioned before - you correctly implemented onPause.
Therefore the solution to your issue should be implementing onPause for your Fragment.
I hope this will resolve your issue

Difficulty sending data on the Broadcast receiver through Intent

I have a Touch Mobile Computer with a barcode scanner.
I'm trying to write an application that scans a barcode and imports data from the DB into the device. In order to use the scanner I use a broadcast receiver.
On the scan activity screen, there are a few barcodes to scan. I set the intent to transfer information from which edittext the scan was performed (using putextra). The broadcast receiver receives the scan the action but the input of the putextra is not exist (The handle variable gets a null value [Attached picture]).
I would be happy to help with what I'm doing wrong.
Activity Class:
public class MoveItemActivity extends AppCompatActivity {
private EditText item;
private EditText to;
private boolean mIsRegisterReceiver = false;
private BroadcastReceiver mBarcodeReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_move_item);
item = (EditText) this.findViewById(R.id.itemInEditText);
item.setOnFocusChangeListener(mEditText);
to = (EditText) this.findViewById(R.id.toInEditText);
to.setOnFocusChangeListener(mEditText);
this.registerReceiver();
}
EditText.OnFocusChangeListener mEditText = new EditText.OnFocusChangeListener(){
#Override
public void onFocusChange(View v, boolean hasFocus) {
Intent intent = new Intent();
switch (v.getId()){
case R.id.itemInEditText:
if (!hasFocus){
new CheckItem(MoveItemActivity.this).execute(item.getText().toString());
break;
}
else {
intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "item");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
break;
}
case R.id.toInEditText:
if (!hasFocus){
new CheckLocation().execute(to.getText().toString());
break;
}
else
{
intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "to");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
break;
}
}
}
};
private void registerReceiver() {
if (mIsRegisterReceiver)
return;
IntentFilter filter = new IntentFilter();
filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA);
filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS);
filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_FAILED);
filter.addAction(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_GET_STATUS);
mBarcodeReceiver = new BarcodeController();
registerReceiver(mBarcodeReceiver, filter);
mIsRegisterReceiver = true;
}
BroadcastReceiver Class:
public class BarcodeController extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int mBarcodeHandle = -1;
if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA)) {
String handle = intent.getExtras().getString(BarcodeControllerConstants.EXTRA_HANDLE);
byte[] data = intent.getByteArrayExtra(BarcodeControllerConstants.EXTRA_BARCODE_DECODING_DATA);
String result = null;
if (data != null) {
result = new String(data);
Intent i = new Intent(context, MoveItemActivity.class);
i.putExtra(handle,result );
context.startActivity(i);
}
} else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS)) {
mBarcodeHandle = intent.getIntExtra(BarcodeControllerConstants.EXTRA_HANDLE, 0);
System.out.println("ACTION_BARCODE_CALLBACK_REQUEST_SUCCESS:" + mBarcodeHandle);
} else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_REQUEST_FAILED)) {
int result = intent.getIntExtra(BarcodeControllerConstants.EXTRA_INT_DATA2, 0);
System.out.println("ACTION_BARCODE_CALLBACK_REQUEST_FAILED:" + result);
} else if (action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_GET_STATUS)) {
int status = intent.getIntExtra(BarcodeControllerConstants.EXTRA_INT_DATA2, 0);
System.out.println("ACTION_BARCODE_CALLBACK_GET_STATUS:" + status);
}
}
}
The item that you're sending in broadcast has action:
intent.setAction(BarcodeControllerConstants.ACTION_BARCODE_OPEN);
But in onReceive() you're checking different action:
action.equals(BarcodeControllerConstants.ACTION_BARCODE_CALLBACK_DECODING_DATA)
Are you sure this is the same intent that the one you're putting extras to?
intent.putExtra(BarcodeControllerConstants.EXTRA_HANDLE, "item");
You don't even register BarcodeControllerConstants.ACTION_BARCODE_OPEN action in your BarcodeController broadcast, so I think it's not received.

Broadcast receiver leaked

I am trying to send a broadcast receiver from a service and i have a issue, the receiver leak and i don't know why.
Here is the code:
public class CameraCapture extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 30;
String URL;
VolleyService mVolleyService;
IResult mResultCallback = null;
final String POSTREQUEST = "POSTCALL";
Map<String, String> params;
String token;
BroadcastReceiver receiver;
IntentFilter filter;
MyReceiver reciver;
boolean mBounded;
GoogleLocation mlocation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
token = checkForToken();
URL = "http://10.0.2.2:3000/fotos/Tulipa";
filter = new IntentFilter("com.myapp.LOCATION_CHANGED");
reciver = new MyReceiver();
registerReceiver(reciver,filter);
String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
File imageFile = new File(imageFilePath);
Uri imageFileUri = Uri.fromFile(imageFile); // convert path to Uri
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_IMAGE_CAPTURE);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, GoogleLocation.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
}
public void onResume() {
super.onResume();
Log.d("RESUME","RESUME");
reciver = new MyReceiver();
registerReceiver(reciver, filter);
}
public void onPause() {
super.onPause();
if(reciver != null){
unregisterReceiver(reciver);
reciver= null;
}
}
public void onStop() {
super.onStop();
if(mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
private byte[] encodeImage(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
return b;
}
private void sendImage(byte[] b) {
ImageStore.getInstance().setCapturedPhotoData(b);
mlocation.getBroadcastData();
Intent i = new Intent(CameraCapture.this, SimiliarPhotos.class);
startActivity(i);
finish();
//inicialize a map with pair key value
//params = new HashMap<String, String>();
// Add form fields to the map
//GoogleLocation l = new GoogleLocation(this);
//l.getPosition();
//Log.d("myLat",String.valueOf(l.getLat()));
//params.put("base64", encodedImage);
//params.put("token",token);
//Log.d("latitudeOP",String.valueOf(l.getLat()));
//JSONObject sendObj = new JSONObject(params);
//initVolleyCallback();
//mVolleyService = new VolleyService(mResultCallback, this);
//mVolleyService.postDataVolley(POSTREQUEST, URL, sendObj);
}
public void showToast(String message) {
Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
toast.show();
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 30: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(CameraCapture.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
void initVolleyCallback() {
mResultCallback = new IResult() {
#Override
public void notifySuccess(String requestType, JSONObject response) {
}
#Override
public void notifySuccess(String requestType, JSONArray response) {
}
#Override
public void notifyError(String requestType, VolleyError error) {
String body;
if (error.networkResponse.data != null) {
String statusCode = String.valueOf(error.networkResponse.statusCode);
try {
body = new String(error.networkResponse.data, "UTF-8");
JSONObject jsonObj = new JSONObject(body);
Log.d("body", String.valueOf(jsonObj.get("message")));
showToast(String.valueOf(jsonObj.get("message")));
} catch (UnsupportedEncodingException e) {
showToast("You need to connect to the internet!");
} catch (JSONException e) {
Log.d("json:", "problems decoding jsonObj");
}
}
}
};
}
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
mlocation = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
GoogleLocation.LocalBinder mLocalBinder = (GoogleLocation.LocalBinder)service;
mlocation = mLocalBinder.getServerInstance();
}
};
as you guys can see i register the receiver 2 times, oncreate and onResume, and then i destroy it onStop.
The problem is you are registering it twice. remove the code from onCreate and keep it only in onResume. Also if you are registering it in onResume then unRegister it in onPause to match the lifecycle events properly.
Always register receiver in onStart() and unregister in onStop().
Since you registering receiver in onCreate(), you have to unregister in onDestroy() as well as there is a chance that activity ends up with only onCreate() and onDestroy() call backs.
Do not forget to unregister a dynamically registered receiver by using Context.unregisterReceiver() method. If you forget this, the Android system reports a leaked broadcast receiver error. For instance, if you registered a receive in onResume() methods of your activity, you should unregister it in the onPause() method.

Android: Firebase Update Issue When the app is in background

Hey I m going to develop an location tracker app in which, this app in client device which constantly send it location to the firebase db.
Here the problem is that it will send the data to firebase only first 3 minutes then it wont. I don't know whats happening. ?
For that even i put a log message that log message is printed perfectly even after three minutes
Any one please help on this........!
Here i attached 3 file One BackgroundLocation: Which is the service in background which will extract the device location and call the LocationReceiver which extends broadcast receiver where it will print log message and send the data to firebase through FBSender.
Thanks in advance
BackgroundLocation.java
Which runs in Background to get the location details and call the broadcast Reveiver. LocationReveiver.java
/**
* Created by geekyint on 1/7/16.
*/
public class BackgroundLocation extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
IBinder mBinder = new LocalBinder();
private GoogleApiClient mGoogleApiClient;
private PowerManager.WakeLock mWakeLock;
private LocationRequest mlocationRequest;
//Flag for boolean request
private boolean mInProgress;
private boolean serviceAvailabe = false;
public class LocalBinder extends Binder {
public BackgroundLocation getServerInstance() {
return BackgroundLocation.this;
}
}
#Override
public void onCreate() {
super.onCreate();
mInProgress = false;
//Create the lcoation request object
mlocationRequest = LocationRequest.create();
//Use the acurecy
mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
//The INTERVAL
mlocationRequest.setInterval(Constants.UPDATE_INTERVAL);
//The FAST INTERVAL
mlocationRequest.setFastestInterval(Constants.FAST_INTERVAL);
serviceAvailabe = serviceConnected();
setUpALocationClientIfNeeded();
ComponentName receiver = new ComponentName(this, LocationReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
/*ComponentName receiver1 = new ComponentName(this, FireBaseSender.class);
PackageManager pm1 = this.getPackageManager();
pm1.setComponentEnabledSetting(receiver1,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);*/
}
private void setUpALocationClientIfNeeded() {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
}
//Create the new Connection to the client
private void buildGoogleApiClient() {
this.mGoogleApiClient = new GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
}
private boolean serviceConnected() {
//Check the google Play service availibility
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
//IF AVAILABLE
if (resultCode == ConnectionResult.SUCCESS) {
return true;
} else {
return false;
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (this.mWakeLock == null) {
this.mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
}
if (!this.mWakeLock.isHeld()) {
this.mWakeLock.acquire();
}
if (!serviceAvailabe || mGoogleApiClient.isConnected() || mInProgress) {
return START_STICKY;
}
setUpALocationClientIfNeeded();
if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() || !mInProgress) {
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Started", Constants.LOG_FILE);
mInProgress = true;
mGoogleApiClient.connect();
}
return START_STICKY;
}
#Override
public void onLocationChanged(Location location) {
String msg = Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
Log.d("debug", msg);
// Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ":" + msg, Constants.LOCATION_FILE);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public String getTime() {
SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return mDateFormat.format(new Date());
}
public void appendLog(String text, String filename) {
File logFile = new File(filename);
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onDestroy() {
// Turn off the request flag
this.mInProgress = false;
if (this.serviceAvailabe && this.mGoogleApiClient != null) {
this.mGoogleApiClient.unregisterConnectionCallbacks(this);
this.mGoogleApiClient.unregisterConnectionFailedListener(this);
this.mGoogleApiClient.disconnect();
// Destroy the current location client
this.mGoogleApiClient = null;
}
// Display the connection status
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ":
// Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
if (this.mWakeLock != null) {
this.mWakeLock.release();
this.mWakeLock = null;
}
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Stopped", Constants.LOG_FILE);
ComponentName receiver = new ComponentName(this, LocationReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
/*
ComponentName receiver1 = new ComponentName(this, FireBaseSender.class);
PackageManager pm1 = this.getPackageManager();
pm1.setComponentEnabledSetting(receiver1,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);*/
super.onDestroy();
}
/*
* Called by Location Services when the request to connect the
* client finishes successfully. At this point, you can
* request the current location or start periodic updates
*/
#Override
public void onConnected(Bundle bundle) {
// Request location updates using static settings
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Intent intent = new Intent (this, LocationReceiver.class);
PendingIntent pendingIntent = PendingIntent
.getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient,
mlocationRequest, pendingIntent);
}
/*
* Called by Location Services if the connection to the
* location client drops because of an error.
*/
#Override
public void onConnectionSuspended(int i) {
// Turn off the request flag
mInProgress = false;
// Destroy the current location client
mGoogleApiClient = null;
// Display the connection status
// Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show();
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected", Constants.LOG_FILE);
}
/*
* Called by Location Services if the attempt to
* Location Services fails.
*/
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
mInProgress = false;
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (connectionResult.hasResolution()) {
// If no resolution is available, display an error dialog
} else {
}
}
}
Here The LocationReceiver Code:
public class LocationReceiver extends BroadcastReceiver {
private String TAG = this.getClass().getSimpleName();
private LocationResult mLocationResult;
private double latitude;
private double longitude;
private double speed;
private String time;
#Override
public void onReceive(Context context, Intent intent) {
// Need to check and grab the Intent's extras like so
if(LocationResult.hasResult(intent)) {
this.mLocationResult = LocationResult.extractResult(intent);
//new SaveToFireB().insertToFireBase(mLocationResult.getLastLocation());
new FBSender().put(mLocationResult.getLastLocation());
Log.i(TAG, "Location Received: " + this.mLocationResult.toString());
String msg = String.valueOf(mLocationResult.getLastLocation().getLongitude()) + " " +
String.valueOf(mLocationResult.getLastLocation().getLatitude());
// appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ":" + msg, Constants.LOCATION_FILE);
}
}
public void appendLog(String text, String filename) {
File logFile = new File(filename);
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here Which will call FBSender to send the data to firebase.
Ther real problems comes here.
It will send the data only in first three minutes then it wont send the data to firebase
For confirmation whether the control going there or not i put log message there that log message will be printed perfectly even after 3 minutes from the start of the app
Here is FBSender.Java
public class FBSender extends Service {
private String TAG = "FBSender";
private double latitude;
private double longitude;
private double speed;
private String time;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void put (Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
time = DateFormat.getTimeInstance().format(new Date());
Log.e(TAG, "Entering the run ()");
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference reference = database.getReference("users/" + user.getUid() + "/vehicles");
Log.e(TAG, "I M in the middle");
Map mLocations = new HashMap();
mLocations.put("latitude", latitude);
mLocations.put("longitude", longitude);
mLocations.put("speed", speed);
mLocations.put("time", time);
reference.setValue(mLocations);
Log.e(TAG, "Exiting The run ()");
}
}
To get more information about why the database writes are not completing after 3 minutes, add a CompetionListener to your setValue():
reference.setValue(mLocations, new DatabaseReference.CompletionListener() {
#Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError == null) {
Log.i(TAG, "onComplete: OKAY");
} else {
Log.e(TAG, "onComplete: FAILED " + databaseError.getMessage());
}
}
});
When you hit the 3 minute mark, if the callback fires with an error, such as permission failure, you can investigate why. If it stops firing at all, that probably means you've lost connection with the Firebase server. You can monitor the connection status using a listener, as described in the documentation.

Categories