Example usage for android.net.wifi.p2p WifiP2pDevice WifiP2pDevice

List of usage examples for android.net.wifi.p2p WifiP2pDevice WifiP2pDevice

Introduction

In this page you can find the example usage for android.net.wifi.p2p WifiP2pDevice WifiP2pDevice.

Prototype

public WifiP2pDevice() 

Source Link

Usage

From source file:it.polimi.deib.p2pchat.discovery.chatmessages.waitingtosend.discovery.MainActivity.java

/**
 * Method called automatically by Android when
 * {@link ChatManager}//from  w ww  .ja va 2  s . com
 * calls handler.obtainMessage(***).sendToTarget().
 */
@Override
public boolean handleMessage(Message msg) {
    Log.d(TAG, "handleMessage, tabNum in this activity is: " + tabNum);

    switch (msg.what) {
    //called by every device at the beginning of every connection (new or previously removed and now recreated)
    case Configuration.FIRSTMESSAGEXCHANGE:
        final Object obj = msg.obj;

        Log.d(TAG, "handleMessage, " + Configuration.FIRSTMESSAGEXCHANGE_MSG + " case");

        chatManager = (ChatManager) obj;

        sendAddress(LocalP2PDevice.getInstance().getLocalDevice().deviceAddress,
                LocalP2PDevice.getInstance().getLocalDevice().deviceName, chatManager);

        break;
    case Configuration.MESSAGE_READ:
        byte[] readBuf = (byte[]) msg.obj;

        Log.d(TAG, "handleMessage, " + Configuration.MESSAGE_READ_MSG + " case");

        // construct a string from the valid bytes in the buffer
        String readMessage = new String(readBuf, 0, msg.arg1);

        Log.d(TAG, "Message: " + readMessage);

        //message filter usage
        try {
            MessageFilter.getInstance().isFiltered(readMessage);
        } catch (MessageException e) {
            if (e.getReason() == MessageException.Reason.NULLMESSAGE) {
                Log.d(TAG, "handleMessage, filter activated because the message is null = " + readMessage);
                return true;
            } else {
                if (e.getReason() == MessageException.Reason.MESSAGETOOSHORT) {
                    Log.d(TAG, "handleMessage, filter activated because the message is too short = "
                            + readMessage);
                    return true;
                } else {
                    if (e.getReason() == MessageException.Reason.MESSAGEBLACKLISTED) {
                        Log.d(TAG,
                                "handleMessage, filter activated because the message contains blacklisted words. Message = "
                                        + readMessage);
                        return true;
                    }
                }
            }
        }

        //if the message received contains Configuration.MAGICADDRESSKEYWORD is because now someone want to connect to this device
        if (readMessage.contains(Configuration.MAGICADDRESSKEYWORD)) {

            WifiP2pDevice p2pDevice = new WifiP2pDevice();
            p2pDevice.deviceAddress = readMessage.split("___")[1];
            p2pDevice.deviceName = readMessage.split("___")[2];
            P2pDestinationDevice device = new P2pDestinationDevice(p2pDevice);

            if (readMessage.split("___").length == 3) {
                Log.d(TAG, "handleMessage, p2pDevice created with: " + p2pDevice.deviceName + ", "
                        + p2pDevice.deviceAddress);
                manageAddressMessageReception(device);
            } else if (readMessage.split("___").length == 4) {
                device.setDestinationIpAddress(readMessage.split("___")[3]);

                //set client ip address
                TabFragment.getWiFiP2pServicesFragment()
                        .setLocalDeviceIpAddress(device.getDestinationIpAddress());

                Log.d(TAG, "handleMessage, p2pDevice created with: " + p2pDevice.deviceName + ", "
                        + p2pDevice.deviceAddress + ", " + device.getDestinationIpAddress());
                manageAddressMessageReception(device);
            }
        }

        //i check if tabNum is valid only to be sure.
        //i using this if, because this peace of code is critical and "sometimes can throw exceptions".
        if (tabFragment.isValidTabNum(tabNum)) {

            if (Configuration.DEBUG_VERSION) {
                //i use this to re-format the message (not really necessary because in the "commercial"
                //version, if a message contains MAGICADDRESSKEYWORD, this message should be removed and used
                // only by the logic without display anything.
                if (readMessage.contains(Configuration.MAGICADDRESSKEYWORD)) {
                    readMessage = readMessage.replace("+", "");
                    readMessage = readMessage.replace(Configuration.MAGICADDRESSKEYWORD, "Mac Address");
                }
                tabFragment.getChatFragmentByTab(tabNum).pushMessage("Buddy: " + readMessage);
            } else {
                if (!readMessage.contains(Configuration.MAGICADDRESSKEYWORD)) {
                    //                            tabFragment.getChatFragmentByTab(tabNum).pushMessage("Buddy: " + readMessage);
                }
            }

            //if the WaitingToSendQueue is not empty, send all his messages to target device.
            //                    if (!WaitingToSendQueue.getInstance().getWaitingToSendItemsList(tabNum).isEmpty()) {
            //                        tabFragment.getChatFragmentByTab(tabNum).sendForcedWaitingToSendQueue();
            //                    }
        } else {
            Log.e("handleMessage", "Error tabNum = " + tabNum + " because is <=0");
        }
        break;
    }
    return true;

}