📄 bluedumpdiscoverythread.java
字号:
//------------------------------------------------------------------
//Copyright (c) 2004 Jyperion SARL
// France
//
//This software is provided "AS IS," without a warranty of any kind.
//ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
//INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
//PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
//------------------------------------------------------------------
package example.bluetooth.dump;
import java.io.IOException;
import javax.bluetooth.*;
/**
* This class is the worker thread that will be launched to make a Bluetooth discovery
* An inquiry could take an important amount of time, that is a reason to implement it inside a Thread to make a background task
*
* @author Guillaume Compagnon
*
*/
public class BlueDumpDiscoveryThread implements Runnable ,DiscoveryListener{
/** Bluetooth discovery agent */
private DiscoveryAgent discoveryAgent;
public void run() {
LocalDevice localDevice;
try {
localDevice = LocalDevice.getLocalDevice();
}
catch (BluetoothStateException e) {
if( BlueDump.DEBUG) e.printStackTrace();
BlueDump.display("Bluetooth connection should be activated on this device",BlueDumpGUI.RED);
//Will exit the application
BlueDump.fireExitAlert();
return;
}
// get the bluetooth discovery agent
discoveryAgent = localDevice.getDiscoveryAgent();
try {
RemoteDevice devices[];
// Lookup all the bluetooth devices in the neighbourhood
devices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACHED);
if (devices != null) {
for(int i=0; i<devices.length;i++)
BlueDump.singleton.bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);
}
devices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREKNOWN);
if (devices != null) {
for(int i=0; i<devices.length;i++)
BlueDump.singleton.bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);
}
// start an Inquiry with GIAC : General/Unlimited Inquiry Access Code
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
// start an Inquiry with LIAC : Limited Dedicated Inquiry Access Code
discoveryAgent.startInquiry(DiscoveryAgent.LIAC, this);
}
catch (BluetoothStateException e) {
if( BlueDump.DEBUG ) e.printStackTrace();
}catch (IOException e) {
if( BlueDump.DEBUG ) e.printStackTrace();
}
}
// ************************************************
// Implement javax.bluetooth.DiscoveryListener interface
// ************************************************
/** Called when a device is found during an inquiry. */
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
if( BlueDump.DEBUG ) System.out.println("deviceDiscovered: " + btDevice.getFriendlyName(false));
// Add Device to a list
BlueDump.singleton.bluetoothDevices.put( btDevice.getFriendlyName(false),btDevice);
BlueDump.display("***** Devices Discovered ******", BlueDumpGUI.BLUE);
BlueDump.display("FriendlyName:"+btDevice.getFriendlyName(false));
BlueDump.display("Address :"+btDevice.getBluetoothAddress());
BlueDump.display("DeviceClass :"+cod);
BlueDump.display("*******************************", BlueDumpGUI.BLUE);
}
catch (Exception e) {
if( BlueDump.DEBUG ) e.printStackTrace();
}
}
/** Called when service(s) are found during a service search. */
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
if( BlueDump.DEBUG ) System.out.println("servicesDiscovered: " + transID + " length = " + servRecord.length);
int attribs[];
DataElement data;
BlueDump.display("***** Services Discovered ******", BlueDumpGUI.BLUE);
BlueDump.display("Transaction ID:"+transID);
for (int i=0; i < servRecord.length; i++) {
BlueDump.singleton.bluetoothDevices.put( servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT , false), servRecord[i]);
BlueDump.display("ConnectionURL:"+(servRecord[i]).getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT , false));
}
BlueDump.display("********************************", BlueDumpGUI.BLUE);
}
/** Called when a service search is completed or was terminated because of an error. */
public void serviceSearchCompleted(int transID, int respCode) {
BlueDump.display("***** Services Search Completed ******", BlueDumpGUI.BLUE);
BlueDump.display("Transaction ID:"+transID);
if ( respCode == DiscoveryListener.SERVICE_SEARCH_TERMINATED )
BlueDump.display("Response Code:SERVICE_SEARCH_TERMINATED");
else if(respCode == DiscoveryListener.SERVICE_SEARCH_COMPLETED )
BlueDump.display("Response Code:SERVICE_SEARCH_COMPLETED");
else if(respCode == DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE )
BlueDump.display("Response Code:SERVICE_SEARCH_DEVICE_NOT_REACHABLE");
else if(respCode == DiscoveryListener.SERVICE_SEARCH_ERROR )
BlueDump.display("Response Code:SERVICE_SEARCH_ERROR");
else if(respCode == DiscoveryListener.SERVICE_SEARCH_NO_RECORDS )
BlueDump.display("Response Code:SERVICE_SEARCH_NO_RECORDS");
BlueDump.display("********************************", BlueDumpGUI.BLUE);
}
/** Called when an inquiry is completed */
public void inquiryCompleted(int discType) {
BlueDump.display("***** Inquiry Completed ******", BlueDumpGUI.BLUE);
if ( discType == DiscoveryListener.INQUIRY_COMPLETED )
BlueDump.display("Response Code:INQUIRY_COMPLETED");
else if(discType == DiscoveryListener.INQUIRY_ERROR )
BlueDump.display("Response Code:INQUIRY_ERROR");
else if(discType == DiscoveryListener.INQUIRY_TERMINATED )
BlueDump.display("Response Code:INQUIRY_TERMINATED");
BlueDump.display("********************************", BlueDumpGUI.BLUE);
}
// ************************************************
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -