Example usage for android.os Messenger send

List of usage examples for android.os Messenger send

Introduction

In this page you can find the example usage for android.os Messenger send.

Prototype

public void send(Message message) throws RemoteException 

Source Link

Document

Send a Message to this Messenger's Handler.

Usage

From source file:de.dcja.prettygreatmusicplayer.MusicPlaybackService.java

private void sendUpdateToClients() {
    List<Messenger> toRemove = new ArrayList<Messenger>();
    synchronized (mClients) {
        for (Messenger client : mClients) {
            Message msg = Message.obtain(null, MSG_SERVICE_STATUS);
            try {
                client.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();//from ww w  .  j  a  va  2s  .  co  m
                toRemove.add(client);
            }
        }

        for (Messenger remove : toRemove) {
            mClients.remove(remove);
        }
    }
}

From source file:com.nbplus.iotapp.service.IoTService.java

private void sendNotificationToApplication(Message message) {
    Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();
    if (clientMessenger != null) {
        if (clientMessenger != null) {
            try {
                clientMessenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();//from   w  ww . j ava  2  s  . co m
            }
        }
    }
}

From source file:ac.robinson.bettertogether.hotspot.HotspotManagerService.java

@SuppressFBWarnings("ANDROID_BROADCAST")
private void sendBroadcastMessageToAllLocalClients(BroadcastMessage msg) {
    for (int i = mClients.size() - 1; i >= 0; i--) {
        try {//from   www.  jav a  2s .c  o  m
            // local directly connected activities (i.e., ours)
            Messenger client = mClients.get(i);
            Message message = Message.obtain(null, MSG_BROADCAST);
            message.replyTo = mMessenger;
            Bundle bundle = new Bundle(1);
            bundle.putSerializable(PluginIntent.KEY_BROADCAST_MESSAGE, msg);
            message.setData(bundle);
            client.send(message);

            // local unconnected activities (i.e., plugins)
            Log.d(TAG,
                    "Sending broadcast message to all local clients with package "
                            + mConnectionOptions.mPluginPackage + " - type: " + msg.getType() + ", message: "
                            + msg.getMessage());
            Intent broadcastIntent = new Intent(PluginIntent.ACTION_MESSAGE_RECEIVED);
            broadcastIntent.setClassName(mConnectionOptions.mPluginPackage, PluginIntent.MESSAGE_RECEIVER);
            // TODO: source is only necessary for internal plugins - remove later?
            broadcastIntent.putExtra(PluginIntent.EXTRA_SOURCE, HotspotManagerService.this.getPackageName());
            broadcastIntent.putExtra(PluginIntent.KEY_BROADCAST_MESSAGE, msg);
            sendBroadcast(broadcastIntent);
        } catch (RemoteException e) {
            e.printStackTrace();
            mClients.remove(i); // client is dead - ok to remove here as we're reversing through the list
        }
    }
}

From source file:ac.robinson.bettertogether.hotspot.HotspotManagerService.java

private void sendSystemMessageToAllLocalClients(int type, String data) {
    for (int i = mClients.size() - 1; i >= 0; i--) {
        try {//  www .ja  v a 2  s. c o  m
            Messenger client = mClients.get(i);
            Message message = Message.obtain(null, type);
            message.replyTo = mMessenger;
            if (data != null) {
                Bundle bundle = new Bundle(1);
                bundle.putString(PluginIntent.KEY_SERVICE_MESSAGE, data);
                message.setData(bundle);
            }
            client.send(message);
        } catch (RemoteException e) {
            e.printStackTrace();
            mClients.remove(i); // client is dead - ok to remove here as we're reversing through the list
        }
    }
}

From source file:my.home.lehome.service.SendMsgIntentService.java

