151. // TODO Should be better. 152. bo.write((daBytes.length - 1) * 2
153. - ((daBytes[daBytes.length - 1] & 0xf0) == 0xf0 ? 1 : 0));
154.
155. // destination address
156. bo.write(daBytes, 0, daBytes.length); 157.
158. // TP-Protocol-Identifier //TP--PID 159. bo.write(0); 160. return bo; 161. } 162. 163. ⑧分析
164. //GsmAlphabet.java
165. public static byte[] stringToGsm7BitPackedWithHeader(String data, byte[] header) 166. throws EncodeException { //这里传进来的head为null
167.
168. if (header == null || header.length == 0) {
169. return stringToGsm7BitPacked(data); //转⑨分析 170. } 171.
172. int headerBits = (header.length + 1) * 8; 173. int headerSeptets = (headerBits + 6) / 7; 174.
175. byte[] ret = stringToGsm7BitPacked(data, headerSeptets, true); 176.
177. // Paste in the header
178. ret[1] = (byte)header.length;
179. System.arraycopy(header, 0, ret, 2, header.length); 180. return ret; 181. } 182. 183. ⑨分析
184. //GsmAlphabet.java
185. public static byte[] stringToGsm7BitPacked(String data) 186. throws EncodeException {
187. return stringToGsm7BitPacked(data, 0, true); //转⑩分析 188. }
189. 190. ⑩分析
191. //GsmAlphabet.java
192. public static byte[] stringToGsm7BitPacked(String data, int startingSeptetOffset, 193. boolean throwException) throws EncodeException { 194. int dataLen = data.length();
195. int septetCount = countGsmSeptets(data, throwException) + startingSeptetOffset; 196. // 当传入的字符串data中含有charToGsm, charToGsmExtended中没有的字符时(例如汉字),该函数会抛出异
常,这样在调用处⑥会捕获该异常。然后会采用UCS-2方式进行编码。
197.
198. if (septetCount > 255) {
199. throw new EncodeException(\); 200. }
201. int byteCount = ((septetCount * 7) + 7) / 8;
202. byte[] ret = new byte[byteCount + 1]; // Include space for one byte length prefix. 203. for (int i = 0, septets = startingSeptetOffset, bitOffset = startingSeptetOffset * 7; 204. i < dataLen && septets < septetCount; 205. i++, bitOffset += 7) { 206. char c = data.charAt(i);
207. int v = GsmAlphabet.charToGsm(c, throwException); 208. if (v == GSM_EXTENDED_ESCAPE) {
209. v = GsmAlphabet.charToGsmExtended(c); // Lookup the extended char. 210. packSmsChar(ret, bitOffset, GSM_EXTENDED_ESCAPE); 211. bitOffset += 7; 212. septets++; 213. }
214. packSmsChar(ret, bitOffset, v); 215. septets++; 216. }
217. ret[0] = (byte) (septetCount); // Validated by check above. 218. return ret;
219. }
220.
221. I分析:
222. //SMSDispatcher.java
223. protected void sendRawPdu(SmsTracker tracker) { 224. HashMap map = tracker.mData;
225. byte pdu[] = (byte[]) map.get(\);
226.
227. PendingIntent sentIntent = tracker.mSentIntent; 228. if (mSmsSendDisabled) {
229. if (sentIntent != null) { 230. try {
231. sentIntent.send(RESULT_ERROR_NO_SERVICE); 232. } catch (CanceledException ex) {} 233. }
234. Log.d(TAG, \); 235. return; 236. } 237.
238. if (pdu == null) {
239. if (sentIntent != null) { 240. try {
241. sentIntent.send(RESULT_ERROR_NULL_PDU); 242. } catch (CanceledException ex) {} 243. }
244. return; 245. }
246.
247. int ss = mPhone.getServiceState().getState(); 248.
249. // if IMS not registered on data and voice is not available... 250. if (!isIms() && ss != ServiceState.STATE_IN_SERVICE) { 251. handleNotInService(ss, tracker); 252. } else {
253. String appName = getAppNameByIntent(sentIntent); 254. if (mCounter.check(appName, SINGLE_PART_SMS)) { 255. sendSms(tracker); // 转II分析 256. } else {
257. sendMessage(obtainMessage(EVENT_POST_ALERT, tracker)); 258. } 259. } 260. } 261.
262. II分析:
263. //GsmSMSDispatcher.java 264. /** {@inheritDoc} */ 265. @Override
266. protected void sendSms(SmsTracker tracker) { 267. HashMap
268.
269. byte smsc[] = (byte[]) map.get(\); 270. byte pdu[] = (byte[]) map.get(\); 271.
272. Message reply = obtainMessage(EVENT_SEND_SMS_COMPLETE, tracker); 273.
274. if (tracker.mRetryCount > 0 || !isIms()) { 275. // this is retry, use old method
276. mCm.sendSMS(IccUtils.bytesToHexString(smsc),
277. IccUtils.bytesToHexString(pdu), reply); 278. } else {
279. mCm.sendImsGsmSms(IccUtils.bytesToHexString(smsc), 280. IccUtils.bytesToHexString(pdu), reply); 281. } 282. }