⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdpclientchannel.java

📁 蓝牙协议栈可是要钱的喔
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
*  (c) Copyright 2003 Christian Lorenz  ALL RIGHTS RESERVED.
* 
* This file is part of the JavaBluetooth Stack.
* 
* The JavaBluetooth Stack is free software; you can redistribute it 
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* 
* The JavaBluetooth Stack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* Created on Jun 28, 2003
* by Christian Lorenz
*/

package org.javabluetooth.stack.sdp;

import java.io.*;
import java.util.*;
import javax.bluetooth.DataElement;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import org.javabluetooth.stack.l2cap.L2CAPChannel;
import org.javabluetooth.util.Debug;

/** 
 * This class implements a L2CAPChannel for the Service Discovery Protocol.
 * It provides the capabilites to send service search and attribute requests, and parses the received results.
 * @author Christian Lorenz
 */
public class SDPClientChannel extends L2CAPChannel {
    private static final byte SDP_ERROR_RESPONSE                    = 0x01;
    private static final byte SDP_SERVICE_SEARCH_REQUEST            = 0x02;
    private static final byte SDP_SERVICE_SEARCH_RESPONSE           = 0x03;
    private static final byte SDP_SERVICE_ATTRIBUTE_REQUEST         = 0x04;
    private static final byte SDP_SERVICE_ATTRIBUTE_RESPONSE        = 0x05;
    private static final byte SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST  = 0x06;
    private static final byte SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE = 0x07;
    private RemoteDevice remoteDevice;
    private DiscoveryListener discoveryListener;

    /**
     * @param remoteDevice
     * @param discoveryAgent
     */
    public SDPClientChannel(RemoteDevice remoteDevice, DiscoveryListener discoveryListener) {
        this.remoteDevice = remoteDevice;
        this.discoveryListener = discoveryListener;
    }

    public void wasDisconnected() { }

    public void send_SDP_ErrorResponse(short transactionID, short errorCode) {
        Debug.println(2, "SDP: Sending Error Response. ");
        //TODO send error response
    }

    public void send_SDP_ServiceSearchRequest(short transactionID, short maxRecordsReturned,
        DataElement uuidListElement) throws IOException {
            byte[] uuidList             = uuidListElement.toByteArray();
            short length                = (short)(uuidList.length + 3);
            byte[] serviceSearchRequest = new byte[length + 5];
            serviceSearchRequest[0] = 0x02;
            serviceSearchRequest[1] = (byte)((transactionID >> 8) & 0xff);
            serviceSearchRequest[2] = (byte)((transactionID) & 0xff);
            serviceSearchRequest[3] = (byte)((length >> 8) & 0xff);
            serviceSearchRequest[4] = (byte)((length) & 0xff);
            serviceSearchRequest[5 + uuidList.length] = (byte)((maxRecordsReturned >> 8) & 0xff);
            serviceSearchRequest[6 + uuidList.length] = (byte)((maxRecordsReturned) & 0xff);
            serviceSearchRequest[7 + uuidList.length] = 0x00; //no continuation
            System.arraycopy(uuidList, 0, serviceSearchRequest, 5, uuidList.length);
            Debug.println(4, "SDP: Sending Service Search Request:", serviceSearchRequest);
            this.sendL2CAPPacket(serviceSearchRequest);
    }

    public void send_SDP_ServiceAttributeRequest(short transactionID, short maxAttributesReturned, int serviceRecordHandle,
        DataElement attributeListElement) throws IOException {
            byte[] attributeList           = attributeListElement.toByteArray();
            short length                   = (short)(attributeList.length + 7);
            byte[] serviceAttributeRequest = new byte[length + 5];
            serviceAttributeRequest[0] = 0x04;
            serviceAttributeRequest[1] = (byte)((transactionID >> 8) & 0xff);
            serviceAttributeRequest[2] = (byte)((transactionID) & 0xff);
            serviceAttributeRequest[3] = (byte)((length >> 8) & 0xff);
            serviceAttributeRequest[4] = (byte)((length) & 0xff);
            serviceAttributeRequest[5] = (byte)((serviceRecordHandle >> 24) & 0xff);
            serviceAttributeRequest[6] = (byte)((serviceRecordHandle >> 16) & 0xff);
            serviceAttributeRequest[7] = (byte)((serviceRecordHandle >> 8) & 0xff);
            serviceAttributeRequest[8] = (byte)((serviceRecordHandle) & 0xff);
            serviceAttributeRequest[9] = (byte)((maxAttributesReturned >> 8) & 0xff);
            serviceAttributeRequest[10] = (byte)((maxAttributesReturned) & 0xff);
            serviceAttributeRequest[11 + attributeList.length] = 0x00; //no continuation state
            System.arraycopy(attributeList, 0, serviceAttributeRequest, 11, attributeList.length);
            Debug.println(4, "SDP: Sending Service Attribute Request:", serviceAttributeRequest);
            this.sendL2CAPPacket(serviceAttributeRequest);
    }

    public void send_SDP_ServiceSearchAttributeRequest(short transactionID, short maxAttributeByteCount,
        DataElement uuidListElement, DataElement attributeListElement) throws IOException {
            byte[] uuidList                      = uuidListElement.toByteArray();
            byte[] attributeList                 = attributeListElement.toByteArray();
            short length                         = (short)(uuidList.length + attributeList.length + 3);
            byte[] serviceSearchAttributeRequest = new byte[length + 5];
            serviceSearchAttributeRequest[0] = 0x06;
            serviceSearchAttributeRequest[1] = (byte)((transactionID >> 8) & 0xff);
            serviceSearchAttributeRequest[2] = (byte)((transactionID) & 0xff);
            serviceSearchAttributeRequest[3] = (byte)((length >> 8) & 0xff);
            serviceSearchAttributeRequest[4] = (byte)((length) & 0xff);
            serviceSearchAttributeRequest[5 + uuidList.length] = (byte)((maxAttributeByteCount >> 8) & 0xff);
            serviceSearchAttributeRequest[6 + uuidList.length] = (byte)((maxAttributeByteCount) & 0xff);
            serviceSearchAttributeRequest[7 + uuidList.length + attributeList.length] = 0x00; //no continuation
            System.arraycopy(uuidList, 0, serviceSearchAttributeRequest, 5, uuidList.length);
            System.arraycopy(attributeList, 0, serviceSearchAttributeRequest, 7 + uuidList.length, attributeList.length);
            Debug.println(4, "SDP: Sending Search Service Attribute Request. ", serviceSearchAttributeRequest);
            this.sendL2CAPPacket(serviceSearchAttributeRequest);
    }

    public void receiveL2CAPPacket(byte[] dataPacket) {
        //Debug.println("SDP: Received Packet: "+Debug.printByteArray(dataPacket));
        switch (dataPacket[0]) //pduID
        {
            case SDP_ERROR_RESPONSE:
                receive_SDP_ErrorResponse(dataPacket);
                break;
            case SDP_SERVICE_SEARCH_RESPONSE:
                receive_SDP_ServiceSearchResponse(dataPacket);
                break;
            case SDP_SERVICE_ATTRIBUTE_RESPONSE:
                receive_SDP_ServiceAttributeResponse(dataPacket);
                break;
            case SDP_SERVICE_SEARCH_ATTRIBUTE_RESPONSE:
                receive_SDP_ServiceSearchAttributeResponse(dataPacket);
                break;
            default:
                receive_SDP_UnknownPacket(dataPacket);
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -