java - Android bluetooth send/receive a hashmap -


i'm trying send hashmap 1 device via bluetooth. i'm creating hashmap this, , calling connection threads writeobject method:

map<string,string> subinfo = new hashmap<>(); subinfo.put("type", "subinfo"); subinfo.put("num", "1"); btservice.connectedthread.writeobject(subinfo); 

my bluetooth service has following class thats used handle data transfer between devices in background thread:

public static class connectedthread extends thread {         private final bluetoothsocket mmsocket;         private final inputstream mminstream;         private final outputstream mmoutstream;         private final objectinputstream mmobjinstream;         private final objectoutputstream mmobjoutstream;          public connectedthread(bluetoothsocket socket) {             mmsocket = socket;             inputstream tmpin = null;             outputstream tmpout = null;             objectinputstream tmpobjin = null;             objectoutputstream tmpobjout = null;              // input , output streams, using temp objects because             // member streams final             try {                 tmpin = socket.getinputstream();                 tmpout = socket.getoutputstream();                 tmpobjin = new objectinputstream(socket.getinputstream());                 tmpobjout = new objectoutputstream(socket.getoutputstream());             } catch (ioexception e) {                 e.printstacktrace();             }              mminstream = tmpin;             mmoutstream = tmpout;             mmobjinstream = tmpobjin;             mmobjoutstream = tmpobjout;         }          public void run() {             byte[] buffer = new byte[1024];  // buffer store stream             int bytes; // bytes returned read()              // keep listening inputstream until exception occurs             while (!this.isinterrupted()) {                 try {                     // read inputstream                     bytes = mminstream.read(buffer);                     // send obtained bytes ui activity                     mhandler.obtainmessage(constants.message_read, bytes, -1, buffer)                             .sendtotarget();                 } catch (ioexception e) {                     e.printstacktrace();                     break;                 }                  try {                     //read objects objectinputstream                     object object = mmobjinstream.readobject();                     // send obtained object ui activity                     mhandler.obtainmessage(constants.message_read_object, -1, 0, object)                             .sendtotarget();                 } catch (classnotfoundexception | ioexception e) {                     e.printstacktrace();                 }             }         }          /* call main activity send data remote device */         public void write(byte[] bytes) {             try {                 mmoutstream.write(bytes);             } catch (ioexception e) {                 e.printstacktrace();             }         }          public void writeobject(object object) {             try {                 mmobjoutstream.writeobject(object);             }catch(exception e){                 log.e(tag, "error objectoutputstream: " + e.getlocalizedmessage());             }         }          /* call main activity shutdown connection */         public void cancel() {             try {                 mmsocket.close();                 this.interrupt();             } catch (ioexception e) {                 e.printstacktrace();             }         }     } 

so when call writeobject method should send object down object output stream of thread above.

on other device, code similar. listens on object input stream object, once has been found sends message_read_object message message handler in main activity:

static class messagehandler extends handler {         private final weakreference<mainactivity> mactivity;          messagehandler(mainactivity activity) {             mactivity = new weakreference<>(activity);         }         @override         public void handlemessage(message msg)         {             final mainactivity mainactivity = mactivity.get();             if (mainactivity == null) {                 return;             }             switch(msg.what){                  case constants.message_read_object:                     object receivedobject = msg.obj;                      //how should pull out hashmap here?                      switch(type){                         case "subinfo":                             //do things                             break;                     }                     break;             }         }     } 

what correct way reassemble hashmap in message handler? need check "type" key of map , switch based on that, of right i'm not sure how map object back. receivedobject object in message handler doesn't seem behave hashmap, , doesn't have typical methods used pulling out keys , values

furthermore, instead of sending generic object on bluetooth, make more sense directly send map type object?

just direct convert

hashmap mhashmap = (hashmap) obj;

this , https://github.com/zhenbeiju/androidcommonutil/blob/master/app/src/main/java/commanutil/utl/file/objutil.java

useage: https://coding.net/u/zhenbeiju/p/javanlp_/git/blob/master/src/com/voice/common/util/nlp/main.java