📄 bluetoothsearch.java
字号:
/* * Movino J2ME Client * Copyright (C) 2007 Johannes Berg * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package org.movino.connection;import java.io.IOException;import javax.bluetooth.BluetoothStateException;import javax.bluetooth.DeviceClass;import javax.bluetooth.DiscoveryAgent;import javax.bluetooth.DiscoveryListener;import javax.bluetooth.LocalDevice;import javax.bluetooth.RemoteDevice;import javax.bluetooth.ServiceRecord;import javax.bluetooth.UUID;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.List;import javax.microedition.lcdui.Ticker;import org.movino.Returnable;public class BluetoothSearch extends Returnable implements CommandListener, DiscoveryListener{ public static Image phoneIcon=null; public static Image desktopIcon=null; static{ try { phoneIcon = Image.createImage("/phone.png"); desktopIcon = Image.createImage("/desktop.png"); } catch (IOException e) {} } private List deviceList; //private Command selectBTDevCommand; private Command abortBTDevCommand; private RemoteDevice[] remoteDevices; private boolean searchCompleted=false; private boolean serviceSearching=false; private boolean searchAborted; public BluetoothSearch(){} public void show(Display display){ deviceList = new List("BT devices",List.IMPLICIT); deviceList.setTicker(new Ticker("Searching for devices...")); //selectBTDevCommand = new Command("Select", Command.ITEM, 1); abortBTDevCommand = new Command("Cancel", Command.BACK, 1); deviceList.setSelectCommand(List.SELECT_COMMAND); //deviceList.addCommand(selectBTDevCommand); deviceList.addCommand(abortBTDevCommand); deviceList.setCommandListener(this); remoteDevices = new RemoteDevice[0]; display.setCurrent(deviceList); try { if(!startSearch()){ alert("Could not start device inquiry. You may need to activate bluetooth in your device."); } } catch (BluetoothStateException e) { alert("Could not start device inquiry "+e+" "+e.getMessage()); } } public void reShow(Display display){ display.setCurrent(deviceList); } public boolean startSearch() throws BluetoothStateException{ searchAborted=false; searchCompleted=false; LocalDevice device = LocalDevice.getLocalDevice(); if(device==null) return false; DiscoveryAgent da = device.getDiscoveryAgent(); if(da==null) return false; return da.startInquiry(DiscoveryAgent.GIAC, this); } public void commandAction(Command cmd, Displayable arg1) { if(/*cmd==selectBTDevCommand || */cmd==List.SELECT_COMMAND){ if(!serviceSearching){ int index = deviceList.getSelectedIndex(); if(index>=0){ RemoteDevice dev = remoteDevices[index]; try { LocalDevice device = LocalDevice.getLocalDevice(); DiscoveryAgent da = device.getDiscoveryAgent(); da.searchServices(null, new UUID[]{new UUID("1027392E",true)}, dev, this); deviceList.setTicker(new Ticker("Searching for movino service...")); serviceSearching=true; } catch (BluetoothStateException e) {} } } } else if(cmd==abortBTDevCommand){ searchAborted=true; returnBack(); } } public void deviceDiscovered(RemoteDevice dev, DeviceClass dev_class) { if(searchAborted) return; RemoteDevice[] new_dev = new RemoteDevice[remoteDevices.length+1]; System.arraycopy(remoteDevices, 0, new_dev, 0, remoteDevices.length); new_dev[new_dev.length-1] = dev; try { int maj = dev_class.getMajorDeviceClass(); Image icon=null; if(maj==0x200) icon = phoneIcon; else icon = desktopIcon; deviceList.append(dev.getFriendlyName(true), icon); remoteDevices = new_dev; } catch (IOException e) {} } public void inquiryCompleted(int disc_type) { if(searchAborted) return; searchCompleted=true; if(!serviceSearching){ deviceList.setTicker(null); if(remoteDevices.length<=0){ returnBack(); if(disc_type==DiscoveryListener.INQUIRY_COMPLETED) alert("Could not find any nearby device."); else alert("Device inquiry failed. You may need to activate bluetooth in your device"); } } } public void serviceSearchCompleted(int arg0, int resp_code) { if(searchAborted) return; deviceList.setTicker(null); serviceSearching=false; if(getDisplay().getCurrent()==deviceList){ //service not found if(resp_code!=DiscoveryListener.SERVICE_SEARCH_COMPLETED){ alert("Could not find Movino service on target device."); } } } public void servicesDiscovered(int arg0, ServiceRecord[] srs) { if(searchAborted) return; if(srs.length>0){ String url = srs[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); ConnectionDialog cd = new ConnectionDialog(new BluetoothConnection(url, srs[0].getHostDevice())); cd.show(getDisplay(), getReturnShowable()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -