CorryWestSide
Member
hi, i have to implement this kind of communication and i wanna know that i just use the correct function in java:
example of what i wanna do:
		
		
	
	
		 
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
			
			example of what i wanna do:
			
				sendDataToIrda:
			
		
		
		//invia tramite l'endpoint OUT della chiavetta USB IrDA. (ovvero la via di uscita per i dati verso il dispositivo USB, chiavetta USB IrDA prenderà questi dati e li invierà alla porta IRDA esterna.)
    public void sendDataToIrda(UsbDeviceConnection connection, UsbDevice device, byte[] data, int timeout) {
        if (connection == null) {
            BA.Log("Errore: Connessione USB non disponibile.");
            return;
        }
    
        UsbEndpoint outEndpoint = null;
        UsbInterface usbInterface = null;
    
        // Trova l'endpoint OUT
        for (int i = 0; i < device.getInterfaceCount(); i++) {
            usbInterface = device.getInterface(i);
            for (int j = 0; j < usbInterface.getEndpointCount(); j++) {
                UsbEndpoint endpoint = usbInterface.getEndpoint(j);
                if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                    outEndpoint = endpoint;
                    BA.Log("MaxPacketSize: " + outEndpoint.getMaxPacketSize());
                    break;
                }
            }
            if (outEndpoint != null) break;
        }
    
        if (outEndpoint == null) {
            BA.Log("Errore: Nessun endpoint OUT trovato! Numero interfacce: " + device.getInterfaceCount());
            return;
        } else {
            BA.Log("Endpoint OUT trovato: " + outEndpoint.getAddress());
        }
        
    
        // Reclama l'interfaccia USB
        if (usbInterface != null) {
            if (!connection.claimInterface(usbInterface, true)) {
                BA.Log("Errore: impossibile acquisire l'interfaccia USB.");
                return;
            }
        }
    
        // Invia i dati con bulkTransfer
        int result = connection.bulkTransfer(outEndpoint, data, data.length, timeout);
        /* bulkTransfer return the length of data transferred (or zero) for success, or negative value for failure*/
        if (result >= 0) BA.Log("Dati inviati con successo con bulkTransfer: " + result + " byte.");
        else BA.Log("Errore nell'invio dei dati con bulkTransfer. Codice di errore: " + result);
    }
			
				receiveDataFromIrda:
			
		
		
		//riceve tramite la chiavetta USB IrDA i dati della porta IRDa esterna
     public byte[] receiveDataFromIrda(UsbDeviceConnection connection, UsbDevice device, DDCMPProtocol protocol) {
        if (connection == null) {
            BA.Log("Errore: Connessione USB non valida.");
            return null;
        }
    
        UsbInterface usbInterface = device.getInterface(0);
        UsbEndpoint inEndpoint = null;
    
        // Trova l'endpoint di ingresso (IN)
        for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
            UsbEndpoint endpoint = usbInterface.getEndpoint(i);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
                inEndpoint = endpoint;
                break;
            }
        }
    
        if (inEndpoint == null) {
            BA.Log("Errore: Endpoint di ingresso (IN) non trovato.");
            return null;
        }
    
        ByteBuffer buffer = ByteBuffer.allocate(512); // Buffer più grande per accumulare i dati
        int bytesRead;
        int totalBytesRead = 0;
    
        long startTime = System.currentTimeMillis();
        long timeout = 3000; // 3 secondi
    
        while (System.currentTimeMillis() - startTime < timeout) {
            byte[] tempBuffer = new byte[128];
            bytesRead = connection.bulkTransfer(inEndpoint, tempBuffer, tempBuffer.length, 500);
            if (bytesRead > 0) {
                buffer.put(tempBuffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            } else {
                break;
            }
        }
    
        if (totalBytesRead > 0) {
            byte[] receivedData = Arrays.copyOf(buffer.array(), totalBytesRead);
            BA.Log("Dati ricevuti (" + totalBytesRead + " byte): " + bytesToHex(receivedData));
            return receivedData;
        } else {
            BA.Log("Timeout: Nessuna risposta ricevuta.");
            return null;
        }
    } 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		