Android 蓝牙 OPP传输文件(下)

接上篇。
上篇最后有提到连接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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值