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

📄 util.java

📁 benhui网的蓝牙例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package btchat;

import javax.bluetooth.*;

import java.util.Enumeration;
import java.io.IOException;
import java.util.Vector;
/**
 * BlueChat example application.
 * Originally published in Java Developer's Journal (volume 9 issue 2).
 * Updated by Ben Hui on www.benhui.net.
 * Copyright: (c) 2003-2004
 * Author: Ben Hui
 *
 * YOU ARE ALLOWED TO USE THIS CODE FOR EDUCATIONAL, PERSONAL TRAINNING,
 * REFERENCE PURPOSE. YOU MAY DISTRIBUTE THIS CODE AS-IS OR MODIFIED FORM.
 * HOWEVER, YOU CANNOT USE THIS CODE FOR COMMERCIAL PURPOSE. THIS INCLUDE,
 * BUT NOT LIMITED TO, PRODUCING COMMERCIAL SOFTWARE, CONSULTANT SERVICE,
 * PROFESSIONAL TRAINNING MATERIAL.
 *
 * <p>Title: A utility class to dump JABWT object contents</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * @author Ben Hui
 * @version 1.0
 */

public class Util
{
  private Util()
  {
  }

  private static void log( String s )
  {
    System.out.println( s );

    // "U" means Util class
    if ( ChatMain.isDebug )
      ChatMain.instance.gui_log( "U", s );

  }

  public static void printRemoteDevice( RemoteDevice dev, DeviceClass devClass )
  {
    try
    {
    log("Print Remote Device "+dev.getBluetoothAddress());
    log("Name: "+dev.getFriendlyName( true ) );
    log("Auth: "+dev.isAuthenticated()+" Encrypted: "+dev.isEncrypted()+" Trusted: "+dev.isTrustedDevice() );

    if ( devClass != null )
    {
      log("MajorDevice:" +
                         majorToName(devClass.getMajorDeviceClass()));
      log("MinorDevice:" +
                         minorToName(devClass.getMajorDeviceClass(),
                                     devClass.getMinorDeviceClass()));
      log("ServiceClass:");
      String[] str = Util.majorServiceToName(devClass.getServiceClasses());
      for (int i = 0; i < str.length; i++) {
        log("  " + str[i]);
      }
    }
    } catch (IOException e)
    {
    }
  }

  public static void printLocalDevice( LocalDevice dev )
  {
    log("Print Local Device "+dev.getBluetoothAddress());
    log("Name: "+dev.getFriendlyName());
    DeviceClass devClass = dev.getDeviceClass();
    if ( devClass != null )
    {
      log("MajorDevice:" +
                         majorToName(devClass.getMajorDeviceClass()));
      log("MinorDevice:" +
                         minorToName(devClass.getMajorDeviceClass(),
                                     devClass.getMinorDeviceClass()));
      log("ServiceClass:");
      String[] str = Util.majorServiceToName(devClass.getServiceClasses());
      for (int i = 0; i < str.length; i++) {
        log("  " + str[i]);
      }
    }

  }
/*
  public static void printDeviceClass( DeviceClass d )
  {
    log("Print Device Class "+d.toString());
  }
*/
  public static void printServiceRecord( ServiceRecord r )
  {
    int[] ids = r.getAttributeIDs();
    log("Print Service Record (# of element: "+ids.length+")");
    log("Print Service Record URL "+r.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false ) );


