Issue in sending a string from Android to other device via Bluetooth -
i trying send string android device other device. in code trying fetch date , time , storing in string , sending string device via bluetooth.
the issue when send string, of characters missing when received other device.
my code :
private void time() { int day = 0; date = new date(); string sdf = new simpledateformat("eeee", locale.english).format(now); switch (sdf) { case ("monday"): day = 1; break; case ("tuesday"): day = 2; break; case ("wednesday"): day = 3; break; case ("thursday"): day = 4; break; case ("friday"): day = 5; break; case ("saturday"): day = 6; break; case ("sunday"): day = 7; break; } int mm = calendar.getinstance().get(calendar.minute); int hh = calendar.getinstance().get(calendar.hour_of_day); int dd = calendar.getinstance().get(calendar.day_of_month); int mm = calendar.getinstance().get(calendar.month) + 1; int yy = calendar.getinstance().get(calendar.year) % 100; string time2 = "a"+mm+hh+"0"+day+dd+mm+yy+"b"; sendmessage(time2); }
i send string calling sendmessage() function given below.
code :
private void sendmessage(string message) { // check we're connected before trying if (mchatservice.getstate() != com.example.hasani.bluetoothterminal.bluetoothchatservice.state_connected) { toast.maketext(getactivity(), r.string.not_connected, toast.length_short).show(); mstartbutton.setbackgroundresource(r.color.button_default); mcalbutton.setbackgroundresource(r.color.button_default); mtestbutton.setbackgroundresource(r.color.button_default); mmanbutton.setbackgroundresource(r.color.button_default); mautobutton.setbackgroundresource(r.color.button_default); return; } // check there's send if (message.length() > 0) { // message bytes , tell bluetoothchatservice write byte[] send = message.getbytes(); while (!mchatservice.write(send)) { try{ thread.sleep(10); } catch (interruptedexception ex){ //log.d(tag,"interrupted exception"); //break; } } // reset out string buffer 0 , clear edit text field moutstringbuffer.setlength(0); } }
but if set string string t = "a000000000000b or "a111111111111b" (length = 14), string received other device , can move further operations.
code :
private void time() { int day = 0; date = new date(); string sdf = new simpledateformat("eeee", locale.english).format(now); switch (sdf) { case ("monday"): day = 1; break; case ("tuesday"): day = 2; break; case ("wednesday"): day = 3; break; case ("thursday"): day = 4; break; case ("friday"): day = 5; break; case ("saturday"): day = 6; break; case ("sunday"): day = 7; break; } int mm = calendar.getinstance().get(calendar.minute); int hh = calendar.getinstance().get(calendar.hour_of_day); int dd = calendar.getinstance().get(calendar.day_of_month); int mm = calendar.getinstance().get(calendar.month) + 1; int yy = calendar.getinstance().get(calendar.year) % 100; string time2 = "a000000000000b"; //*just check out assigning value*// sendmessage(time2); }
that if sending string "a000000000000b" length=14, accepted. when on computation did above, values missing starting position of string.
please help! thank you!
in string time2 = "a"+mm+hh+"0"+day+dd+mm+yy+"b";
of fields int
means 3rd minute string '3' , not '03'.
if insist on using int
, try
string min = string.format("%02d", min);
or use simpledateformat used earlier day of week.
calendar cal = calendar.getinstance(); simpledateformat dateformat = new simpledateformat("mmhh"); dateformat.setcalendar(cal); string mmhh = dateformat.format(cal.gettime()));
Comments
Post a Comment