protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); RequestWindowFeature(WindowFeatures.IndeterminateProgress); SetContentView(Resource.Layout.main); var toolbar = FindViewById <V7Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); SupportActionBar.Title = GetString(Resource.String.dev_pair); permissions(); CheckBt(); SetResult(Result.Canceled); newDevicesArrayAdapter = new ArrayAdapter <string>(this, Resource.Layout.device_name); pairedDevicesArrayAdapter = new ArrayAdapter <string>(this, Resource.Layout.device_name); var DevicesListView = FindViewById <ListView>(Resource.Id.list_devices); DevicesListView.ItemClick += DeviceListView_ItemClick; devButton = FindViewById <Button>(Resource.Id.btn_dev); devButton.Click += (sender, e) => { SupportActionBar.Title = GetString(Resource.String.dev_pair); DevicesListView.Adapter = pairedDevicesArrayAdapter; pairedDevicesArrayAdapter.Clear(); DevicePair(); (sender as View).Visibility = ViewStates.Gone; scanButton.Visibility = ViewStates.Visible; }; scanButton = FindViewById <Button>(Resource.Id.btn_scan); scanButton.Click += (sender, e) => { SupportActionBar.Title = GetString(Resource.String.dev_search); DevicesListView.Adapter = newDevicesArrayAdapter; newDevicesArrayAdapter.Clear(); DoDiscovery(); (sender as View).Visibility = ViewStates.Gone; }; DevicesListView.Adapter = pairedDevicesArrayAdapter; DevicePair(); receiver = new DeviceDiscoveredReceiver(this, devButton); var filter = new IntentFilter(BluetoothDevice.ActionFound); RegisterReceiver(receiver, filter); filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished); RegisterReceiver(receiver, filter); }
public BluetoothService() { var ctx = Application.Context; // Register for broadcasts when a device is discovered _receiver = new DeviceDiscoveredReceiver(); var filter = new IntentFilter(BluetoothDevice.ActionFound); ctx.RegisterReceiver(_receiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished); ctx.RegisterReceiver(_receiver, filter); // Get the local Bluetooth adapter _btAdapter = BluetoothAdapter.DefaultAdapter; // Get a set of currently paired devices var pairedDevices = _btAdapter.BondedDevices; }
protected override void OnResume() { base.OnResume(); this.Window.DecorView.SystemUiVisibility = (StatusBarVisibility)(SystemUiFlags.LowProfile | SystemUiFlags.Fullscreen | SystemUiFlags.HideNavigation | SystemUiFlags.Immersive | SystemUiFlags.ImmersiveSticky); StartD2DInfo(); //register the broadcast receivers UsbBroadcastReceiver = new UsbDeviceReceiver(this); RegisterReceiver(UsbBroadcastReceiver, new IntentFilter(UsbManager.ActionUsbDeviceDetached)); RegisterReceiver(UsbBroadcastReceiver, new IntentFilter(UsbManager.ActionUsbDeviceAttached)); // Register for broadcasts when a device is discovered BTBroadcastReceiver = new DeviceDiscoveredReceiver(this); RegisterReceiver(BTBroadcastReceiver, new IntentFilter(BluetoothDevice.ActionFound)); RegisterReceiver(BTBroadcastReceiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished)); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Setup the window RequestWindowFeature(WindowFeatures.IndeterminateProgress); SetContentView(Resource.Layout.activity_device_list); // Set result CANCELED incase the user backs out SetResult(Result.Canceled); // Initialize the button to perform device discovery var scanButton = FindViewById <Button>(Resource.Id.button_scan); scanButton.Click += (sender, e) => { DoDiscovery(); (sender as View).Visibility = ViewStates.Gone; }; // Initialize array adapters. One for already paired devices and // one for newly discovered devices var pairedDevicesArrayAdapter = new ArrayAdapter <string>(this, Resource.Layout.device_name); newDevicesArrayAdapter = new ArrayAdapter <string>(this, Resource.Layout.device_name); // Find and setup the ListView for paired devices var pairedListView = FindViewById <ListView>(Resource.Id.paired_devices); pairedListView.Adapter = pairedDevicesArrayAdapter; pairedListView.ItemClick += DeviceListView_ItemClick; // Find and set up the ListView for newly discovered devices var newDevicesListView = FindViewById <ListView>(Resource.Id.new_devices); newDevicesListView.Adapter = newDevicesArrayAdapter; newDevicesListView.ItemClick += DeviceListView_ItemClick; // Register for broadcasts when a device is discovered receiver = new DeviceDiscoveredReceiver(this); var filter = new IntentFilter(BluetoothDevice.ActionFound); RegisterReceiver(receiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished); RegisterReceiver(receiver, filter); // Get the local Bluetooth adapter btAdapter = BluetoothAdapter.DefaultAdapter; // Get a set of currently paired devices var pairedDevices = btAdapter.BondedDevices; // If there are paired devices, add each on to the ArrayAdapter if (pairedDevices.Count > 0) { FindViewById(Resource.Id.title_paired_devices).Visibility = ViewStates.Visible; foreach (var device in pairedDevices) { pairedDevicesArrayAdapter.Add(device.Name + "\n" + device.Address); } } else { var noDevices = Resources.GetText(Resource.String.none_paired); pairedDevicesArrayAdapter.Add(noDevices); } }
protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle); AndroidEnvironment.UnhandledExceptionRaiser += MyApp_UnhandledExceptionHandler; global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App(new AndroidInitializer())); _btAdapter = BluetoothAdapter.DefaultAdapter; if (_btAdapter != null) { _blManager = new BlManager(); } /*var pairedDevices = btAdapter.BondedDevices; * if (pairedDevices.Count == 0) * { * _receiver = new DeviceDiscoveredReceiver(null); * } * else * { * var devices = pairedDevices.Select(i => new BlDevice * { * Name = i.Name, * Address = i.Address * }).ToList(); * _receiver = new DeviceDiscoveredReceiver(devices); * MessagingCenter.Send(new object(), nameof(Messages.StopDeviceses), devices); * } * var filter = new IntentFilter(BluetoothDevice.ActionFound); * RegisterReceiver(_receiver, filter); * * filter = new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished); * RegisterReceiver(_receiver, filter);*/ MessagingCenter.Subscribe <ConnectPageViewModel>(this, nameof(Messages.ScanDeviceses), (sender) => { /*if (btAdapter.IsDiscovering) * { * btAdapter.CancelDiscovery(); * } * * var x = btAdapter.StartDiscovery();*/ var pairedDevices = _btAdapter.BondedDevices; if (pairedDevices.Count == 0) { _receiver = new DeviceDiscoveredReceiver(null); } else { var devices = pairedDevices.Select(i => new BlDevice { Name = i.Name, Address = i.Address }).ToList(); _receiver = new DeviceDiscoveredReceiver(devices); MessagingCenter.Send(new object(), nameof(Messages.StopDeviceses), devices); } }); MessagingCenter.Subscribe <ConnectPageViewModel, string>(this, nameof(Messages.Connect), (sender, address) => { _blManager.ConnectDevice(address); } ); MessagingCenter.Subscribe <MainPageViewModel, string>(this, nameof(Messages.SendMessage), (sender, value) => { _blManager.SendMessage(value); }); }