Search code examples
javaxbee

Send a Java Map<Integer,String> With XBee


I would send a Java Map with XBee, but I see in the Git repository how they send byte[]. How can I send my Map? I thinked use Python for that, but I prefer use Java for all and not use Java for all unless for send the message.

This is my code:

public class Comunicacion {
    /* Constants */
    private static final String PORT = "ttyUSB0";
    private static final int BAUD_RATE = 9600;

    private static Map<Integer, String> dataToSend = new HashMap<Integer, String>();

public static void main(String[] args) {
    XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
    byte[] dataInByte = dataToSend.getBytes();

    try {
        myDevice.open();

        System.out.format("Sending broadcast data: '%s'", new String(dataInByte));

        myDevice.sendBroadcastData(dataInByte);

        System.out.println(" >> Success");

    } catch (XBeeException e) {
        System.out.println(" >> Error");
        e.printStackTrace();
        System.exit(1);
    } finally {
        myDevice.close();
    }
}

Solution

  • Just an example of a conversion for just about anything to a binary string... However I am unsure of what your sending this info too and the required headers. None the less, this may get you what you want, a converted binary value.

    public class JavaApplication10 {
        static HashMap<String, Boolean> map = new HashMap<>(); 
    
    
        public static void main(String[] args){
    
            byte[] dataInByte = map.toString().getBytes();
            String sender ="";
            for (byte b : dataInByte) {
                sender = sender + (Integer.toBinaryString(b));
            }
            System.out.println(sender);   
        }
    }