public override View GetView(int position, View convertView, ViewGroup parent) { var row = convertView; if (row == null) { var inflater = Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; row = inflater.Inflate(global::Android.Resource.Layout.SimpleListItem2, null); } var port = this.GetItem(position); var driver = port.GetDriver(); var device = driver.GetDevice(); var title = string.Format("Vendor {0} Product {1}", HexDump.ToHexString((short)device.VendorId), HexDump.ToHexString((short)device.ProductId)); row.FindViewById <TextView>(global::Android.Resource.Id.Text1).Text = title; var subtitle = device.Class.SimpleName; row.FindViewById <TextView>(global::Android.Resource.Id.Text2).Text = subtitle; return(row); }
/// <summary> /// The HandleMessage /// </summary> /// <param name="msg">The msg<see cref="Message"/></param> public override void HandleMessage(Message msg) { switch (msg.What) { case ServiceConstants.MESSAGE_FROM_SERIAL_PORT: byte[] recvData = (byte[])msg.Obj; string data = HexDump.ToHexString(recvData) + "\n"; //mActivity.display.Append(data); break; case ServiceConstants.CTS_CHANGE: Toast.MakeText(mActivity, "CTS_CHANGE", ToastLength.Long).Show(); break; case ServiceConstants.DSR_CHANGE: Toast.MakeText(mActivity, "DSR_CHANGE", ToastLength.Long).Show(); break; case ServiceConstants.MESSAGE_STATE_CHANGE: switch (msg.What) { case LGBluetoothService.STATE_CONNECTED: //chatFrag.SetStatus(chatFrag.GetString(Resource.String.title_connected_to, chatFrag.connectedDeviceName)); Toast.MakeText(mActivity, "Connected", ToastLength.Short).Show(); break; case LGBluetoothService.STATE_CONNECTING: //chatFrag.SetStatus(Resource.String.title_connecting); Toast.MakeText(mActivity, "Connecting...", ToastLength.Short).Show(); break; case LGBluetoothService.STATE_LISTEN: //chatFrag.SetStatus(Resource.String.not_connected); Toast.MakeText(mActivity, "Listening...", ToastLength.Short).Show(); break; case LGBluetoothService.STATE_NONE: //chatFrag.SetStatus(Resource.String.not_connected); Toast.MakeText(mActivity, "Not Connected", ToastLength.Short).Show(); break; } break; case ServiceConstants.MESSAGE_WRITE: var writeBuffer = (byte[])msg.Obj; string writeMessage = Encoding.ASCII.GetString(writeBuffer); writeMessage = System.BitConverter.ToString(writeBuffer); break; case ServiceConstants.MESSAGE_READ: var readBuffer = (byte[])msg.Obj; string readMessage = Encoding.ASCII.GetString(readBuffer); readMessage = System.BitConverter.ToString(readBuffer); break; case ServiceConstants.MESSAGE_DEVICE_NAME: if (mActivity != null) { Toast.MakeText(mActivity, $"Connected to {msg.Data.GetString(ServiceConstants.DEVICE_NAME)}.", ToastLength.Long).Show(); } break; case ServiceConstants.MESSAGE_TOAST: break; default: break; } }
// Réception de données Li-Fi avec port série micro USB private async Task OpenLiFiReceiverPort() { UsbManager UsbSerialManager = AppActivity.ApplicationContext.GetSystemService(Context.UsbService) as UsbManager; var Table = UsbSerialProber.DefaultProbeTable; Table.AddProduct(0x1b4f, 0x0008, Java.Lang.Class.FromType(typeof(CdcAcmSerialDriver))); // IOIO OTG var Prober = new UsbSerialProber(Table); var Drivers = await Prober.FindAllDriversAsync(UsbSerialManager); LiFiReceiverPort = null; foreach (var Driver in Drivers) // On cherche notre driver (le récepteur Li-Fi) { foreach (var Port in Driver.Ports) { if (HexDump.ToHexString((short)Port.Driver.Device.VendorId) == "0403" && HexDump.ToHexString((short)Port.Driver.Device.ProductId) == "6015") { LiFiReceiverPort = Port; } } } if (LiFiReceiverPort == null) // Si il n'est pas branché on affiche un message { AppActivity.RunOnUiThread(() => { ReceiverStatus.Text = "Récepteur Li-Fi absent"; }); } else { var IsPermissionGranted = await UsbSerialManager.RequestPermissionAsync(LiFiReceiverPort.Driver.Device, AppActivity.ApplicationContext); if (IsPermissionGranted) // On demande la permission à l'utilisateur d'utiliser le récepteur (Android) { SerialIOManager = new SerialInputOutputManager(LiFiReceiverPort) // Configuration du port série { BaudRate = 115200, DataBits = 8, StopBits = StopBits.One, Parity = Parity.None }; SerialIOManager.DataReceived += (source, args) => // Thread de réception de données { ReceivedSerialData = Encoding.UTF8.GetString(args.Data); // Données recu }; SerialIOManager.ErrorReceived += (source, args) => // Thread si il y a une erreur { AppActivity.RunOnUiThread(() => { ReceiverStatus.Text = "Récepteur Li-Fi absent"; // On affiche un message de débranchement SerialIOManager.Close(); }); }; try { SerialIOManager.Open(UsbSerialManager); // On ouvre le port AppActivity.RunOnUiThread(() => { ReceiverStatus.Text = "Récepteur Li-Fi opérationnel"; }); } catch (Java.IO.IOException Exception) { AppActivity.RunOnUiThread(() => { ReceiverStatus.Text = "Erreur récepteur Li-Fi: " + Exception.Message; }); } } else { AppActivity.RunOnUiThread(() => { ReceiverStatus.Text = "Permission requise"; }); } } }