📄 btconnect.java
字号:
package GPSreader;
import java.util.*;
import javax.bluetooth.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class BtConnect
{
private final static int ID_SERVICE_COMPLETED = 1;
private final static int ID_DEVICE_COMPLETED = 2;
private final static int ID_DEVICE_SELECTED = 3;
public static Command COMPLETED = new Command("COMPLETED", Command.SCREEN, 1);
public static Command SELECTED = new Command("SELECTED", Command.SCREEN, 1);
public static Vector devices = new Vector();
public static Vector deviceClasses = new Vector();
public static Vector services = new Vector();
public static CommandListener callback;
private static int selectedDevice = -1;
private int deviceReturnCode, serviceReturnCode, discoveryMode;
private RemoteDeviceUI remotedeviceui = null;
private LocalDevice device = null;
private DiscoveryAgent agent = null;
private UUID[] serviceUUIDs = null;
private Display display = null;
public BtConnect(MIDlet host, CommandListener listener)
{
display = Display.getDisplay(host);
this.callback = listener;
}
public Screen getUI()
{
return remotedeviceui;
}
public ServiceRecord getFirstDiscoveredService()
{
if (services.size() > 0)
return (ServiceRecord)services.elementAt(0);
else
return null;
}
public int getDeviceDiscoveryReturnCode()
{
return deviceReturnCode;
}
public ServiceRecord[] getDiscoveredServices()
{
ServiceRecord[] r = new ServiceRecord[services.size()];
services.copyInto(r);
return r;
}
public RemoteDevice getSelectedDevice()
{
if (selectedDevice != -1)
return (RemoteDevice)devices.elementAt(selectedDevice);
else
return null;
}
public void startInquiry(RemoteDeviceUI ui)
{
try
{
this.remotedeviceui = ui;
this.discoveryMode = DiscoveryAgent.GIAC;
this.serviceUUIDs = new UUID[]{new UUID(0x1101)};
devices.removeAllElements();
deviceClasses.removeAllElements();
device = LocalDevice.getLocalDevice();
device.setDiscoverable(DiscoveryAgent.GIAC);
agent = device.getDiscoveryAgent();
boolean result = agent.startInquiry(discoveryMode, new Listener());
}
catch(BluetoothStateException e)
{
e.printStackTrace();
}
}
public void ServiceSearch(int deviceIndex)
{
services.removeAllElements();
RemoteDevice remoteDevice = (RemoteDevice)devices.elementAt(deviceIndex);
try
{
agent.searchServices(null,serviceUUIDs,remoteDevice,new Listener());
// tell callback device selected
display.callSerially(new Worker(ID_DEVICE_SELECTED));
}
catch(BluetoothStateException ex)
{
ex.printStackTrace();
}
}
private class Listener implements DiscoveryListener
{
public void deviceDiscovered(RemoteDevice remoteDevice,DeviceClass deviceClass)
{
devices.addElement(remoteDevice);
deviceClasses.addElement(deviceClass);
}
public void inquiryCompleted(int complete)
{
deviceReturnCode = complete;
if (devices.size() == 0)
{
Alert alert = new Alert("Bluetooth", "No Bluetooth device found", null, AlertType.INFO);
alert.setTimeout(5000);
remotedeviceui.showui();
display.setCurrent(alert, remotedeviceui);
}
else
{
remotedeviceui.showui();
display.setCurrent(remotedeviceui);
}
}
public void servicesDiscovered(int transId, ServiceRecord[] records)
{
for (int i=0; i< records.length; i ++)
{
ServiceRecord record = records[i];
services.addElement(record);
}
}
public void serviceSearchCompleted(int transId, int complete)
{
serviceReturnCode = complete;
// we cannot callback in this thread because this is a Bluetooth
// subsystem thread. we do not want to block it.
display.callSerially(new Worker(ID_SERVICE_COMPLETED));
}
}
private class Worker implements Runnable
{
int cmd = 0;
public Worker(int cmd)
{
this.cmd = cmd;
}
public void run()
{
switch (cmd)
{
case ID_SERVICE_COMPLETED:
callback.commandAction(COMPLETED, remotedeviceui);
break;
case ID_DEVICE_COMPLETED:
callback.commandAction(COMPLETED, remotedeviceui);
break;
case ID_DEVICE_SELECTED:
callback.commandAction(SELECTED, remotedeviceui);
break;
default:
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -