接上篇。
上篇最后有提到连接connect,这里接着向下看。
1 BluetoothOppObexClientSession
private void connect(int numShares) {
if (D) {
Log.d(TAG, "Create ClientSession with transport " + mTransport1.toString());
}
try {
mCs = new ClientSession(mTransport1);
mConnected = true;
} catch (IOException e1) {
Log.e(TAG, "OBEX session create error");
}
if (mConnected) {
mConnected = false;
HeaderSet hs = new HeaderSet();
hs.setHeader(HeaderSet.COUNT, (long) numShares);
synchronized (this) {
mWaitingForRemote = true;
}
try {
mCs.connect(hs);
if (D) {
Log.d(TAG, "OBEX session created");
}
mConnected = true;
} catch (IOException e) {
Log.e(TAG, "OBEX session connect error");
}
}
synchronized (this) {
mWaitingForRemote = false;
}
}
2 ClientSession
public HeaderSet connect(final HeaderSet header) throws IOException {
...
/*
* Write the OBEX CONNECT packet to the server.
* Byte 0: 0x80
* Byte 1&2: Connect Packet Length
* Byte 3: OBEX Version Number (Presently, 0x10)
* Byte 4: Flags (For TCP 0x00)
* Byte 5&6: Max OBEX Packet Length (Defined in MAX_PACKET_SIZE)
* Byte 7 to n: headers
*/
byte[] requestPacket = new byte[totalLength];
int maxRxPacketSize = ObexHelper.getMaxRxPacketSize(mTransport);
// We just need to start at byte 3 since the sendRequest() method will
// handle the length and 0x80.
requestPacket[0] = (byte)0x10;
requestPacket[1] = (byte)0x00;
requestPacket[2] = (byte)(maxRxPacketSize >> 8);
requestPacket[3] = (byte)(maxRxPacketSize & 0xFF);
if (head != null) {
System.arraycopy(head, 0, requestPacket, 4, head.length);
}
// Since we are not yet connected, the peer max packet size is unknown,
// hence we are only guaranteed the server will use the first 7 bytes.
if ((requestPacket.length + 3) > ObexHelper.MAX_PACKET_SIZE_INT) {
throw new IOException("Packet size exceeds max packet size for connect");
}
HeaderSet returnHeaderSet = new HeaderSet();
sendRequest(ObexHelper.OBEX_OPCODE_CONNECT, requestPacket, returnHeaderSet, null, false);
/*
* Read the response from the OBEX server.
* Byte 0: Response Code (If successful then OBEX_HTTP_OK)
* Byte 1&2: Packet Length
* Byte 3: OBEX Version Number
* Byte 4: Flags3
* Byte 5&6: Max OBEX packet Length
* Byte 7 to n: Optional HeaderSet
*/
if (returnHeaderSet.responseCode == ResponseCodes.OBEX_HTTP_OK) {
mObexConnected = true;
}
setRequestInactive();
return returnHeaderSet;
}
这里的注释很清楚:
- Byte 0: 0x80
- Byte 1&2: Connect Packet Length
- Byte 3: OBEX Version Number (Presently, 0x10)
- Byte 4: Flags (For TCP 0x00)
- Byte 5&6: Max OBEX Packet Length (Defined in MAX_PACKET_SIZE)
- Byte 7 to n: headers
其中requestPacket为Obex cmd的Byte 3后面的数据,前两个Byte在sendRequest中添加。
ObexHelper.OBEX_OPCODE_CONNECT = 0x80
public boolean sendRequest(int opCode, byte[] head, HeaderSet header,
PrivateInputStream privateInput, boolean srmActive) throws IOException {
...
int bytesReceived;
ByteArrayOutputStream out