📄 btutil.java
字号:
package com.Series40Book;
import javax.microedition.lcdui.*;
import java.util.Enumeration;
import java.io.IOException;
import java.util.*;
import java.io.*;
import javax.bluetooth.*;
public class BTUtil {
private static Form form;
public BTUtil(Form form) {
this.form = form;
}
/**
* BTDisplayProperties
*
* Displays the current values of the known properties for the
* LocalDevice.
*/
public void BTDisplayLocalDeviceProperties(LocalDevice lDevice)
{
log ("");
log ("--- properties ---");
try {
log("bt.api.version",lDevice.getProperty("bluetooth.api.version"));
}
catch (Exception ex) {
}
log ("bt.connected.inquiry.scan", lDevice.getProperty("bluetooth.connected.inquiry.scan"));
log ("bt.connected.page.scan", lDevice.getProperty("bluetooth.connected.page.scan"));
log ("bt.connected.inquiry", lDevice.getProperty("bluetooth.connected.inquiry"));
log ("bt.connected.page",lDevice.getProperty("bluetooth.connected.page"));
log ("bt.master.switch",lDevice.getProperty("bluetooth.master.switch"));
log ("bt.connected.devices.max",lDevice.getProperty("bluetooth.connected.devices.max"));
log ("bt.sd.attr.retrievable.max",lDevice.getProperty("bluetooth.sd.attr.retrievable.max"));
log ("bt.l2cap.receiveMTU.max",lDevice.getProperty("bluetooth.l2cap.receiveMTU.max"));
log ("bt.sd.trans.max",lDevice.getProperty("bluetooth.sd.trans.max"));
}
/**
* BTDisplayLocalDevice
*
* Displays the Bluetooth specific details of the local decice. The connection to
* the local device needs already be established.
*
* @param lDevice LocalDevice
*/
public void BTDisplayLocalDevice(LocalDevice lDevice)
{
log("Lcl DeviceAddress",lDevice.getBluetoothAddress());
log("Lcl DeviceName", lDevice.getFriendlyName());
log("Discover Mode", getDiscoverMode(lDevice.getDiscoverable()));
DeviceClass devClass = lDevice.getDeviceClass();
if ( devClass != null )
{
log("MajorDevice:", BTMajorDeviceName(devClass.getMajorDeviceClass()));
log("MinorDevice:", BTMinorDeviceName(devClass.getMajorDeviceClass(),
devClass.getMinorDeviceClass()));
log("ServiceClass:","");
String[] str = BTUtil.BTMajorServiceName(devClass.getServiceClasses());
for (int i = 0; i < str.length; i++) {
log(str[i]);
}
}
}
/**
* getDiscoverMode
*
* Gets back a String which reflects the current discobery mode of the
* LocalDevice.
*
* @param mode int
* @return String
*/
public String getDiscoverMode (int mode) {
String result = "";
switch (mode) {
case DiscoveryAgent.NOT_DISCOVERABLE: result+= "Not discoverable "; break;
//case DiscoveryAgent.CACHED: result += "Cached "; break;
case DiscoveryAgent.GIAC: result += "GIAC "; break;
case DiscoveryAgent.LIAC: result += "LIAC "; break;
case DiscoveryAgent.PREKNOWN: result += "Preknown "; break;
default:
result += ("0x");
result += Integer.toHexString(mode); break;
}
return result;
}
/**
* BTDisplayRemoteDevice
*
* Displays details of a given RemoteDevice.
*
* @param dev RemoteDevice
* @param devClass DeviceClass
*/
public void BTDisplayRemoteDevice( RemoteDevice dev, DeviceClass devClass )
{
cls();
try
{
log("Remote Device",dev.getBluetoothAddress());
log("Device Name",dev.getFriendlyName( false ) );
log("AuthInfo",""+dev.isAuthenticated());
log("Encrypted",""+dev.isEncrypted());
log("Trusted",""+dev.isTrustedDevice() );
if ( devClass != null )
{
log("MajorDevice", BTMajorDeviceName(devClass.getMajorDeviceClass()));
log("MinorDevice", BTMinorDeviceName(devClass.getMajorDeviceClass(),
devClass.getMinorDeviceClass()));
log("ServiceClass:","");
String[] str = BTUtil.BTMajorServiceName(devClass.getServiceClasses());
for (int i = 0; i < str.length; i++) {
log(str[i]);
}
}
}catch (IOException e)
{
log ("Error displaying RemoteDevice detasils");
}
}
/**
* BTDisplayServiceRecord
*
* Displays the details in a given ServiceRecord.
*
* @param r ServiceRecord
*/
public void BTDisplayServiceRecord( ServiceRecord record )
{
int[] AttrIDs = record.getAttributeIDs(); // get all the AttributeIDs
log("ServiceRecord Attributes",""+AttrIDs.length);
log("ServiceRecord URL",record.getConnectionURL( ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false ) );
for ( int i=0; i < AttrIDs.length; i++ )
{
DataElement el = record.getAttributeValue( AttrIDs[i] );
log ("DataElement Name", BTIDToName(AttrIDs[i]));
BTDisplayDataElement( el, AttrIDs[i]); // show mor details
}
}
/**
* BTDisplayDataElement
*
* Displays the readable details of a given DataElement in a single AttributeID.
*
* @param e DataElement
* @param id int
* @param indent String
*/
public void BTDisplayDataElement( DataElement e, int id)
{
int type = e.getDataType();
if ( type == DataElement.DATALT ||
type == DataElement.DATSEQ )
{
Enumeration enum = (Enumeration)e.getValue();
log("DataElement", " " + type + " Elements: "+e.getSize());
while ( enum.hasMoreElements() )
{
DataElement e2 = (DataElement)enum.nextElement();
BTDisplayDataElement( e2, id);
}
}
else if (type == DataElement.INT_1 ||
type == DataElement.INT_2 ||
type == DataElement.INT_4 ||
type == DataElement.INT_8 ||
type == DataElement.U_INT_1 ||
type == DataElement.U_INT_2 ||
type == DataElement.U_INT_4) {
long v = e.getLong();
log("DataElement",""+v);
} else if ( type == DataElement.UUID )
{
UUID uuid = (UUID) e.getValue();
log("DataElement", BTUUIDToName(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("DataElement", s );
} else if ( type == DataElement.STRING ||
type == DataElement.URL )
{
String v = (String) e.getValue();
log("DataElement",v);
} else if ( type == DataElement.BOOL )
{
boolean v = e.getBoolean();
log("DataElement",String.valueOf( v));
} else if ( type == DataElement.NULL )
{
log("DataElement", "--null--");
}
}
/**
* BTIDToName
*
* Gets back the readable name of a given AttributeID.
*
* @param id int
* @return String
*/
public static String BTIDToName( 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
{
return "UnknownAttribute("+id+")";
}
}
/**
* BTUUIDToName
*
* Gets back the readable description of a UUID short value according to
* the Bluetooth standard.
*
* @param uuidvalue UUID
* @return String
*/
public static String BTUUIDToName( UUID uuidvalue )
{
if ( uuidvalue.equals( new UUID( 0x0001 ) ))
return "SDP";
else if ( uuidvalue.equals( new UUID( 0x0003 ) ))
return "RFCOMM";
else if ( uuidvalue.equals( new UUID( 0x0008 ) ))
return "OBEX";
else if ( uuidvalue.equals( new UUID( 0x000C ) ))
return "HTTP";
else if ( uuidvalue.equals( new UUID( 0x0100 ) ))
return "L2CAP";
else if ( uuidvalue.equals( new UUID( 0x000F ) ))
return "BNEP";
else if ( uuidvalue.equals( new UUID( 0x1000 ) ))
return "ServiceDiscoveryServerServiceClassID";
else if ( uuidvalue.equals( new UUID( 0x1001 ) ))
return "BrowseGroupDescriptorCerviceClassID";
else if ( uuidvalue.equals( new UUID( 0x1002 ) ))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -