mirror of
				https://github.com/mik3y/usb-serial-for-android
				synced 2025-10-30 18:07:21 +00:00 
			
		
		
		
	readme with sync + async read
This commit is contained in:
		
							parent
							
								
									6d3ed12ca8
								
							
						
					
					
						commit
						a954db1b94
					
				
							
								
								
									
										17
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								README.md
									
									
									
									
									
								
							| @ -1,3 +1,4 @@ | ||||
| [](https://github.com/mik3y/usb-serial-for-android/actions) | ||||
| [](https://jitpack.io/#mik3y/usb-serial-for-android) | ||||
| [](https://www.codacy.com/manual/kai-morich/usb-serial-for-android-mik3y?utm_source=github.com&utm_medium=referral&utm_content=mik3y/usb-serial-for-android&utm_campaign=Badge_Grade) | ||||
| [](https://codecov.io/gh/mik3y/usb-serial-for-android) | ||||
| @ -52,6 +53,7 @@ to your project's `res/xml/` directory and configure in your `AndroidManifest.xm | ||||
| 
 | ||||
| **3.** Use it! Example code snippet: | ||||
| 
 | ||||
| open device: | ||||
| ```java | ||||
|     // Find all available drivers from attached devices. | ||||
|     UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE); | ||||
| @ -71,18 +73,25 @@ to your project's `res/xml/` directory and configure in your `AndroidManifest.xm | ||||
|     UsbSerialPort port = driver.getPorts().get(0); // Most devices have just one port (port 0) | ||||
|     port.open(connection); | ||||
|     port.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE); | ||||
| ``` | ||||
| then use direct read/write | ||||
| ```java | ||||
|     port.write(request, WRITE_WAIT_MILLIS); | ||||
|     len = port.read(response, READ_WAIT_MILLIS); | ||||
| ``` | ||||
| or direct write + event driven read: | ||||
| ```java | ||||
|     usbIoManager = new SerialInputOutputManager(usbSerialPort, this); | ||||
|     Executors.newSingleThreadExecutor().submit(usbIoManager); | ||||
| ``` | ||||
| ```java | ||||
|     ... | ||||
|     port.write("hello".getBytes(), WRITE_WAIT_MILLIS); | ||||
| ``` | ||||
| ```java | ||||
|      | ||||
| @Override | ||||
| public void onNewData(byte[] data) { | ||||
|     runOnUiThread(() -> { textView.append(new String(data)); }); | ||||
| } | ||||
| ``` | ||||
| and finally: | ||||
| ```java | ||||
|     port.close(); | ||||
| ``` | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user