    for ( int i=0; i < ids.length; i++ )
    {
      DataElement el = r.getAttributeValue( ids[i] );
      printDataElement( el, ids[i], "" );
    }
  }

  public static void printDataElement( DataElement e, int id, String indent )
  {
      int type = e.getDataType();
      if ( type == DataElement.DATALT || type == DataElement.DATSEQ )
      {
        Enumeration enum = (Enumeration)e.getValue();
        log(indent+"DataElement["+idToName(id)+"] "+type+" (# of element: "+e.getSize()+")");
        while ( enum.hasMoreElements() )
        {
          DataElement e2 = (DataElement)enum.nextElement();
          printDataElement( e2, id, indent+"  " );
        }
      } else if ( type == DataElement.U_INT_1 || type ==  DataElement.U_INT_2 || type == DataElement.U_INT_4 ||
                  type == DataElement.INT_1 || type == DataElement.INT_2 || type == DataElement.INT_4 || type == DataElement.INT_8 )
      {
        long v = e.getLong();
        log(indent+"DataElement["+idToName(id)+"] "+v);
      } else if ( type == DataElement.UUID )
      {
        UUID uuid = (UUID) e.getValue();
        log(indent+"DataElement["+idToName(id)+"] "+ uuidToName(uuid) );
      } else if ( type == DataElement.U_INT_8 || type == DataElement.U_INT_16 || type == DataElement.INT_16 )
      {
        byte[] v = (byte[]) e.getValue();
        String s = "";
        for ( int i=0; i< v.length; i++ )
        {
          s+= Integer.toHexString( (int) v[i] );
        }
        log(indent+"DataElement["+idToName(id)+"] "+s );

      } else if ( type == DataElement.STRING || type == DataElement.URL )
      {
        String v = (String) e.getValue();
        log(indent+"DataElement["+idToName(id)+"] "+v );

      } else if ( type == DataElement.BOOL )
      {
        boolean  v =  e.getBoolean();
        log(indent+"DataElement["+idToName(id)+"] "+ String.valueOf( v));

      } else if ( type == DataElement.NULL )
      {
        log(indent+"DataElement["+idToName(id)+"] NULL");

      }

  }

  // convert Attribute  ID to human friendly name
  public static String idToName( int id )
  {
    if ( id == 0x0000 )
    {
      return "ServiceRecordHandle";
    } else if ( id == 0x0001 )
    {
      return "ServiceClassIDList";

    } else if ( id == 0x0002 )
    {
      return "ServiceRecordState";
    } else if ( id == 0x0003 )
    {
      return "ServiceID";
    } else if ( id == 0x0004 )
    {
      return "ProtocolDescriptorList";
    } else if ( id == 0x0005 )
    {
      return "BrowseGroupList";
    } else if ( id == 0x0006 )
    {
      return "LanguageBasedAttributeIDList";
    } else if ( id == 0x0007 )
    {
      return "ServiceInfoTimeToLive";
    } else if ( id == 0x0008 )
    {
      return "ServiceAvailability";
    } else if ( id == 0x0009 )
    {
      return "BluetoothProfileDescriptorList";
    } else if ( id == 0x000A )
    {
      return "DocumentationURL";
    } else if ( id == 0x000B )
    {
      return "ClientExecutableURL";
    } else if ( id == 0x000C )
    {
      return "IconURL";
    } else if ( id == 0x000D )
    {
      return "AdditionalProtocol";
    } else if ( id == 0x0100 )
    {
      return "ServiceName";
    } else if ( id == 0x0101 )
    {
      return "ServiceDescription";
    } else if ( id == 0x0102 )
    {
      return "ProviderName";
    } else if ( id == 0x0200 )
    {
      /** @todo why the spec say it is GroupID, IpSubnet and VersionNumberList as well? */
      return "GroupID";
    } else if ( id == 0x0201 )
    {
      return "ServiceDatabaseState";

    } else if ( id == 0x0300 )
    {
      return "ServiceVersion";
    } else if ( id == 0x0301 )
    {
      return "ExternalNetwork";
    } else if ( id == 0x0302 )
    {
      // @todo or FaxClass1Support in case of Fax Profile
      return "RemoteAudioVolumeControl";
    } else if ( id == 0x0303 )
    {
      // @todo or FaxClass2Support in case of Fax Profile
      return "SupportedFormatList";
    } else if ( id == 0x0304 )
    {
      return "FaxClass2Support";
    } else if ( id == 0x0305 )
    {
      return "AudioFeedbackSupport";
    } else if ( id == 0x0306 )
    {
      return "NetworkAddress";
    } else if ( id == 0x0307 )
    {
      return "WAPGateway";
    } else if ( id == 0x0308 )
    {
      return "HomePageURL";
    } else if ( id == 0x0309 )
    {
      return "WAPStackType";
    } else if ( id == 0x030A )
    {
      return "SecurityDescription";
    } else if ( id == 0x030B )
    {
      return "NetAccessType";
    } else if ( id == 0x030C )
    {
      return "MaxNetAccessrate";
    } else if ( id == 0x030D )
    {
      return "IPv4Subnet";
    } else if ( id == 0x030E )
    {
      return "IPv6Subnet";
    } else if ( id == 0x0310 )
    {
      return "SupportedCapabalities";
    } else if ( id == 0x0311 )
    {
      return "SupportedFeatures";
    } else if ( id == 0x0312 )
    {
      return "SupportedFunctions";
    } else if ( id == 0x0313 )
    {
      return "TotalImagingDataCapacity";
    } else
    {
      return "UnknownAttribute("+id+")";
    }
  }

  public static String uuidToName( UUID u )
  {
    if ( u.equals( new UUID( 0x0001 ) ))
      return "SDP";
    else if ( u.equals( new UUID( 0x0003 ) ))

⌨️ 快捷键说明

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