Android平台 短信发送流程图形剖析(含编码)
转自:http://lzd20021683.iteye.com/blog/1306918
其他SMS/MMS分析:http://lzd20021683.iteye.com/category/187047
本文对Android平台短信发送流程进行了走读和剖析,特别是编码部分,今天将流程整理出来,以便平时参考,也希望对大家有用!!!
先上图,下面2个图是用PPT画的,这里截图附上来:
流程图1:
Framewoks
流程图2:
发送流程编码解析:
从上图中的GsmSMSDispatcher的sendText开始分析
Java代码 1. //GsmSMSDispatcher.java 2. /** {@inheritDoc} */ 3. @Override 4. // ①入口
5. protected void sendText(String destAddr, String scAddr, String text, 6. PendingIntent sentIntent, PendingIntent deliveryIntent) { 7. SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu ( //转②分析 8. scAddr, destAddr, text, (deliveryIntent != null));
9. 10. HashMap map = SmsTrackerMapFactory(destAddr, scAddr, text, pdu); 11. SmsTracker tracker = SmsTrackerFactory(map, sentIntent, deliveryIntent, 12. RadioTechnologyFamily.RADIO_TECH_3GPP); 13. sendRawPdu(tracker); //转I分析
14. }
15. 16. ②分析:
17. //SmsMessage.java
18. public static SubmitPdu getSubmitPdu(String scAddress, 19. String destinationAddress, String message, 20. boolean statusReportRequested) {
21. return getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested, null); //转③分
析 22. }
23. 24. ③分析
25. //SmsMessage.java
26. public static SubmitPdu getSubmitPdu(String scAddress, 27. String destinationAddress, String message, 28. boolean statusReportRequested, byte[] header) {
29. return getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested, header,ENCODIN
G_UNKNOWN/*默认编码方式*/); //转④分析 30. }
31. 32. ④分析 编码核心函数
33. //SmsMessage.java
34. public static SubmitPdu getSubmitPdu(String scAddress, 35. String destinationAddress, String message,
36. boolean statusReportRequested, byte[] header, int encoding) { 37. // 3
38. // Perform null parameter checks.
39. if (message == null || destinationAddress == null) { 40. return null; 41. }
42. 43. SubmitPdu ret = new SubmitPdu(); 44. // MTI = SMS-SUBMIT, UDHI = header != null 45. byte mtiByte = (byte)(0x01 | (header != null ? 0x40 : 0x00));
46. //MTI :bit 0 和 bit1 UDHI:bit6
47. // 0x01 = 0000 0001 0x40 = 0010 0000 48. ByteArrayOutputStream bo = getSubmitPduHead( 49. scAddress, destinationAddress, mtiByte, 50. statusReportRequested, ret); //转⑤分析 51. // User Data (and length) //TP-DCS 和TP-UDL 52. byte[] userData;
53. if (encoding == ENCODING_UNKNOWN) {
54. // First, try encoding it with the GSM alphabet
55. encoding = ENCODING_7BIT; //默认先采用ENCODING_7BIT编码模式 56. } 57. try {
58. if (encoding == ENCODING_7BIT) {
59. userData = GsmAlphabet.stringToGsm7BitPackedWithHeader(message, header);
60. //采用ENCODING_7BIT进行编码,若出现编码异常,函数会抛出异常:EncodeException,转至⑥处。编码成功转
至⑦。stringToGsm7BitPackedWithHeader分析转⑧处 61. } else { //assume UCS-2 62. try {
63. userData = encodeUCS2(message, header); 64. } catch(UnsupportedEncodingException uex) { 65. Log.e(LOG_TAG,
66. \, 67. uex); 68. return null; 69. } 70. }
71. } catch (EncodeException ex) { //⑥ 7bit编码模式失败后,就采用UCS-2进行编码 72. // Encoding to the 7-bit alphabet failed. Let's see if we can 73. // send it as a UCS-2 encoded message 74. try {
75. userData = encodeUCS2(message, header); 76. encoding = ENCODING_16BIT;
77. } catch(UnsupportedEncodingException uex) { 78. Log.e(LOG_TAG,
79. \, 80. uex); 81. return null; 82. } 83. }
84. 85. if (encoding == ENCODING_7BIT) { //⑦ 7bit编码成功 86. if ((0xff & userData[0]) > MAX_USER_DATA_SEPTETS) { 87. // Message too long 88. return null; 89. } 90. // TP-Data-Coding-Scheme 91. // Default encoding, uncompressed 92. // To test writing messages to the SIM card, change this value 0x00 93. // to 0x12, which means \ 94. // class is 2\ 95. // words, messages sent by the phone with this change will end up on 96. // the receiver's SIM card. You can then send messages to yourself 97. // (on a phone with this change) and they'll end up on the SIM card.
98. //0x12 = 0001 0010 未压缩,class2,存储到SIM卡 99. //0x00 = 0000 0000 未压缩,class0,GSM7bit编码 100. bo.write(0x00); 101. } else { // assume UCS-2
102. if ((0xff & userData[0]) > MAX_USER_DATA_BYTES) { 103. // Message too long 104. return null; 105. }
106. // TP-Data-Coding-Scheme
107. // Class 3, UCS-2 encoding, uncompressed
108. bo.write(0x0b); //0x0b = 0000 1011 未压缩,class3,UCS-2编码 109. }
110.
111. // (no TP-Validity-Period)
112. bo.write(userData, 0, userData.length); 113. ret.encodedMessage = bo.toByteArray(); 114. return ret; 115. } 116. 117. ⑤分析
118. //SmsMessage.java
119. private static ByteArrayOutputStream getSubmitPduHead(
120. String scAddress, String destinationAddress, byte mtiByte, 121. boolean statusReportRequested, SubmitPdu ret) 122. //scAddress为短信中心号码,destinationAddress为目标地址号码
123. //mtiByte为MTI和UDHI 编码数据,见上面分析,statusReportRequested为状态报告 //ret 下面会写入数据
到ret 124. {
125. ByteArrayOutputStream bo = new ByteArrayOutputStream( 126. MAX_USER_DATA_BYTES + 40);
127.
128. // SMSC address with length octet, or 0 129. if (scAddress == null) {
130. ret.encodedScAddress = null; 131. } else {
132. ret.encodedScAddress = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength(
133. scAddress); 134. }
135. // TP-Message-Type-Indicator (and friends) 136. if (statusReportRequested) {
137. // Set TP-Status-Report-Request bit. //TP-SRR bit 5 ,0x20 = 0010 0000 138. mtiByte |= 0x20;
139. if (Config.LOGD) Log.d(LOG_TAG, \); 140. }
141. bo.write(mtiByte);
142.
143. // space for TP-Message-Reference //TP-MR 144. bo.write(0); 145.
146. byte[] daBytes; 147.
148. daBytes = PhoneNumberUtils.networkPortionToCalledPartyBCD(destinationAddress); 149.
150. // destination address length in BCD digits, ignoring TON byte and pad