Вообщемто все бонально, сделал класс для соединения с блютуз принтером и передачи на нго данные, но при соединении или при вызове метода GetConnectionURL, выскакивает окошко подтверждения соединения с принтером, но ровно на одну секунду а потом оно закрывается...это раз...потом если я ввожу пароль (то бишь успеваю нажать на accept в окошке , то через две секунды пока я даже его не ввел, вылезает сообщение что Pairing failed и все) вот код моего сласса, может подскажете что я не так делаю.. Код | package hello;
import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.DiscoveryListener;
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.bluetooth.LocalDevice; import javax.bluetooth.RemoteDevice; import javax.bluetooth.ServiceRecord; import javax.bluetooth.UUID; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.List; import javax.microedition.lcdui.StringItem; import javax.microedition.midlet.MIDlet;
/** * * @author palexandr */ public class BtConn implements DiscoveryListener,Runnable {
Form form; List list; Vector devices, services; boolean sear = false; MIDlet parent; private LocalDevice local; private DiscoveryAgent agent; private StreamConnection sc; private InputStream is; private OutputStream os; private String url; private boolean connectedPRN = false; private static boolean checking = false; private final static byte[] CMD_INIT = {0x1B, 0x40}; private final static byte CMD_PRN_NULL = 0x00; private final static byte CMD_PRN_RET = 0x0C; private final static byte CMD_PRN_RET_LN = 0x0A;
//private ResourceReactor rr;
/* public BtConn(String url) { try { local = LocalDevice.getLocalDevice(); agent = local.getDiscoveryAgent(); queFindDevices(); while (sear) { } this.url = url; } catch (BluetoothStateException ex) { ex.printStackTrace(); } }
public boolean connPRN() { return connectedPRN; }*/
public boolean isCheckedPRN() { return checking; }
public boolean checkConn(String Url) { try { System.out.println("Connect to " + url); sc = (StreamConnection) Connector.open("btspp://" + Url + ":1"); is = sc.openInputStream(); System.out.println("Test input stream"); os = sc.openOutputStream(); System.out.println("Test output stream"); os.write(CMD_INIT); os.flush(); is.close(); os.close(); sc.close(); connectedPRN = true; checking = true; return true; } catch (Exception ex) { ex.printStackTrace(); connectedPRN = false; checking = true; return false; } } List Servises = null; public BtConn(List list,List sevises1,MIDlet parent) { thr = new Thread(this); this.list = list; this.parent = parent; Servises = sevises1; }
public Vector getFindDevice() { return devices; }
public void SearchServices() { try { servicerecords = new Vector(); UUID[] uidset = new UUID[1]; UUID uid = new UUID(0x003); uidset[0] = uid; RemoteDevice dev = (RemoteDevice)devices.elementAt(list.getSelectedIndex()); agent.searchServices(null, uidset, dev, this); } catch(Exception ex) { ex.printStackTrace(); } }
public void FindDevices() { try { // parent.switchDisplayable(null, parent.getFormLoading()); // parent.getGuage().setValue(0);
devices = new Vector(); services = new Vector(); local = LocalDevice.getLocalDevice(); agent = local.getDiscoveryAgent(); //parent.getGuage().setValue(10); agent.startInquiry(DiscoveryAgent.GIAC, this); } catch (Exception e) { form.append(getStrCenterLay(e.getMessage())); sear = false; } }
/* public void queFindDevices() { try { devices = new Vector(); services = new Vector(); agent.startInquiry(DiscoveryAgent.GIAC, this); } catch (Exception e) { e.printStackTrace(); } }*/
public static StringItem getStrCenterLay(String str) { StringItem sit = new StringItem(null, str); sit.setLayout(StringItem.LAYOUT_CENTER); return sit; }
/* public void setUsedDevice(int numDev) { try { String addr = ((RemoteDevice) devices.elementAt(list.getSelectedIndex())).getBluetoothAddress(); sc = (StreamConnection) Connector.open("btspp://" + addr + ":1"); } catch (Exception ex) { form.deleteAll(); form.append(ex.getMessage()); } }*/
public void printCheque(String prnURL) { String [] header= null; try { header=new String[]{"================================\r\n","Company"+"\r\n","================================\r\n"}; sc = (StreamConnection) Connector.open("btspp://" + prnURL + ":1;authenticate=true"); wait(1500); is = sc.openInputStream(); os = sc.openOutputStream(); os.write(CMD_INIT); os.write(header[0].getBytes()); os.write(header[1].getBytes()); os.write(header[2].getBytes()); // os.write(XMLRPC.USING.Constants.BT_Printing_TEXT.getBytes()); os.write(CMD_PRN_RET_LN); os.flush(); is.close(); os.close(); sc.close(); } catch (Exception ex) { ex.printStackTrace(); } // parent.switchDisplayable(null, parent.getListServices()); }
public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) { devices.addElement(remoteDevice); // parent.getGuage().setValue(parent.getGuage().getValue()+20); }
Vector servicerecords = null; public void servicesDiscovered(int arg0, ServiceRecord[] arg1) { servicerecords.copyInto(arg1); }
public void serviceSearchCompleted(int arg0, int arg1) { switch(arg1) { case DiscoveryListener.SERVICE_SEARCH_COMPLETED: for(int i=0;i<servicerecords.size();i++) { Servises.append(((ServiceRecord)servicerecords.elementAt(i)).toString(), null); } break; case DiscoveryListener.SERVICE_SEARCH_ERROR: Servises.append("SERVICE_SEARCH_ERROR",null);break; case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:Servises.append("SERVICE_SEARCH_NO_RECORDS",null);break; case DiscoveryListener.SERVICE_SEARCH_TERMINATED:Servises.append("SERVICE_SEARCH_TERMINATED",null);break;
} }
public void inquiryCompleted(int param) { switch (param) { case DiscoveryListener.INQUIRY_COMPLETED: //Inquiry completed normally list.deleteAll(); if(devices.size()>0){ for (int x = 0; x < devices.size(); x++) { try { String device_name = ((RemoteDevice) devices.elementAt(x)).getFriendlyName(false); list.append(((RemoteDevice) devices.elementAt(x)).getFriendlyName(true), null);
System.out.println("Device: " + device_name); } catch (Exception e) { e.printStackTrace(); } } // parent.getGuage().setValue(100); // parent.switchDisplayable(null, list); sear = false;} else { // parent.getAlert().setString("Ни одного устройства не найдено"); // parent.switchDisplayable(parent.getAlert(), parent.getListServices()); } break; case DiscoveryListener.INQUIRY_ERROR: // Error during inquiry // parent.getAlert().setString("Ошибка при поиске"); // parent.switchDisplayable(parent.getAlert(), parent.getListServices()); sear = false; break; case DiscoveryListener.INQUIRY_TERMINATED: // Inquiry terminated by agent.cancelInquiry() // parent.getAlert().setString("Поиск отменен"); // parent.switchDisplayable(parent.getAlert(), parent.getListServices()); sear = false; break; } }
public void SetDefaultDeviceAndPrintText(int Index) { url = ((RemoteDevice) devices.elementAt(Index)).getBluetoothAddress(); //if(checkConn(url)) printCheque(url); } Thread thr = null; public void run() { thr.start(); try { devices = new Vector(); services = new Vector(); sear = true; //FindDevices(); } catch (Exception ex) { ex.printStackTrace(); form.append(getStrCenterLay(ex.getMessage())); } }
}
|
Это сообщение отредактировал(а) Выхухоль - 23.5.2009, 14:47
|