現(xiàn)在SMS客戶端是創(chuàng)建起來了,也就是說你手上的設(shè)備已經(jīng)和服務(wù)器建立了連接,那么如何發(fā)送短信息呢?首先,你應(yīng)該使用MessageConnection接口的newMessage()方法創(chuàng)建一個(gè)空(empty)的消息,然后再設(shè)置該消息的PayloadText(也就是需要發(fā)送的文本或者是二進(jìn)制數(shù)據(jù)),最后調(diào)用MessageConnection的send()方法將短信息發(fā)送到目標(biāo)設(shè)備中去。請看下面的代碼:
public void sendText( MessageConnection conn, String text)
throws IOException, InterruptedIOException {
TextMessage msg = conn.newMessage( conn.TEXT_MESSAGE );
msg.setPayloadText( text );
conn.send( msg );
}
假如是發(fā)送二進(jìn)制格式的數(shù)據(jù),那么代碼略有不同:
public void sendBinary( MessageConnection conn, byte[] data)
throws IOException, InterruptedIOException {
BinaryMessage msg=conn.newMessage( conn.BINARY_MESSAGE);
當(dāng)然了,你所能發(fā)送的數(shù)據(jù)量是有限的,一般來說,SMS文本信息可以包含160或者是70個(gè)字符,這依靠于你使用何種字符編碼,假如是二進(jìn)制數(shù)據(jù),那么容量是140字節(jié)(Note:The WMA requires support for message concatenation, however, which means that these limits are actually at least three times higher.)。同時(shí),你可以使用MessageConnection接口的numberO fSegments()方法決定某一個(gè)非凡的短信息能否被發(fā)送,并且需要把這條信息拆分為多少個(gè)信息段(message segments)。