public override void OnReceive(Context context, Intent intent) { string action = intent.Action; if (USBCommunicator.ACTION_USB_PERMISSION.Equals(action)) { lock (this) { UsbDevice device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice); if (intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false)) { if (device != null) { //call method to set up accessory communication if (_communicator.UsbDevice.DeviceId == device.DeviceId) { _communicator.Start(); } } } else { Log.Debug("CDC", "permission denied for accessory " + device.DeviceName); } } } else if (UsbManager.ActionUsbDeviceAttached.Equals(action)) { UsbDevice device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice); if (device != null) { _communicator.Connect(); // call your method that cleans up and closes communication with the accessory } } else if (UsbManager.ActionUsbDeviceDetached.Equals(action)) { UsbDevice device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice); if (device != null) { _communicator.Stop(); // call your method that cleans up and closes communication with the accessory } } }
protected override void OnResume() { base.OnResume(); usbHandler = new Handler( (message) => { //switch ((USBDeviceStatus)message.What) //{ // case USBDeviceStatus.UsbReading: // break; // case USBDeviceStatus.DeviceConnectionClosed: // break; //} if (message.What == 111) { string raw = message.Data.GetString("raw"); _usb_DataReceived(this, new BeaconInfoEventArgs(BeaconInfoData.FromString(raw), raw)); } }); _usb = new USBCommunicator(this, usbHandler); _usb.DataReceived += _usb_DataReceived; _usb.Connect(); }