内部蓝牙打印机错误 - PrinterInterface.open失败

时间:2017-03-05 13:20:09

标签: android bluetooth

我为具有内部蓝牙设备的wizarhand设备开发了一个应用程序。我可以连接所有外部蓝牙设备,但在连接内部设备时,我收到此错误:

  

在PrinterInterface.open上失败

这是我的代码:

public BTPrinter(String namePrefix) throws Exception {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        Set pairedDevices = btAdapter.getBondedDevices();
        String address = null;
        Iterator e = pairedDevices.iterator();

        BluetoothDevice device;
        while(e.hasNext()) {
            device = (BluetoothDevice)e.next();
            if(device.getName().equals("WizarPOS_Printer")) {
                address = device.getAddress();
                this.deviceName = device.getName();
                break;
            }
        }

        if(address == null) {
            Log.d("BTPrinter", "Bluetooth Printer Not Found.");
        }

        device = btAdapter.getRemoteDevice(address);

        try {
            if(Build.VERSION.SDK_INT >= 10) {
                try {
                    this.btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                } catch (Exception var12) {
                    ;
                }
            }

            if(this.btSocket == null) {
                this.btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
            }
        } catch (IOException var13) {
            Log.d("BTPrinter", "Failed to create bluetooth socket.");
        }

        btAdapter.cancelDiscovery();

        try {
            this.btSocket.connect();
        } catch (Exception var11) {
            Log.d("BTPrinter", "Failed to connect to printer.");

            try {
                this.btSocket.close();
            } catch (Exception var10) {
                ;
            }
        }

        if(!this.btSocket.isConnected()) {
            try {
                Thread.sleep(400L);
            } catch (InterruptedException var8) {
                Thread.currentThread().interrupt();
            }

            throw new Exception("Bluetooth connection issue");
        } else {
            try {
                this.outStream = this.btSocket.getOutputStream();
                this.inStream = this.btSocket.getInputStream();
                Log.d("BTPrinter", "Connected to " + this.deviceName + ".");
            } catch (IOException var9) {
                throw new Exception("Bluetooth connection issue");
            }
        }
    }

0 个答案:

没有答案