I have a question. Help. I need a quick answer. (Bluetooth)

edited November 2016 in Android Mode

I am trying to make a Bluetooth communication application with Arduino. With a tool called processing, We want to inform the smartphone when any event occurs.

Can I turn on the app on my smartphone (when sending data) when I announce an event?

Can I sleep the system as follows? I would appreciate it if you could tell me what to do if possible. (For example, share source code.)

Answers

  • @gwak=== not sure to understand your question; "turning on" or "system sleeps" is not clear for me (neither for android i m afraid!) - What you can do (perhaps) is a) to create code for the blue tooth connection, which is easy with ketai lib or with the android native API;b) in the same package you create a service (local) for bluetooth, in order that it runs even if the app is on pause. In this service you add methods for your arduino messages: when there is some message the service tries to launch your activity or fragment. This one starts the service then calls finish when everything thing (connection on, service created) is ok.

  • Thank you sincerely for your kind reply. My question is, when the smartphone is in system sleeps state, I want to generate an event.

    For example, if an application is running on a smartphone, it is easy to apply. However, I do not know how to configure it when I'm in app sleep mode on my cellphone. Please give me a simple example source. Please.

  • edited November 2016

    @gwak===

    • in this case if i understand what you want you have to create by code some broadcastReceiver with a filter for ScreenOn && ScreenOff and an intent like: intent.getAction().equals(Intent.ACTION_SCREEN_OFF)

    for more details: https://developer.android.com/reference/android/content/Intent.html#ACTION_SCREEN_OFF

    then you put your todo code in the onReceive() method from your BroadCastReceiver class

    onPause() && onResume() can also be used (because when the screen is turning off on Pause() is called && when it's turning on onResume is called)

    Dont forget to unregister your receiver in onDestroy()

  • Thank you very much. I have a favor to ask. Can not you just give me a little code? Sample source, thank you.

  • edited November 2016

    @Gwak===

    without knowing what you have done till now, that is difficult; i can on ly repeat the main things to do (the code samples have been tested with Eclipse, android 6.0)

    • in your main activity (or fragment as it is with P5)

    a) you import what is needed= at least

        import android.content.BroadcastReceiver;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
    

    b) you declare your broadcast receiver:

    BroadCastReceiver= myBr;

    && create some booleans= boolean brLaunched, ScreenOn=true, ScreenOff= false;

    // ScreenOn at start is true of course!

    in your setup() you write something like:

            if(!brLaunched){
            myBr = new (ScreenBr);//in order to avoid creating too much instances for br
            }
    

    After that you have to create a class for your ScreenBr, extending broadcastReceiver

        class ScreenBr extends BroadcastReceiver {
    
    
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                    EcranOff = true;// here see the links from android i have given
                    EcranOn = false;
                } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                    EcranOff = false;
                     EcranOn = true;
                }
    

    // you can also( instead of ACTION_SCREEN...) use isInteractive(): see the docs.

                Intent i = new Intent(context, ServiceBr.class);
                i.putExtra("screen_state", EcranOff);// in order that it broadcast the new //state when the screen goes out....
                context.startService(i);
            }
    
        }
    

    /// then you create the Service class extending Service (which is called by the intent) :

    class ServiceBr extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
    
            registerReceiver(mReceiver, filter);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
          lanceService= true;
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            println("lanceservice======"+ lanceService);
            if (!screenOn) {
                ............................
            } else {
                // your code
    
            }
        }
    
      @Override
      public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
      }
    
    
    }
    

    /// and you add onPause(), onResume() and onDestroy() with onPause === ScreenOn= false; ScreenOff= true; the reverse with onResume() and with onDestroy() you Unregister your BroadcastReceiver...

    Finally look at your manifest:

    in the application tag you have to declare your receiver and your service

  • I understand the intent of your coding. But is this sample source not running in processing?

    Is it not processing?

    Or is it in processing if it works in Eclipse?

    As you can see, the example does not seem to work in processing.

  • @Gwak===

    java is java for all...

  • Dear akenaton, Thank you so much for your kind reply.

    But, as you explained, I worked on the processing. It does not work. I do not know why.

    I want to notify my smartphone when I receive an event signal through Bluetooth or when the Bluetooth signal disappears.

    The screen of the smartphone is turned off, but the application is turned on for each event.

    I want to make this idea into processing, but it does not work. I do not know what to do.

  • edited November 2016

    error

    Example)
    (1) Write Code : '<'a href.... (2) Run : Error

  • @GWAK: that is the way for the forum to translate "@Override"!!!!

  • edited November 2016
        import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    
    BroadCastReceiver myBr;
    
    void setup(){
    }
    
    void draw(){
    
    }
    
    class ScreenBr extends BroadcastReceiver { 
    
       @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                EcranOff = true;// here see the links from android i have given
                EcranOn = false;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                EcranOff = false;
                 EcranOn = true;
            }
                    Intent i = new Intent(context, ServiceBr.class);
            i.putExtra("screen_state", EcranOff);// in order that it broadcast the new //state when the screen goes out....
            context.startService(i);
        }
    
    }
    
    class ServiceBr extends Service {
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
    
            registerReceiver(mReceiver, filter);
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
          lanceService= true;
            boolean screenOn = intent.getBooleanExtra("screen_state", false);
            println("lanceservice======"+ lanceService);
            if (!screenOn) {
    
            } else {
                // your code
    
            }
        }
    
      @Override
      public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
      }
    
    
    }
    

    19 problems (16 errors, 3 warnings) The class "BroadCastReceiver" does not exist The variable "EcranOff" does not exist ....................

    Dear. akenaton Thank you very much. In addition, there is a question.

    Why do I get an error?

  • @Gwac=

    • change in the code <a href....for only @Override as i told you to do!
    • you never declare the boolean EcranOff
    • you never create myBr
  • Dear. akenaton, I really appreciate your help.

    code

    error_b

    What's the problem now?

  • 13 problems (10 errors, 3 warnings)

  • edited November 2016

    @Gwak===

    • first problem is that you need to import android classes (Service, iBinder...); have you tried to click on theses errors in the console???
    • second problem is that you need to declare 2 more booleans to false at start (lanceservice,lancebr)
    • third problem - and that is very important - is that you does not seem to know how to use a class (new...)
    • fourth one is that you are declaring myBr in your set up in a strange manner
    • fifth one is that you never declared your receiver (mReceiver)
  • Your biggest problem is that you aren't first reading your error messages and trying to figure out what they mean by thinking about them, looking at where they occur in your code, and if necessary, looking them up. Until you develop this habit, not reading error messages is going to be a huge problem when you do any programming for the rest of your life.

  • I know what it means.

    By the way, I am a beginner. If so, would not you understand if I write the code as an example?

    If you are concerned that there is a problem with the current code, please write the example code to make it possible.

    You can do it. I just want to see your complete example source. Then I can apply. Simple but please ask.

  • It does not matter to me, but a sample is important. The sample is an example source.

  • Answer ✓

    @gwak=== if you are a beginner, don't ask for bluetooth, android && complex attempts; begin with simple things with java mode. As for the code it works, i have used it and tested it. Be patient!

  • OK, I know your intentions. Thank you very much. I will show you success.

Sign In or Register to comment.