Forum Xamarin.Android


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

USB communication, Android phone with special hardware (with phone modul and usb/bluetooth)

Hello I need information if the current version of xamarin.Android contains the implementation USB communication with other devices. I found it no examples or documentation (the documentation is solved USB for debugging, but it is not my case).

Currently I Commissioning communication with the target device via bluetooth (BluetoothAdapter, BroadcastReceiver, get bluetooth device form intent.GetParcelableExtra (BluetoothDevice.ExtraDevice) etc.).

I guess that the USB communication should be like bluetooth.

I found some info regarding UsbManager, UsbDevice, UsbAccessory, but it works differently and I'm not able to make it work.

Thank you for more info


Best Answer


  • MichalFrantaMichalFranta CZMember ✭✭

    Hi, i find that the problem was in Android version on my Phone. If i test tablet and other phone with android version 4.4.2 than it find usb device correctly.
    In doccumentation writes that it shall works on 3.1 and newer but MyPhone has 4.1.1 and does not work well. I will use newer version android and it will be OK.

    This question is answered and can be closed.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @JamieHouston I have to send some characters to the Device discovered... I can I do? Do I have to use UsbEndpoint?

  • YassineMAMAYassineMAMA Member ✭✭

    im working in android xamarin project , and i want send a string data ("WR11100_r") to serial port, so currently i can find my dvice and i can open it but i cant't send data i think i must set control Trasnfer before sending data, the config of my dvice is :
    Baud rate 9600 bits per second
    Data bits 8
    Stop bit 1
    Parity None
    Flow control No
    so i tried this code, but its dosent work :
    string cmd = "WR11100/n";
    byte[] buff = System.Text.Encoding.ASCII.GetBytes(cmd);

                        deviceConnection.ControlTransfer(0, 0, 0, 0, null, 0, 0);// reset
                        deviceConnection.ControlTransfer(0, 2, 0, 0, null, 0, 0);// flow  control none   
                        deviceConnection.ControlTransfer(UsbAddressing.Out, 3, 16696, 0, null, 0, 0);
                        deviceConnection.ControlTransfer(0, 4, 8, 0, null, 0, 0);
                        deviceConnection.BulkTransfer(usbEndpoint, buff, 0, 8, 0);

    when the bulkTransfer instrucion is exectued the buff[0] = 0 and the buff[1] = 96 so i think there is a probleme in my config can you help me plz such you was working working on a project that uses the Android device as a USB Host to connect to a separate USB device

  • delfinomoralesdelfinomorales Member ✭✭
    edited March 2020

    @YessineMAMA, crees que me puedas apoyar, yo necesito enviar datos de mi telefono a la pc por el puerto serial, he buscado informacion pero no logro entender como hacer esto.

    Te agradeceria mucho cualquier ayuda.


    Nota: Disculpa que no escriva en ingles, no domino el idioma.

  • DeepakDYDeepakDY INMember ✭✭✭
    edited June 2020

    Hello @AlessandroCaliaro

    I am working on xamarin Forms and i want to connect USB device(Dongle).
    And i implemented some code but i receiving DeviceList null

    This is my MainActivity class

    `[Activity(Label = "UsbTest", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    UsbDeviceDetachedReceiver usbReciever;
    UsbDevice device;
    private static String ACTION_USB_PERMISSION = "";

        protected override void OnCreate(Bundle savedInstanceState)
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            usbReciever = new UsbDeviceDetachedReceiver();
            UsbManager manager = (UsbManager)this.GetSystemService(Context.UsbService);
            PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            RegisterReceiver(usbReciever, filter);
            foreach (var dev in manager.DeviceList)
                if (dev.Value.VendorId == 8192)
                    device = dev.Value;
            manager.RequestPermission(device, mPermissionIntent);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        public class UsbDeviceDetachedReceiver : BroadcastReceiver
            public override void OnReceive(Context context, Intent intent)
                String action = intent.Action;
                if (ACTION_USB_PERMISSION.Equals(action))
                    lock (this)
                        UsbDevice device = (UsbDevice)intent
                        if (intent.GetBooleanExtra(
                                UsbManager.ExtraPermissionGranted, false))
                            if (device != null)
                                // call method to set up device communication

    and this is my Manifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="" android:versionCode="1" android:versionName="1.0" package="com.companyname.bluetoothtest" android:installLocation="auto">
            <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
            <application android:label="BluetoothTest.Android">
                <activity android:name=".MainActivity">
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    this is my device_filter.xml

        <?xml version="1.0" encoding="utf-8" ?> 
            <!-- 0x0403 FTDI -->
            <usb-device vendor-id="1027" product-id="24577" /> <!-- 0x6001: FT232R -->
            <usb-device vendor-id="1027" product-id="24592" /> <!-- 0x6010: FT2232H -->
            <usb-device vendor-id="1027" product-id="24593" /> <!-- 0x6011: FT4232H -->
            <usb-device vendor-id="1027" product-id="24596" /> <!-- 0x6014: FT232H -->
            <usb-device vendor-id="1027" product-id="24597" /> <!-- 0x6015: FT231X -->
            <!-- 0x10C4 / 0xEAxx: Silabs CP210x -->
            <usb-device vendor-id="4292" product-id="60000" /> <!-- 0xea60: CP2102 -->
            <usb-device vendor-id="4292" product-id="60016" /> <!-- 0xea70: CP2105 -->
            <usb-device vendor-id="4292" product-id="60017" /> <!-- 0xea71: CP2108 -->
            <usb-device vendor-id="4292" product-id="60032" /> <!-- 0xea80: CP2110 -->
            <!-- 0x067B / 0x2303: Prolific PL2303 -->
            <usb-device vendor-id="1659" product-id="8963" />
            <!-- 0x1a86 / 0x7523: Qinheng CH340 -->
            <usb-device vendor-id="6790" product-id="29987" />
            <!-- CDC driver -->
            <usb-device vendor-id="9025" />                   <!-- 0x2341 / ......: Arduino -->
            <usb-device vendor-id="5824" product-id="1155" /> <!-- 0x16C0 / 0x0483: Teensyduino  -->
            <usb-device vendor-id="1003" product-id="8260" /> <!-- 0x03EB / 0x2044: Atmel Lufa -->
            <usb-device vendor-id="7855" product-id="4"    /> <!-- 0x1eaf / 0x0004: Leaflabs Maple -->
            <usb-device vendor-id="3368" product-id="516"  /> <!-- 0x0d28 / 0x0204: ARM mbed -->

    But i am receiving manager.DeviceList null on MainActivity OnCreate method and also BroadcastReceiver OnReceive method not calling

    Where i am wrong.

Sign In or Register to comment.