public override void OnReceive(Context context, Intent intent) { string action = intent.Action; // When discovery finds a device if (action == BluetoothDevice.ActionFound) { // Get the BluetoothDevice object from the Intent BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice); // If it's already paired, skip it, because it's been listed already if (device.BondState != Bond.Bonded) { DeviceBLE item = new DeviceBLE { Name = device.Name, Address = device.Address }; _newDevices.Add(item); } } else if (action == BluetoothAdapter.ActionDiscoveryFinished) { _tcScan.TrySetResult(_newDevices); } }
public async Task <bool> ConnectAsync(DeviceBLE info) { var device = _btAdapter.GetRemoteDevice(info.Address); // Connect as a Client try { _clientSocket = device.CreateRfcommSocketToServiceRecord(MY_UUID_SECURE); await _clientSocket.ConnectAsync(); } catch (System.Exception ex) { try { // https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3/25647197#25647197 _clientSocket = (BluetoothSocket)device.Class.GetMethod("createRfcommSocket", Integer.Type).Invoke(device, 1); await _clientSocket.ConnectAsync(); } catch (System.Exception ex2) { } } //try //{ // // Connect as a Server // _serverSocket = _btAdapter.ListenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID_SECURE); // BluetoothSocket socket = null; // // Listener // while (true) // { // try // { // socket = _serverSocket.Accept(); // } // catch (Java.IO.IOException e) // { // break; // } // if (socket != null) // { // lock (this) // { // try // { // socket.Close(); // } // catch (Java.IO.IOException e) // { // } // break; // } // } // } //} //catch (System.Exception ex) //{ //} _inStream = _clientSocket.InputStream; return(true); }
private void ItemTappedHandle(DeviceBLE device) { HandleSelectedDevice(device); }
public Task <bool> Disconnect(DeviceBLE device) { _clientSocket?.Close(); _serverSocket?.Close(); return(Task.FromResult(true)); }
private void HandleSelectedDevice(DeviceBLE device) { }