private void preparePengindCommand(Intent intent) {
    Messenger messenger;
    if (intent.hasExtra("messenger"))
        messenger = (Messenger) intent.getExtras().get("messenger");
    else//from  ww  w.  j  a  v  a 2  s.c o  m
        messenger = null;
    Message repMsg = Message.obtain();
    repMsg.what = MSG_BEGIN_SENDING;

    boolean isSysCmd = intent.getBooleanExtra("isSysCmd", false);
    if (isSysCmd) {
        Log.d(TAG, "sys cmd item");
        return;
    }

    ChatItem item = intent.getParcelableExtra("update");
    if (item == null) {
        item = new ChatItem();
        item.setContent(intent.getStringExtra("cmd"));
        item.setType(ChatItemConstants.TYPE_CLIENT);
        item.setState(Constants.CHATITEM_STATE_ERROR); // set ERROR
        item.setDate(new Date());
        DBStaticManager.addChatItem(getApplicationContext(), item);
    }
    item.setState(Constants.CHATITEM_STATE_PENDING);

    Log.d(TAG, "enqueue item: \n" + item);
    Bundle bundle = new Bundle();
    bundle.putBoolean("update", intent.hasExtra("update"));
    bundle.putParcelable("item", item);
    if (messenger != null) {
        repMsg.setData(bundle);
        try {
            messenger.send(repMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } else {
        Log.d(TAG, "messager is null, send broadcast instead:" + ACTION_SEND_MSG_BEGIN);
        Intent newIntent = new Intent(ACTION_SEND_MSG_BEGIN);
        newIntent.putExtras(bundle);
        sendBroadcast(newIntent);
    }

    intent.putExtra("pass_item", item);
}

From source file:com.nbplus.iotapp.service.IoTService.java

/**
 * ??  Request?   ./*  www.j a v a2s.co  m*/
 * ? Async ? ? ? ? ? ? ?  ??   .
 *
 * @param clientMessenger
 * @param msgId
 * @param command
 * @param result
 */
private void sendResultToApplication(Messenger clientMessenger, String msgId, String deviceId, int command,
        IoTResultCodes result) {
    if (clientMessenger != null) {
        Message response = new Message();
        response.what = IoTServiceCommand.COMMAND_RESPONSE;

        // ?? Bundle ? request command (int) result code (serializable)  ?.
        Bundle b = new Bundle();
        b.putString(IoTServiceCommand.KEY_MSGID, msgId);
        b.putInt(IoTServiceCommand.KEY_CMD, command);
        b.putString(IoTServiceCommand.KEY_DEVICE_UUID, deviceId);
        b.putSerializable(IoTServiceCommand.KEY_RESULT, result);
        response.setData(b);

        try {
            clientMessenger.send(response);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.nbplus.iotapp.service.IoTService.java

public void handleMessage(Message msg) {
    if (msg == null) {
        return;//from   w w w.  j av  a2 s  . c om
    }
    Log.d(TAG, "handle message msg.what = " + msg.what);
    switch (msg.what) {
    // when using iot gateway
    case IoTServiceCommand.IOT_GATEWAY_CONNECTED: {
        Log.d(TAG, "IoTServiceCommand.IOT_GATEWAY_CONNECTED received...");
        mIoTGatewayWorkerMessengerRef = new WeakReference<>(msg.replyTo);
        // TODO : test handler
        if (mIoTGatewayWorkerMessengerRef != null) {
            try {
                Messenger workerMessenger = mIoTGatewayWorkerMessengerRef.get();
                if (workerMessenger != null) {
                    Message testMsg = new Message();
                    msg.what = IoTServiceCommand.IOT_GATEWAY_DISCONNECTED;
                    Log.d(TAG, "test IoTServiceCommand.IOT_GATEWAY_DISCONNECTED");
                    workerMessenger.send(testMsg);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        break;
    }
    case IoTServiceCommand.SCANNING_START: {
        Bundle extras = msg.getData();
        if (extras == null) {
            Log.w(TAG, "Scanning bundle data is not found !!!");
            return;
        }
        boolean isPeriodic = extras.getBoolean(IoTServiceCommand.KEY_DATA);
        Log.d(TAG, "IoTServiceCommand.SCANNING_START called. periodic = " + isPeriodic);
        if (mUseIoTGateway) {

        } else {
            if (mBluetoothLeService != null && mServiceStatus == IoTServiceStatus.RUNNING) {
                mIsBleScanPeriodic = isPeriodic;
                mBluetoothLeService.scanLeDevicePeriodically(true, isPeriodic);
            }
        }
        break;
    }
    case IoTServiceCommand.SCANNING_STOP: {
        Bundle extras = msg.getData();
        if (extras == null) {
            Log.w(TAG, "Scanning bundle data is not found !!!");
            return;
        }
        //boolean isPeriodic = extras.getBoolean(IoTServiceCommand.KEY_DATA);
        Log.d(TAG, "IoTServiceCommand.SCANNING_STOP called.");
        if (mUseIoTGateway) {

        } else {
            if (mBluetoothLeService != null && mServiceStatus == IoTServiceStatus.RUNNING) {
                mIsBleScanPeriodic = false;
                mBluetoothLeService.scanLeDevicePeriodically(false, false);
            }
        }
        break;
    }
    case HANDLER_MESSAGE_CONNECTIVITY_CHANGED:
        final boolean isConnected = NetworkUtils.isConnected(this);
        Log.d(TAG, "HANDLER_MESSAGE_CONNECTIVITY_CHANGED received isConnected = " + isConnected);
        if (mLastConnectionStatus == isConnected) {
            Log.d(TAG, "mLastConnectionStatus == isConnected do not anything.");
            return;
        }

        mLastConnectionStatus = isConnected;
        if (mLastConnectionStatus) {
            Log.d(TAG, "HANDLER_MESSAGE_CONNECTIVITY_CHANGED network is connected !!!");
            // TODO : re-connect
            if (mServiceStatus == IoTServiceStatus.STOPPED) {

            }
        } else {
            Log.d(TAG, "HANDLER_MESSAGE_CONNECTIVITY_CHANGED network is disconnected !!!");
        }
        break;
    case HANDLER_MESSAGE_BT_STATE_CHANGED:
        Log.d(TAG, "HANDLER_MESSAGE_CONNECTIVITY_CHANGED received !!!");
        final int state = msg.arg1;
        if ((mLastConnectionStatus && state == BluetoothAdapter.STATE_ON)
                || (!mLastConnectionStatus && state == BluetoothAdapter.STATE_OFF)) {
            return;
        }

        mLastConnectionStatus = (state == BluetoothAdapter.STATE_ON) ? true : false;
        if (mLastConnectionStatus) {
            Log.d(TAG, "HANDLER_MESSAGE_BT_STATE_CHANGED bluetooth is enabled !!!");

            if (mServiceStatus != IoTServiceStatus.RUNNING) {
                final Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
                if (!bindService(gattServiceIntent, mBluetoothServiceConnection, BIND_AUTO_CREATE)) {
                    Log.e(TAG, ">> bind BluetoothServiceConnection failed.... !!!");
                }
            }
        } else {
            Log.d(TAG, "HANDLER_MESSAGE_BT_STATE_CHANGED bluetooth is disabled !!!");
            mErrorCodes = IoTResultCodes.BLUETOOTH_NOT_ENABLED;
            unbindService(mBluetoothServiceConnection);

            mServiceStatus = IoTServiceStatus.STOPPED;
            mHandler.removeMessages(HANDLER_MESSAGE_CONNECTION_NOT_RESPOND);
            mConnectedDeviceList.clear();
            mRequestHandleMap.clear();

            sendServiceStatusNotification();
        }
        break;
    case IoTServiceCommand.REGISTER_SERVICE: {
        Bundle b = msg.getData();
        if (msg.replyTo == null || b == null) {
            Log.e(TAG, "Invalid register args...");
            break;
        }

        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        if (!StringUtils.isEmptyString(msgId)) {
            Log.d(TAG, "msgId == " + msgId);
            String[] strArrays = msgId.split("_");
            if (strArrays.length == 2) {
                mRegisteredAppMessenger = new WeakReference<>(msg.replyTo);
                sendResultToApplication(msg.replyTo, msgId, null, msg.what, IoTResultCodes.SUCCESS);

                // ??  ??  ? ? .
                sendServiceStatusNotification();
            } else {
                Log.e(TAG, "Invalid register args...");
                sendResultToApplication(msg.replyTo, msgId, null, msg.what,
                        IoTResultCodes.INVALID_REQUEST_ARGUMENTS);
                break;
            }
        } else {
            Log.e(TAG, "Invalid register args...");
            sendResultToApplication(msg.replyTo, "", null, msg.what, IoTResultCodes.INVALID_REQUEST_ARGUMENTS);
            break;
        }

        break;
    }
    case IoTServiceCommand.UNREGISTER_SERVICE: {
        Bundle b = msg.getData();
        if (b == null) {
            Log.e(TAG, "Invalid register args...");
            break;
        }

        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        if (!StringUtils.isEmptyString(msgId)) {
            String[] strArrays = msgId.split("_");
            if (strArrays.length == 2) {
                Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null
                        : mRegisteredAppMessenger.get();
                if (clientMessenger != null) {
                    sendResultToApplication(clientMessenger, msgId, null, msg.what, IoTResultCodes.SUCCESS);
                    mRegisteredAppMessenger = null;
                } else {
                    //??
                }
            } else {
                Log.e(TAG, "Invalid register args...");
                break;
            }
        } else {
            Log.e(TAG, "Invalid un-register args...");
        }
        break;
    }

    case IoTServiceCommand.GET_DEVICE_LIST: {
        if (mUseIoTGateway) {
            // TODO
        } else {
            mBluetoothLeService.scanLeDevice(false);
            mBluetoothLeService.scanLeDevicePeriodically(true, mIsBleScanPeriodic);
        }
        break;
    }

    case IoTServiceCommand.DEVICE_DISCONNECT_ALL: {
        Bundle b = msg.getData();
        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        b.setClassLoader(IoTHandleData.class.getClassLoader());
        IoTHandleData ioTHandleData = b.getParcelable(IoTServiceCommand.KEY_DATA);

        if (mConnectedDeviceList.size() > 0) {
            mIsDisconnectAllState = true;
            ioTHandleData.setMsgId(msgId);
            ioTHandleData.setRequestCommand(msg.what);

            String connectedDeviceId = mConnectedDeviceList.get(0);
            if (mUseIoTGateway) {
                // TODO

            } else {
                if (mBluetoothLeService != null) {
                    mBluetoothLeService.disconnect(connectedDeviceId);
                } else {
                    Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null
                            : mRegisteredAppMessenger.get();
                    sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                            IoTResultCodes.FAILED);
                }
            }
        } else {
            Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null
                    : mRegisteredAppMessenger.get();
            sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                    IoTResultCodes.SUCCESS);
        }
        break;
    }

    case IoTServiceCommand.DEVICE_CONNECT: {
        Bundle b = msg.getData();
        b.setClassLoader(IoTHandleData.class.getClassLoader());
        IoTHandleData ioTHandleData = b.getParcelable(IoTServiceCommand.KEY_DATA);
        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();

        if (ioTHandleData == null) {
            sendResultToApplication(clientMessenger, msgId, null, msg.what, IoTResultCodes.FAILED);
            Log.w(TAG, "case READ_WRITE_SETNOTI : ioTHandleData is null");
            return;
        }

        if (mRequestHandleMap.get(ioTHandleData.getDeviceId()) != null) {
            Log.d(TAG, "Already previous command deviceid = " + ioTHandleData.getDeviceId() + ", command = "
                    + ioTHandleData.getRequestCommand());
            sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                    IoTResultCodes.FAILED);
            return;
        }

        if (mUseIoTGateway) {
        } else {
            if (mBluetoothLeService == null) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG,
                        "case IoTServiceCommand.DEVICE_CONNECT : mBluetoothLeService or ioTHandleData is null");
                return;
            }
            if (ioTHandleData.getDeviceTypeId() != IoTDevice.DEVICE_TYPE_ID_BT) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG,
                        "case IoTServiceCommand.DEVICE_DISCONNECT : is not bt device... mUseIoTGateway == false");
                return;
            }

            if (ioTHandleData.isKeepAliveDevice()) {
                if (mKeepAliveConnectionDeviceList.contains(ioTHandleData.getDeviceId())) {
                    Log.d(TAG, "Keep alive device is already connected.. id = " + ioTHandleData.getDeviceId());
                    sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                            IoTResultCodes.SUCCESS);

                    // send to IoT interface module
                    Message connectedMsg = new Message();
                    connectedMsg.what = IoTServiceCommand.DEVICE_CONNECTED;
                    Bundle extras = new Bundle();
                    extras.putString(IoTServiceCommand.KEY_DEVICE_UUID, ioTHandleData.getDeviceId());
                    connectedMsg.setData(extras);

                    sendNotificationToApplication(connectedMsg);
                    return;
                }
            }

            if (mBluetoothLeService.connect(ioTHandleData.getDeviceId())) {
                ioTHandleData.setMsgId(msgId);
                ioTHandleData.setRequestCommand(msg.what);

                mRequestHandleMap.put(ioTHandleData.getDeviceId(), ioTHandleData);
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.SUCCESS);
                // ?... connect  ?  .
                Message notRespondMsg = new Message();
                notRespondMsg.what = HANDLER_MESSAGE_CONNECTION_NOT_RESPOND;
                Bundle extras = new Bundle();
                extras.putParcelable(IoTServiceCommand.KEY_DATA, ioTHandleData);
                notRespondMsg.setData(extras);
                mHandler.sendMessageDelayed(notRespondMsg, CONNECTION_NOT_RESPOND_WAIT_TIME);
            } else {
                Log.w(TAG, "mBluetoothLeService.connect() return false");
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
            }
        }
        break;
    }

    case HANDLER_MESSAGE_CONNECTION_NOT_RESPOND: {
        Log.d(TAG, "HANDLER_MESSAGE_CONNECTION_NOT_RESPOND ...");
        Bundle b = msg.getData();
        b.setClassLoader(IoTHandleData.class.getClassLoader());
        IoTHandleData ioTHandleData = b.getParcelable(IoTServiceCommand.KEY_DATA);
        if (mBluetoothLeService != null) {
            mBluetoothLeService.disconnect(ioTHandleData.getDeviceId());
        }
        mRequestHandleMap.remove(ioTHandleData.getDeviceId());
        Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();
        sendResultToApplication(clientMessenger, ioTHandleData.getMsgId(), ioTHandleData.getDeviceId(),
                IoTServiceCommand.DEVICE_CONNECT, IoTResultCodes.DEVICE_CONNECTION_NOT_RESPOND);
        break;
    }

    case IoTServiceCommand.DEVICE_DISCONNECT: {
        Bundle b = msg.getData();
        b.setClassLoader(IoTHandleData.class.getClassLoader());
        IoTHandleData ioTHandleData = b.getParcelable(IoTServiceCommand.KEY_DATA);
        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();

        if (ioTHandleData == null) {
            sendResultToApplication(clientMessenger, msgId, null, msg.what, IoTResultCodes.FAILED);
            Log.w(TAG, "case READ_WRITE_SETNOTI : ioTHandleData is null");
            return;
        }

        if (mRequestHandleMap.get(ioTHandleData.getDeviceId()) != null) {
            Log.d(TAG, "Already previous command deviceid = " + ioTHandleData.getDeviceId() + ", command = "
                    + ioTHandleData.getRequestCommand());
            sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                    IoTResultCodes.FAILED);
            return;
        }

        if (mUseIoTGateway) {
        } else {
            if (mBluetoothLeService == null) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG,
                        "case IoTServiceCommand.DEVICE_DISCONNECT : mBluetoothLeService or ioTHandleData is null");
                return;
            }
            if (ioTHandleData.getDeviceTypeId() != IoTDevice.DEVICE_TYPE_ID_BT) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG,
                        "case IoTServiceCommand.DEVICE_DISCONNECT : is not bt device... mUseIoTGateway == false");
                return;
            }

            if (mConnectedDeviceList.contains(ioTHandleData.getDeviceId())
                    || mKeepAliveConnectionDeviceList.contains(ioTHandleData.getDeviceId())) {
                ioTHandleData.setMsgId(msgId);
                ioTHandleData.setRequestCommand(msg.what);

                mRequestHandleMap.put(ioTHandleData.getDeviceId(), ioTHandleData);
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.SUCCESS);
                mBluetoothLeService.disconnect(ioTHandleData.getDeviceId());
            } else {
                Log.w(TAG, "mConnectedDeviceList.contains(ioTHandleData.getDeviceId()) is false");
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
            }
        }
        break;
    }

    case IoTServiceCommand.DEVICE_READ_DATA:
    case IoTServiceCommand.DEVICE_WRITE_DATA:
    case IoTServiceCommand.DEVICE_SET_NOTIFICATION: {
        Bundle b = msg.getData();
        b.setClassLoader(IoTHandleData.class.getClassLoader());
        IoTHandleData ioTHandleData = b.getParcelable(IoTServiceCommand.KEY_DATA);
        String msgId = b.getString(IoTServiceCommand.KEY_MSGID, "");
        Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();

        if (ioTHandleData == null) {
            sendResultToApplication(clientMessenger, msgId, null, msg.what, IoTResultCodes.FAILED);
            Log.w(TAG, "case READ_WRITE_SETNOTI : ioTHandleData is null");
            return;
        }

        if (mRequestHandleMap.get(ioTHandleData.getDeviceId()) != null) {
            Log.d(TAG, "Already previous command deviceid = " + ioTHandleData.getDeviceId() + ", command = "
                    + ioTHandleData.getRequestCommand());
            sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                    IoTResultCodes.FAILED);
            return;
        }

        if (mUseIoTGateway) {
        } else {
            if (mBluetoothLeService == null) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG, "case READ_WRITE_SETNOTI : ioTHandleData is null");
                return;
            }

            if (ioTHandleData.getDeviceTypeId() != IoTDevice.DEVICE_TYPE_ID_BT) {
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
                Log.w(TAG, "case READ_WRITE_SETNOTI : is not bt device... mUseIoTGateway == false");
                return;
            }

            if (mConnectedDeviceList.contains(ioTHandleData.getDeviceId())
                    || mKeepAliveConnectionDeviceList.contains(ioTHandleData.getDeviceId())) {
                ioTHandleData.setMsgId(msgId);
                ioTHandleData.setRequestCommand(msg.what);

                boolean result = true;
                switch (msg.what) {
                case IoTServiceCommand.DEVICE_READ_DATA:
                    result = mBluetoothLeService.readCharacteristic(ioTHandleData.getDeviceId(),
                            ioTHandleData.getServiceUuid(), ioTHandleData.getCharacteristicUuid());
                    break;
                case IoTServiceCommand.DEVICE_WRITE_DATA:
                    result = mBluetoothLeService.writeRemoteCharacteristic(ioTHandleData.getDeviceId(),
                            ioTHandleData.getServiceUuid(), ioTHandleData.getCharacteristicUuid(),
                            ioTHandleData.getValue());
                    break;
                case IoTServiceCommand.DEVICE_SET_NOTIFICATION:
                    result = mBluetoothLeService.setCharacteristicNotification(ioTHandleData.getDeviceId(),
                            ioTHandleData.getServiceUuid(), ioTHandleData.getCharacteristicUuid(), true);
                    break;
                }

                if (result) {
                    mRequestHandleMap.put(ioTHandleData.getDeviceId(), ioTHandleData);
                    sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                            IoTResultCodes.SUCCESS);
                } else {
                    sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                            IoTResultCodes.FAILED);
                }
            } else {
                Log.w(TAG, "mConnectedDeviceList.contains(ioTHandleData.getDeviceId()) is false");
                sendResultToApplication(clientMessenger, msgId, ioTHandleData.getDeviceId(), msg.what,
                        IoTResultCodes.FAILED);
            }
        }
        break;
    }

    }
}

