0

I have a problem with the android wear device I can turn on the camera with the message api by loading turnOn() onCreate on mobile device.

Problem One: I can't open an activity if the phone is locked. Problem Two: I can start only one activity if the phone is unlocked but can't call methods of the phone. The only way I started the camera was starting the MainActivity on the Oncreate() method using the wearListener service.

I want something like this app: https://play.google.com/store/apps/details?id=com.jumpbyte.flashlight

Here is my code: Oncreate method phone

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = getIntent() ;
        int valor = intent.getIntExtra("parameter",0);
        String value = "key" ;
        if (valor == 0){
            boolean isCameraFlash = getApplicationContext().getPackageManager()
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
            if(!isCameraFlash) {
                showCameraAlert();
            } else {
                camera = Camera.open();
                params = camera.getParameters();
            }
            if(isFlashlightOn) {
                setFlashlightOff();
            } else {
                setFlashlightOn();
            }
        }
        else{
            finish();
        }
        /* boolean isCameraFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if(!isCameraFlash) {
            showCameraAlert();
        } else {
            camera = Camera.open();
            params = camera.getParameters();
        }

        if(isFlashlightOn) {
            setFlashlightOff();
        } else {
            setFlashlightOn();
        }

*/
    }

Listener phone:

private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear" ;
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equals(HELLO_WORLD_WEAR_PATH)) {
            // AudioManager audiomanager ;
            // audiomanager = (AudioManager)getSystemService(Context.AUDIO_SERVICE) ;
            // audiomanager = (AudioManager)getSystemService(Context.AUDIO_SERVICE) ;
            // audiomanager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI) ;
            // Toast.makeText(getApplicationContext(), "Volume Up | Vibration Time Increased", Toast.LENGTH_SHORT).show() ;
            //light lightOne = new light();
            // MainActivity one = new MainActivity() ;
            // one.messageone();
            // lightOne.on();
            // Toast.makeText(getApplicationContext(),
            //        "Button is clicked", Toast.LENGTH_LONG).show() ;
            Intent startIntent = new Intent(this, MainActivity.class) ;
            startIntent.putExtra("parameter", 0); //Optional parameters
            startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
            startActivity(startIntent) ;
            /* if(isFlashlightOn) {
                setFlashlightOff();
            } else {
                setFlashlightOn();
            }
            */
        } else {

        } } }

Wear main class:

private void sendMessage() {
        if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) {
            Wearable.MessageApi.sendMessage(
                    mGoogleApiClient, mNode.getId(), HELLO_WORLD_WEAR_PATH, null).setResultCallback(

                    new ResultCallback<MessageApi.SendMessageResult>() {
                        @Override
                        public void onResult(MessageApi.SendMessageResult sendMessageResult) {

                            if (!sendMessageResult.getStatus().isSuccess()) {
                                Log.e("TAG", "Failed to send message with status code: "
                                        + sendMessageResult.getStatus().getStatusCode());
                            }
                        }
                    }
            );
        }else{        } }
    protected void onStart() {
        super.onStart() ;
        if (!mResolvingError) {
            mGoogleApiClient.connect() ;
        } }
    private void resolveNode() {
        Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
            @Override
            public void onResult(NodeApi.GetConnectedNodesResult nodes) {
                for (Node node : nodes.getNodes()) {
                    mNode = node ; }     }
        }) ; }
    public void onConnected(Bundle bundle) {
        resolveNode();
    }
    public void onConnectionSuspended(int i) {
        //Improve your code
    }
    public void onConnectionFailed(ConnectionResult connectionResult) {
        //Improve your code
    }

Can someone help me out with this to problems please I can't find any way to pass those two problems. I want something like this app: https://play.google.com/store/apps/details?id=com.jumpbyte.flashlight

1 Answers1

0

I'm somewhat of a noob myself but I'm having a similar problem so I'll try to give some input on what I've learned so far.

I don't think any of that code needs to be in onCreate(), since it'll only run once when the app is first opened.

First, I would take all the code you wrote inside of onCreate() and write a seperate method outside of onCreate(). The constants like intent,valor, and value should just be private and inside of the normal Main Activity.

After making a new method and some copy pasta I would run it inside of onMessageReceived().

Here is link to a MessageAPIDemo. Very helpful.

I'm personally having trouble getting my phone to receive a message when the app isn't opened on the phone, somewhat similar to your locked phone problem. I hope I've been of some help.

Johnny Meza
  • 13
  • 1
  • 4
  • This may also be of use to you as well. http://stackoverflow.com/questions/28778481/android-wear-message-listener – Johnny Meza Jun 24 '15 at 07:25