2014-11-08 95 views
0

我正在嘗試與USB附件(磁條讀卡器,型號-E-Seek M250)進行通信,其中Nexus 7充當USBHost。解碼來自讀卡器的數據

使用案例:當刷卡時,我需要從卡中獲取詳細信息並將其轉換爲用戶可讀的格式。

我已經能夠成功獲取設備,其接口和輸入端點。 之後,這是我在做什麼來獲取數據:

int receivedBytes = mConnection.bulkTransfer(usbEndpointIN, readBytes, readBytes.length, 3000); 
if (receivedBytes > 2) { 
    dataString = new String(readBytes); 
    Log.v(Util.TAG, " :: Received Byte Count ::" + receivedBytes); 
    Log.v(Util.TAG, " :: Final Value Bytes" + readBytes); 
    Log.v(Util.TAG, " :: Final Value String" + dataString); 
} 

多次嘗試後,我無法找到一個辦法讓用戶可讀格式的數據,下面是數據在顯示方式日誌。

logs

任何人都可以讓我知道如何將這些數據轉換成用戶可讀的格式?

回答

1

該閱讀器未加密,因此可能是編碼問題。檢查閱讀器的文檔,看看它們用於卡數據的編碼類型,並在將字節數組傳遞給它時使用該編碼。以下是使用UTF-8的例子。

int receivedBytes = mConnection.bulkTransfer(usbEndpointIN, readBytes, readBytes.length, 3000); 
if (receivedBytes > 2) { 
    String dataString = null; 
    Log.v(Util.TAG, " :: Received Byte Count ::" + receivedBytes); 
    Log.v(Util.TAG, " :: Final Value Bytes" + readBytes); 

    try { 

     dataString = new String(readBytes, "UTF-8"); 
     Log.v(Util.TAG, " :: Final Value String" + dataString); 

    } catch (UnsupportedEncodingException e) { 

     e.printStackTrace(); 

    } 

} 
相關問題