android中上下层使用socket说明

Java程序中


s = new LocalSocket();

l = new LocalSocketAddress("trild",

LocalSocketAddress.Namespace.RESERVED);

s.connect(l);




sample: If you want to get signal strength.

send:


RILRequest rr = RILRequest.obtain(RIL_REQUEST_SIGNAL_STRENGTH, result);

data = rr.mp.marshall();


// parcel length in big endian

dataLength[0] = dataLength[1] = 0;

dataLength[2] = (byte)((data.length >> 8) & 0xff);

dataLength[3] = (byte)((data.length) & 0xff);


s.getOutputStream().write(dataLength);

s.getOutputStream().write(data);



receive:

// First, read in the length of the message

byte[] buffer;

int countRead;

int messageLength;

int serial, error;

int numInts = 2;

int response[];


buffer = new byte[RIL_MAX_COMMAND_BYTES];

countRead = mSocket.getInputStream().read(buffer, 0, 4);

messageLength = ((buffer[0] & 0xff) << 24)

| ((buffer[1] & 0xff) << 16)

| ((buffer[2] & 0xff) << 8)

| (buffer[3] & 0xff);


countRead = mSocket.getInputStream().read(buffer, 0, messageLength);

p = Parcel.obtain();

p.unmarshall(buffer, 0, length);

p.setDataPosition(0);

serial = p.readInt();

error = p.readInt();


response = new int[numInts];

for (int i = 0 ; i < numInts ; i++) {

response[i] = p.readInt();

}


使用详细方法参考代码中的RIL.java文件。






C or C++程式序中

#include <cutils/sockets.h>


fd = socket_local_client("trild",

ANDROID_SOCKET_NAMESPACE_RESERVED,

SOCK_STREAM);

if (fd < 0) {

perror ("opening radio debug socket");

exit(-1);

}




sample: If you want to get signal strength.

send:

Parcel p;

uint32_t header;

void* data;

size_t datasize;


p.writeInt32 (RIL_REQUEST_SIGNAL_STRENGTH);

p.writeInt32 (token);


data = p.data();

datasize = p.dataSize();

header = htonl(p.dataSize());

write (fd, &header, sizeof(header));

write (fd, data, datasize);



receive:

#include <cutils/record_stream.h>

Parcel p;

RecordStream *p_rs;

void *p_record;

size_t recordlen;

int ret;

int serial, error;

int numInts = 2;

int response[2];


p_rs = record_stream_new(fd, MAX_COMMAND_BYTES);

record_stream_get_next(p_rs, &p_record, &recordlen);

p.setData((uint8_t *) buffer, buflen);

p.readInt32(&serial);

p.readInt32 (&error);


for(i = 0; i < numInts; i++)

{

p.readInt32(&response[i]);

}



上面只是一些简单的使用,真实使用中还需加入一些出错判断等情形的考虑,请不要直接使用上面代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值