2012-12-13 218 views
7

我正在實施一個應用程序。如何通過藍牙配對zebra mz320打印機?

根據我的要求,我想通過移動藍牙從zebra mz320打印機打印輸出。

我試圖讓從mobile bluetooth一對以printer bluetoth.

當我嘗試配對,打印機拋出類似「1234型或0000 PIN爲make 連接的消息。

我鍵入相同PIN碼。

但打印機沒有配對到我的移動設備。

它拋出像com.zebra.android.comm.ZebraPrinterConnectionException: Could not connect to printer: Unable to start Service Discovery

異常

如果有人知道解決方案,請幫助我。
在此先感謝。

+0

我有同樣的問題。你找到了解決方案嗎? –

+0

您需要獲取打印機的UUID。 listenUsingRfcommWithServiceRecord(字符串名稱,UUID uuid) – SeahawksRdaBest

回答

1

UUID列表請參閱here。 你應該嘗試從這個片段的一個或所有隊列進行連接:

@TargetApi(10)私人的BluetoothSocket connectDeviceUsingAPI10()拋出IOException異常{

BluetoothSocket socket = null; 
IOException ioex = null; 
int port = 1; // may be from 1 to 14 if I'm not confused 
UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

// way #0. Connect using workaround for Android < 2.3 
try { 
    if (!isThreadActive) 
     return null; 
    Log.d("Try via API10: createInsecureRfcommSocketToServiceRecord"); 
    socket = mDevice.createInsecureRfcommSocketToServiceRecord(SPP_UUID); // or RFCOMM_UUID); 
    } catch (IOException e) { ioex = e; } 
     if (socket != null && ioex == null) { 
      try { 
       socket.connect(); 
       setStreams(socket.getOutputStream(), socket.getInputStream()); 
      } catch (IOException ex) { 
       ioex = ex; 
       try { 
        socket.close(); 
       } catch (IOException e) { 
       } finally { 
        socket = null; 
       } 
      } 
     } 
     if (socket != null && ioex == null) { 
      return socket; 

     } 

     ioex = null; 
     socket = null; 
     // way #1. Using standard secure connection procedure via UUID 
     try { 
      if (!isThreadActive) 
       return null; 
      Log.d("Try via API10: createRfcommSocketToServiceRecord"); 
      socket = mDevice 
        .createRfcommSocketToServiceRecord(SPP_UUID);// or RFCOMM_UUID 
     } catch (IOException e) { 
      ioex = e; 
     } 
     if (socket != null && ioex == null) { 
      try { 
       socket.connect(); 
       setStreams(socket.getOutputStream(), socket.getInputStream()); 
      } catch (IOException ex) { 
       ioex = ex; 
       try { 
        socket.close(); 
       } catch (IOException e) { 
       } finally { 
        socket = null; 
       } 
      } 
     } 
     if (socket != null && ioex == null) { 
      return socket; 
     } 

     // way #2. Using hidden api procedure with insecure socket 
     socket = null; 
     ioex = null; 
     // Try to fallback to API5 method 
     try { 
      if (!isThreadActive) 
       return null; 
      Log.d("Try via API10: createInsecureRfcommSocket"); 
      Method m = mDevice.getClass().getMethod(
        "createInsecureRfcommSocket", new Class[] { int.class }); 
      socket = (BluetoothSocket) m.invoke(mDevice, Integer.valueOf(port)); 
     } catch (IOException e) { // ... } 
     if (socket != null && ioex == null) { 
      try { 
       socket.connect(); 
       setStreams(socket.getOutputStream(), socket.getInputStream()); 
      } catch (IOException ex) { 
       ioex = ex; 
       try { 
        socket.close(); 
       } catch (IOException e) { 
       } finally { 
        socket = null; 
       } 
      } 
     } 

     if (socket != null && ioex == null) { 
      return socket; 
     } 

     ioex = null; 
     socket = null; 
     // way #3. Connect using workaround for Android < 2.3 
     try { 
      if (!isThreadActive) 
       return null; 
      Log.d("Try via API10: createRfcommSocket"); 
      Method m = mDevice.getClass().getMethod("createRfcommSocket", 
        new Class[] { int.class }); 
      socket = (BluetoothSocket) m.invoke(mDevice, Integer.valueOf(port)); 
     } catch (IOException e) { 
       ioex = e; 
     } 
     if (socket != null && ioex == null) { 
      try { 
       socket.connect(); 
       setStreams(socket.getOutputStream(), socket.getInputStream()); 
      } catch (IOException ex) { 
       ioex = ex; 
       try { 
        socket.close(); 
       } catch (IOException e) { 
       } finally { 
        socket = null; 
       } 
      } 
     } 
     if (socket != null && ioex == null) { 
      return socket; 
     } 
     return socket; 
    }