/// <summary> /// /// </summary> /// <param name="savedInstanceState"></param> public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); _bluetoothAction = (BluetoothAction)Arguments.GetInt(ARG_REQUEST_BLUETOOTH_ACTION); _bluetoothDevice = (BluetoothDevice)Arguments.GetParcelable(ARG_REQUEST_BLUETOOTH_DEVICE); _requestCode = Arguments.GetInt(ARG_REQUEST_CODE); if (_activity.Bluetooth == null) { _activity.Bluetooth = new Bluetooth(); } _scanStarted = false; }
//protected override Fragment CreateFragment() //{ // return null;//BluetoothFragment.NewInstance(this as MainActivity); //} protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.bluetooth_utils_fragment); Button search = FindViewById <Button>(Resource.Id.search_button); ListView listview = FindViewById <ListView>(Resource.Id.device_list); if (mBluetooth == null) { mBluetooth = new Bluetooth(); } mBluetoothAdapter = BluetoothAdapter.DefaultAdapter; mBluetoothReceiver = new BluetoothReceiver(this); IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound); filter.AddAction(BluetoothAdapter.ActionDiscoveryFinished); filter.AddAction(BluetoothDevice.ActionPairingRequest); filter.AddAction(BluetoothDevice.ActionBondStateChanged); filter.AddAction(BluetoothAdapter.ActionDiscoveryStarted); RegisterReceiver(mBluetoothReceiver, filter); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothDevice.ActionBondStateChanged)); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryFinished)); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothAdapter.ActionDiscoveryStarted)); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothDevice.ActionFound)); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothDevice.ActionPairingRequest)); RegisterReceiver(mBluetoothReceiver, new IntentFilter(BluetoothAdapter.ActionRequestDiscoverable)); search.Click += delegate { mBluetoothAction = BluetoothAction.GetListOfAvailableDevices; mScanStarted = true; // cancel any requests which may be in progress mBluetoothAdapter.CancelDiscovery(); mBluetoothAdapter.StartDiscovery(); }; listview.ItemClick += delegate { }; }
/// <summary> /// /// </summary> /// <param name="inflater"></param> /// <param name="container"></param> /// <param name="savedInstanceState"></param> /// <returns></returns> public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { _parentView = inflater.Inflate(Resource.Layout.bluetooth_utils_fragment, container, false); _buttonScan = _parentView.FindViewById <Button>(Resource.Id.search_button); _buttonScan.Text = "Search"; _buttonScan.Click += delegate { // reset so spinner selectionused _bluetoothAction = BluetoothAction.GetListOfAvailableDevices; _scanStarted = true; // cancel any requests which may be in progress _activity.Bluetooth.Adapter.CancelDiscovery(); _activity.Bluetooth.Adapter.StartDiscovery(); }; _deviceFoundList = _parentView.FindViewById <ListView>(Resource.Id.device_list); _deviceFoundList.Adapter = new BluetoothDeviceArrayAdapter(_parentView.Context, Resource.Layout.row_layout, _deviceList); _deviceFoundList.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) { _bluetoothDevice = _deviceList[e.Position]; _deviceFoundList.SetSelection(e.Position); (_deviceFoundList.Adapter as BluetoothDeviceArrayAdapter).SetSelectedIndex(e.Position); Toast.MakeText(_parentView.Context, "Copied " + _bluetoothDevice.Name + " to clipboard.", ToastLength.Long).Show(); }; Button buttonOK = _parentView.FindViewById <Button>(Resource.Id.buttonOK); buttonOK.Click += delegate { var returnIntent = new Intent(); if (_bluetoothDevice != null) { returnIntent.PutExtra("BluetoothDeviceName", _bluetoothDevice.Name); returnIntent.PutExtra("BluetoothDeviceAddress", _bluetoothDevice.Address); } _buttonOkClicked.BluetoothFragmentOnOKButtonClicked(returnIntent, (int)ActivityCode.Bluetooth); return; }; _success = false; switch (_bluetoothAction) { case BluetoothAction.UnpairDevice: { _activity.Bluetooth.Device = _bluetoothDevice; _activity.Bluetooth.UnpairDevice(); } break; case BluetoothAction.GetListOfAvailableDevices: { _scanStarted = true; _activity.Bluetooth.Adapter.CancelDiscovery(); _activity.Bluetooth.Adapter.StartDiscovery(); } break; case BluetoothAction.PairDevice: { _activity.Bluetooth.Device = _bluetoothDevice; if (_activity.Bluetooth.IsDevicePaired()) { break; } _deviceList.Clear(); _deviceList.Add(_bluetoothDevice); _activity.Bluetooth.Adapter.CancelDiscovery(); _activity.Bluetooth.Device.CreateBond(); } break; } return(_parentView); }