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]);
}
上面只是一些简单的使用,真实使用中还需加入一些出错判断等情形的考虑,请不要直接使用上面代码。