public void OpenAccessory(UsbAccessory accessory) { usbmanager.RequestPermission(accessory, mPermissionIntent); filedescriptor = usbmanager.OpenAccessory(accessory); if (filedescriptor != null) { usbaccessory = accessory; FileDescriptor fd = filedescriptor.FileDescriptor; inputstream = new FileInputStream(fd); outputstream = new FileOutputStream(fd); /*check if any of them are null*/ if (inputstream == null || outputstream == null) { return; } SetConfig(1200, 8, 1, 0, 0); if (READ_ENABLED == false) { READ_ENABLED = true; Task.Factory.StartNew(() => { LeerDatos.Datos(inputstream); }); //System.Threading.Thread myThread = new System.Threading.Thread(LeerDatos.Datos); //myThread.Start(inputstream); } } }
public Connection(UsbAccessory accessory, UsbManager manager, Action <byte[]> onDataReceived, Action onConnectionClosed) { Logger.LogInfo(nameof(Connection), "Constructor", "called."); try { _Accessory = accessory; _Manager = manager; _OnDataReceived += onDataReceived; _OnConnectionClosed += onConnectionClosed; OpenConnection(); _TaskCancelTokenSource = new CancellationTokenSource(); _TaskCancelToken = _TaskCancelTokenSource.Token; // Start the receivedata task in a new thread, with the given canceltoken. Task.Factory.StartNew(() => ReceiveData(), _TaskCancelToken); Logger.LogInfo(nameof(Connection), "Constructor", nameof(ReceiveData) + " task : " + "created."); } catch (Exception ex) { Logger.LogError(ex); } }
protected void findUSBSerialDevice() { if (m_usbManager == null) { m_usbManager = (UsbManager)Application.Context.GetSystemService(Context.UsbService); } if (m_usbManager != null) { UsbAccessory[] ubTempList = m_usbManager.GetAccessoryList(); PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); UsbAccessory accesory = ubTempList[0]; //m_usbManager.RequestPermission(accesory, mPermissionIntent); readSb = new StringBuffer(); bool tienepermiso = m_usbManager.HasPermission(accesory); filedescriptor = m_usbManager.OpenAccessory(accesory); if (filedescriptor != null) { writeIndex = 0; FileDescriptor fd = filedescriptor.FileDescriptor; inputstream = new FileInputStream(fd); outputstream = new FileOutputStream(fd); if (inputstream == null || outputstream == null) { return; } SetConfig(1200, 8, 1, 0, 0); if (inputstream != null) { while (true) { readcount = inputstream.Read(usbdata, 0, 1024); if (readcount > 0) { for (int count = 0; count < readcount; count++) { readBuffer[writeIndex] = usbdata[count]; readBufferToChar[writeIndex] = (char)readBuffer[writeIndex]; writeIndex++; } writeIndex = 0; readSb.Append(readBufferToChar); string p = readSb.ToString().Replace("\0", ""); if (p.Contains("2+")) { string peso = p.Substring(p.IndexOf("2+"), 7); Toast.MakeText(this, peso, ToastLength.Long).Show(); break; } } } } } } }
public static void CreateUSBConnection(Activity activity, Action <byte[]> onDataReceived, Action onConnectionClosed, Intent intent = null) { Logger.LogInfo(nameof(USB), nameof(CreateUSBConnection), "called."); if (_Instance == null) { UsbAccessory accessory = intent == null ? (UsbAccessory)activity.Intent.GetParcelableExtra(UsbManager.ExtraAccessory) : (UsbAccessory)intent.GetParcelableExtra(UsbManager.ExtraAccessory); UsbManager manager = (UsbManager)activity.GetSystemService("usb"); Logger.LogInfo(nameof(USB), nameof(CreateUSBConnection), nameof(_Instance) + " : " + "was null."); _Instance = new Connection(accessory, manager, onDataReceived, onConnectionClosed); } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); writeusbdata = new byte[256]; usbdata = new byte[1024]; readBuffer = new byte[4096]; readBufferToChar = new char[4096]; actualNumBytes = new int[1]; UsbManager m_usbManager = (UsbManager)GetSystemService(Context.UsbService); UsbReciever usbReciever = new UsbReciever(); PendingIntent mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); RegisterReceiver(usbReciever, filter); UsbAccessory[] ubTempList = m_usbManager.GetAccessoryList(); UsbAccessory accesory = ubTempList[0]; foreach (var dev in m_usbManager.DeviceList) { if (dev.Value.VendorId == 8192) { device = dev.Value; } } m_usbManager.RequestPermission(accesory, mPermissionIntent); bool hasPermision = m_usbManager.HasPermission(accesory); UsbDeviceConnection connection = m_usbManager.OpenDevice(device); if (connection == null) { return; } //_bascula = new Bascula(ApplicationContext); //Task.Factory.StartNew(() => { Read_Data(); }); //System.Threading.Thread myThread = new System.Threading.Thread(Read_Data); //myThread.Start(); //RunOnUiThread(() => Read_Data()); //findUSBSerialDevice(); }
//Constructor public Bascula(Context context) { global_context = context; usbdata = new byte[1024]; writeusbdata = new byte[256]; readBuffer = new byte[maxnumbytes]; readIndex = 0; writeIndex = 0; usbmanager = (UsbManager)context.GetSystemService(Context.UsbService); mPermissionIntent = PendingIntent.GetBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.AddAction(UsbManager.ActionUsbAccessoryDetached); inputstream = null; outputstream = null; UsbAccessory[] ubTempList = usbmanager.GetAccessoryList(); UsbAccessory accesory = ubTempList[0]; OpenAccessory(accesory); }
public UsbAccessoryPermissionReceiver(IObserver <bool> observer, UsbAccessory accessory) { this.observer = observer; this.accessory = accessory; }
/// <summary> /// Requests temporary permission for the given package to access the accessory. /// This may result in a system dialog being displayed to the user if permission had not already been granted. /// </summary> /// <returns>The observable sequence of permission values.</returns> /// <param name="manager">The UsbManager system service.</param> /// <param name="context">The Context to request the permission from.</param> /// <param name="accessory">The UsbAccessory to request permission for.</param> public static IObservable <bool> PermissionRequested(this UsbManager manager, Context context, UsbAccessory accessory) { return(Observable.Create <bool> (observer => { var usbPermissionReceiver = new UsbAccessoryPermissionReceiver(observer, accessory); context.RegisterReceiver(usbPermissionReceiver, new IntentFilter(ACTION_USB_PERMISSION)); var intent = PendingIntent.GetBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); manager.RequestPermission(accessory, intent); return Disposable.Create(() => context.UnregisterReceiver(usbPermissionReceiver)); })); }