Android Open Source - BLEMeshChat Chat Service

From Project

Back to project page BLEMeshChat.


The source code is released under:

GNU General Public License

If you think the Android project BLEMeshChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pro.dbro.ble;
//from  w ww. j  a v  a  2 s  . c om
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

 * Created by davidbrodsky on 11/4/14.
public class ChatService extends Service implements ActivityRecevingMessagesIndicator {
    public static final String TAG = "ChatService";

    private ChatApp mApp;
    private boolean mActivityRecevingMessages;

    private ChatServiceBinder mBinder;

    private Looper mBackgroundLooper;
    private BackgroundThreadHandler mBackgroundHandler;
    private Handler mForegroundHandler;

    /** Handler Messages */
    public static final int CONNECT       = 0;
    public static final int SEND_MESSAGEE = 1;
    public static final int SHUTDOWN      = 2;

    public void onCreate() {
        Log.i(TAG, "onCreate");
        mApp = new ChatApp(getApplicationContext());
        // Immediately make us available if an identity is available
        if (mApp.getPrimaryIdentity() != null) mApp.makeAvailable();

        // Start up the thread running the service.  Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block.  We also make it
        // background priority so CPU-intensive work will not disrupt our UI.
        HandlerThread thread = new HandlerThread(TAG, android.os.Process.THREAD_PRIORITY_BACKGROUND);

        // Get the HandlerThread's Looper and use it for our Handler
        mBackgroundLooper = thread.getLooper();
        mBackgroundHandler = new BackgroundThreadHandler(mBackgroundLooper);
        mForegroundHandler = new Handler(Looper.getMainLooper());

    public IBinder onBind(Intent intent) {
        if (mBinder == null) mBinder = new ChatServiceBinder();
        Log.i(TAG, "Bind service");
        return mBinder;

    public int onStartCommand (Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    /** ActivityRecevingMessagesIndicator */
    public boolean isActivityReceivingMessages() {
        return mActivityRecevingMessages;

    /** Binder through which Activities can interact with this Service */
    public class ChatServiceBinder extends Binder {

//        public ChatService getService() {
//            // Return this instance of LocalService so clients can call public methods
//            return ChatService.this;
//        }

        public void connect() {

        public void sendPublicMessageFromPrimaryIdentity(String message) {
            mBackgroundHandler.sendMessage(mBackgroundHandler.obtainMessage(SEND_MESSAGEE, message));

        public ChatApp getChatApp() {
            return mApp;

        public void shutdown() {

         * Set by Activity bound to this Service. If isActive is false, this Service
         * should post incoming messages as Notifications.
        public void setActivityReceivingMessages(boolean receivingMessages) {
            mActivityRecevingMessages = receivingMessages;
            Log.i(TAG, "setActivityRecevingMessages " + receivingMessages);

    /** Handler that processes Messages on a background thread */
    private final class BackgroundThreadHandler extends Handler {
        public BackgroundThreadHandler(Looper looper) {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CONNECT:
                    Log.i(TAG, "handling connect");
                case SEND_MESSAGEE:
                    mApp.sendPublicMessageFromPrimaryIdentity((String) msg.obj);
                case SHUTDOWN:
                    Log.i(TAG, "handling shutdown");

                    // Stop the service using the startId, so that we don't stop
                    // the service in the middle of handling another job

Java Source Code List