📄 bluetoothdevices.java
字号:
package com.btsendfile;
import javax.bluetooth.*;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.IOException;
import java.lang.Thread;
import javax.swing.JFrame;
import java.awt.List;
/**
*
* @author vudra
*/
public class BluetoothDevices extends JFrame implements Runnable, DiscoveryListener {
LocalDevice local;
private DiscoveryAgent discoveryAgent;
private Hashtable bluetoothDevices = new Hashtable();
List deviceList;
private String btAddress;
public BluetoothDevices() {
// Device List
deviceList = new List(0, false);
}
private void doBluetoothDiscovery() {
Thread t = new Thread(this);
t.start();
}
// Implement Runnable
public void run() {
bluetoothDiscovery();
}
public void printString(String s) {
System.out.println(s);
}
public void bluetoothDiscovery() {
LocalDevice localDevice;
try {
localDevice = LocalDevice.getLocalDevice();
} catch (BluetoothStateException e) {
printString("BluetoothStateException: " + e);
return;
}
discoveryAgent = localDevice.getDiscoveryAgent();
RemoteDevice devices[] = null;
try {
//devices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACHED);
if (devices != null) {
for (int i = 0; i < devices.length; i++) {
bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);
printString("Device (cached): " + devices[i].getFriendlyName(false) + " " + devices[i].getBluetoothAddress());
}
}
//devices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREKNOWN);
if (devices != null) {
for (int i = 0; i < devices.length; i++) {
bluetoothDevices.put(devices[i].getFriendlyName(false), devices[i]);
printString("Device (cached): " + devices[i].getFriendlyName(false) + " " + devices[i].getBluetoothAddress());
}
}
} catch (IOException e) {
printString("Exception (b2): " + e);
}
try {
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e) {
printString("Exception (b3): " + e);
}
printString("return from bluetoothDiscovery()");
}
private void bluetoothCopyDeviceTableToList() {
// Remove old entries
//for (int i = deviceList.size(); i > 0; i--) {
//deviceList.delete(i - 1);
//}
// Append keys from table
for (Enumeration e = bluetoothDevices.keys(); e.hasMoreElements();) {
try {
String name = (String) e.nextElement();
//deviceList.add(name, null);
} catch (Exception exception) {
}
}
}
// ************************************************
// Implement BT DiscoveryListener
// ************************************************
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
printString("deviceDisc: " + btDevice.getFriendlyName(false));
printString("deviceAddress: " + btDevice.getBluetoothAddress());
btAddress = btDevice.getBluetoothAddress();
// Add Device to table
bluetoothDevices.put(btDevice.getFriendlyName(false), btDevice);
} catch (Exception e) {
printString("Exception (b4): " + e);
}
}
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
}
public void serviceSearchCompleted(int transID, int respCode) {
}
public void inquiryCompleted(int discType) {
printString("inquiryCompleted! " + discType);
bluetoothCopyDeviceTableToList();
}
public String getBtAddress() {
return btAddress;
}
public void setBtAddress(String btAddress) {
this.btAddress = btAddress;
}
// ************************************************
// ************************************************
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BluetoothDevices bluetoothDevices = new BluetoothDevices();
bluetoothDevices.bluetoothDiscovery();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -