Inside an android Service I connect to the Microsoft Band 2 and read data from all the sensors.(I want to receive data even if the screen is locked).
However, after a while I stop receiving any data (no more callback are called). Also, there is not event coming on the connection callback - where I should expect connection states changes to be signaled.
I should also mention that I am registering for all possible sensor events.
My code is similar to the one in the documentation examples, but I can provide snippets if useful (the documentation contains samples for connecting inside activities but it should work the same).
Has anybody encountered this issue or a similar behavior?
You should probably enable WakeLock. I had my MBand2 transfer realtime data (via a Service) throughout the day/night with no data loss or connection issues.
Related
I'm currently making an app to exchange data between phone and a bluetooth device. I need this exchange to be in the background so when the device send some data, the app can read it and act from it, whenever the app is active or not.
I already establish some read and write data in the app, but only when it is active for the user. Now I want to take the next level and make the same read and write data but in the background, so the connection between the devices is establish and maintain.
I already know about jobschedule and service. Also, I know that, with the new versions of Android OS, services are killed by some time passed or in Doze mode. Other things I found out was to use threads in the background, but I'm still searching for that. But in the end, I'm still a newbie and maybe I'm missing out some information or I couldn't understand the concepts in their fullness, because I can't see yet how to make a process in background that could react upon a message received from the device or react upon a message received from the phone and create this exchange data.
In all, this raised me a question: how to have this kind of task in the background which read and write data from bluetooth connection between phone and a device? Is it jobschedule? Threads? Or there is one that I'm missing out?
Yet, in SO exist some questions about this related topic, some are outdate about the killing service which android does now, but if this question is a repeated one, please link me one that can solve this problem. I'll be gratefull.
Side note: I would like to know if there is a way to turn this task on and off by some condition or by the user's preference.
EDIT:
Some questions I found:
How to keep Bluetooth connection background? - This one is recent, but I don't know if this service will not be killed by the system.
Android 8.0 background services for bluetooth device - This is recent too and it has in his answer the bluetooth connection could be killed too.
I started with a application where you can chat.
Now im in the position to start with the chat.
The problem I'm facing is that I don't want to use
resources from "outside". With outside I mean:
Firebase, Socket.io and so on.
I do simply rent a webspace. And I'm asking you now,
how is it possible to realize an live chat without
using extern services like firebase.
Is it possible with only using an Webspace?
What is required to make an live chat?
And there comes the second question:
How do I realize to stay connected to a server to check if there is a new message without using much battery or network ressources?
I'm not asking without hardly trying by my self.
Two days ago I started with the research of possibility, but I didn't found anything which would work I guess.
Thanks folks...
You need to connect to the Web Server using a Socket and keep that connection open to receive new messages with little delay (see for example http://srchea.com/build-a-real-time-application-using-html5-websockets) This keeps the phone active and uses much battery.
The very purpose of Firebase is to bundle this work for all services which need this type of communication (E-Mail, Push messages of newspapers, Chats) such that the phone only has to query one server. Therefore, I see no way for you to find another solution which uses little battery.
I am trying to implement registration using firebase, but somehow when connection breaks,onCancelled() is not getting called.I tried working with onDisconnect() but no success.Finally i landed up calling a php wrapper for firebase and got rid of this problem.The wrapper i used is https://github.com/ktamas77/firebase-php. Is this wrong way of implementing because there are 2 server calls(->php->firebase) involved which will eventually make my application slow or there is an alternate to time out.
You should really explore why your onDisconnect() is not working as it's reliable and works well.
If you really want to monitor your connection status, look into the .info/connected special location in Firebase.
How to build a user presence system
Managing Presence
about 1/2 way down in the Detecting Connection State section.
We have a singleton that monitors connection status and when the connection is lost all of our classes are notified so they can take appropriate action.
I want to develop an Android application that satisfies the following specifications:
Record data from a sensor (for example the accelerometer) at an
approximate rate of 10-30 Hz.
Upload this raw data to a remote server (for example using TCP
sockets).
The user interface should be minimum, just a pair of buttons to start
or stop the recording and transmission of the data.
All the process should be unnoticeable for the user and keep working
when the screen goes off for several hours.
Battery life is not critical (it should last several hours).
Vision: I would like to analyse in quasi-real time the sensor measurements of a group of users without their intervention (apart from starting the application).
After some research, I could manage to perform these tasks separately and inefficiently. I've tried classes such as Service and IntentService, SensorEventListener, etc. I don't know if I should use Thread, Service or IntentService for each task. Specifically, I have serious problems to communicate them.
My questions:
What class(es) do you recommend to use in order to solve this
problem?
What task should be done on each of them?
If the tasks are performed in different entities (threads, services,
intentservices, etc.), how should I intercommunicate them (I'm
thinking about the recording and uploading tasks)?
I am asking for the best-practice structure to solve my problem. You do not need to go into details in terms of developing/coding the solution.
Thank you very much and feel free to ask if something is not clear enough.
David
UPDATE:
After some more research and thanks to DROIDcoder, I manage to design a skeleton for my app:
Main UI: Activity object presenting two buttons (start/stop) that
will launch a Service with the usual startService/stopService methods
Background: Service object
Awake when screen goes off: the service requests a power lock in onCreate() and releases the power lock in onDestroy(). Find more info here: https://developer.android.com/training/scheduling/wakelock.html#cpu
Log sensor values: implement SensorEventListener in the Service as usual
Upload sensor values to server: use AsyncTask in the service as described here: How to use AsyncTask
Data formatting and transmission: use GSON library + HttpClient as described here: How to send POST request in JSON using HTTPClient?
Testing: use the website http://www.jsontest.com/ to test the JSON queries
As this is only a proposition, I add it as an edition. I will post a detailed answer when the whole system works.
The questions remains: can you think about a better design for the application?
Thanks again!
Finally what I have done:
Issue 1: record data from a sensor on the background for a long period of time.
Solved using the class Service to initialize the sensor and listen for callbacks.
Issue 2: communicate the Activity class holding the UI with the Service class.
Solved using the startService(Intent myMessage) method from the Activity class combined with a switch in the onStartCommand() method from the Service class to classify the message.
Issue 3: communicate the Service class with the Activity class.
Solved registering a custom BroadcastReceiver in the Activity and sending Intents from the Service. I've used it to update a progress bar (in the Activity) during the file uploading (in the Service). An exceptional tutorial can be found here.
Issue 4: upload data to a remote server.
Solved using AsyncTask inside the Service like in this site.
here are my suggestion
Upload this raw data to a remote server
You can use JSON parsing for server communications. you will use AsynTask(Background Thread) for background data uploading
All the process should be unnoticeable for the user and keep working when the screen goes off for several hours.
You should use services for background long term processing
I am getting started with BLE development. I am trying to dig into the source code of a SensorTag Android app. But my small brain is having some difficulties in understanding the callbacks conceptually. According to the the callback explained by Wikipedia, it is a routine called within another routine upon some trigger. I would like to know how callbacks are applied in the context of BLE. For example, why reading and writing a characteristics on BLE server need callbacks? Thanks!
They need callbacks because all of BLE is asynchronous. So, you can request to read an attribute, some X other events can occur, and then the data you requested shows up. Unless you want your program to freeze until it gets the data you requested, you have to postpone processing the result with a callback.