From source file:com.nbplus.iotapp.service.IoTService.java

/**
 *  ??  ? ? .//www .jav a  2 s  .  c  o m
 */
private void sendServiceStatusNotification() {
    if (PackageUtils.isActivePackage(this, getApplicationContext().getPackageName() + ":remote")) {
        Log.d(TAG, "Remote process(RealtimeBroadcast..) is running...");
        return;
    }
    Messenger clientMessenger = (mRegisteredAppMessenger == null) ? null : mRegisteredAppMessenger.get();
    if (clientMessenger != null) {
        Message response = new Message();
        response.what = IoTServiceCommand.SERVICE_STATUS_NOTIFICATION;

        // ?? Bundle ? request command (int) result code (serializable)  ?.
        Bundle b = new Bundle();
        b.putString(IoTServiceCommand.KEY_MSGID,
                /*this.getApplicationContext().*/getPackageName() + "_" + System.currentTimeMillis());
        b.putSerializable(IoTServiceCommand.KEY_SERVICE_STATUS, mServiceStatus);
        b.putSerializable(IoTServiceCommand.KEY_SERVICE_STATUS_CODE, mErrorCodes);
        response.setData(b);

        try {
            clientMessenger.send(response);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

From source file:my.home.lehome.service.SendMsgIntentService.java

private void saveAndNotify(Intent intent, String result) {
    Context context = getApplicationContext();
    int rep_code = -1;
    String desc;//w  w  w  .j a  va 2 s .c o  m
    try {
        JSONObject jsonObject = new JSONObject(result);
        rep_code = jsonObject.getInt("code");
        desc = jsonObject.getString("desc");
    } catch (JSONException e) {
        e.printStackTrace();
        desc = context.getString(R.string.chat_error_json);
    }

    Messenger messenger;
    if (intent.hasExtra("messenger"))
        messenger = (Messenger) intent.getExtras().get("messenger");
    else
        messenger = null;

    Message repMsg = Message.obtain();
    repMsg.what = MSG_END_SENDING;

    ChatItem item = intent.getParcelableExtra("pass_item");
    ChatItem newItem = null;
    if (item != null) {
        if (rep_code == 200) {
            item.setState(Constants.CHATITEM_STATE_SUCCESS);
            DBStaticManager.updateChatItem(context, item);
        } else {
            if (rep_code == 415) {
                item.setState(Constants.CHATITEM_STATE_SUCCESS);
            } else {
                item.setState(Constants.CHATITEM_STATE_ERROR);
            }
            DBStaticManager.updateChatItem(context, item);

            newItem = new ChatItem();
            newItem.setContent(desc);
            newItem.setType(ChatItemConstants.TYPE_SERVER);
            newItem.setState(Constants.CHATITEM_STATE_ERROR); // always set true
            newItem.setDate(new Date());
            DBStaticManager.addChatItem(context, newItem);
        }
    } else {
        if (rep_code != 200) {
            Log.d(TAG, result);
            newItem = new ChatItem();
            newItem.setContent(getString(R.string.loc_send_error) + ":" + desc); // TODO - not only loc report
            newItem.setType(ChatItemConstants.TYPE_SERVER);
            newItem.setState(Constants.CHATITEM_STATE_SUCCESS); // always set true
            newItem.setDate(new Date());
            DBStaticManager.addChatItem(context, newItem);
        }
    }

    Log.d(TAG, "dequeue item: " + item);
    if (item == null)
        return;

    Bundle bundle = new Bundle();
    bundle.putParcelable("item", item);
    if (newItem != null)
        bundle.putParcelable("new_item", newItem);
    bundle.putInt("rep_code", rep_code);
    if (messenger != null) {
        repMsg.setData(bundle);
        try {
            messenger.send(repMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } else {
        Log.d(TAG, "messager is null, send broadcast instead:" + ACTION_SEND_MSG_END);
        Intent newIntent = new Intent(ACTION_SEND_MSG_END);
        newIntent.putExtras(bundle);
        sendBroadcast(newIntent);
    }
}

From source file:com.smithdtyler.prettygoodmusicplayer.MusicPlaybackService.java

private void sendUpdateToClients() {
    List<Messenger> toRemove = new ArrayList<Messenger>();
    synchronized (mClients) {
        for (Messenger client : mClients) {
            Message msg = Message.obtain(null, MSG_SERVICE_STATUS);
            Bundle b = new Bundle();
            if (songFile != null) {
                b.putString(PRETTY_SONG_NAME, Utils.getPrettySongName(songFile));
                b.putString(PRETTY_ALBUM_NAME, songFile.getParentFile().getName());
                b.putString(PRETTY_ARTIST_NAME, songFile.getParentFile().getParentFile().getName());
            } else {
                // songFile can be null while we're shutting down.
                b.putString(PRETTY_SONG_NAME, " ");
                b.putString(PRETTY_ALBUM_NAME, " ");
                b.putString(PRETTY_ARTIST_NAME, " ");
            }/* ww w. j a va2  s.c o m*/

            b.putBoolean(IS_SHUFFLING, this._shuffle);

            if (mp.isPlaying()) {
                b.putInt(PLAYBACK_STATE, PlaybackState.PLAYING.ordinal());
            } else {
                b.putInt(PLAYBACK_STATE, PlaybackState.PAUSED.ordinal());
            }
            // We might not be able to send the position right away if mp is
            // still being created
            // so instead let's send the last position we knew about.
            if (mp.isPlaying()) {
                lastDuration = mp.getDuration();
                lastPosition = mp.getCurrentPosition();
            }
            b.putInt(TRACK_DURATION, lastDuration);
            b.putInt(TRACK_POSITION, lastPosition);
            msg.setData(b);
            try {
                client.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
                toRemove.add(client);
            }
        }

        for (Messenger remove : toRemove) {
            mClients.remove(remove);
        }
    }
}