Exemple #1
0
            public override void OnReceive(Context context, Intent intent)
            {
                string action = intent.Action;

                if (BluetoothAdapter.ActionDiscoveryStarted == action)
                {
                    this._BluetoothManager.IsDiscoverying = true;
                    _BluetoothManager.OnDiscoveryStarted?.Invoke(_BluetoothManager);
                    Application.Context.UnregisterReceiver(this);
                }
                if (BluetoothDevice.ActionFound == action)
                {
                    BluetoothDevice        device        = intent.GetParcelableExtra(BluetoothDevice.ExtraDevice) as BluetoothDevice;
                    BluetoothDeviceWrapper deviceWrapper = BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(_BluetoothManager, device);

                    _BluetoothManager.OnDevicesFound?.Invoke(_BluetoothManager, new RemoteX.Bluetooth.IBluetoothDevice[] { deviceWrapper });
                }
                if (BluetoothAdapter.ActionDiscoveryFinished == action)
                {
                    _BluetoothManager.IsDiscoverying = false;
                    _BluetoothManager.OnDiscoveryFinished?.Invoke(_BluetoothManager);
                    Application.Context.UnregisterReceiver(this);
                    Application.Context.UnregisterReceiver(_BluetoothManager._DevicesFoundReceiver);
                }
            }
Exemple #2
0
            public override void OnConnectionStateChange(BluetoothDevice droidDevice, [GeneratedEnum] ProfileState status, [GeneratedEnum] ProfileState newState)
            {
                base.OnConnectionStateChange(droidDevice, status, newState);
                var bluetoothDevice = BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(GattServer.BluetoothManager, droidDevice);

                Log.Info("BLEAdver", bluetoothDevice + "OnConnectionStateChange Status:" + status + ", NewStatus:" + newState);
                bool alreadyConnected = false;

                foreach (var connectedDevice in GattServer._ConnectedDeviceList)
                {
                    if (connectedDevice == bluetoothDevice)
                    {
                        alreadyConnected = true;
                        break;
                    }
                }
                if (newState == ProfileState.Connected)
                {
                    if (!alreadyConnected)
                    {
                        GattServer._ConnectedDeviceList.Add(bluetoothDevice);
                        GattServer.DeviceConnected?.Invoke(GattServer, bluetoothDevice);
                    }
                }
                else if (newState == ProfileState.Disconnected)
                {
                    if (alreadyConnected)
                    {
                        GattServer._ConnectedDeviceList.Remove(bluetoothDevice);
                        GattServer.DeviceDisconnected?.Invoke(GattServer, bluetoothDevice);
                    }
                }
            }
Exemple #3
0
        public IBluetoothDevice GetBluetoothDevice(ulong macAddress)
        {
            byte[] addressBytesULong = BitConverter.GetBytes(macAddress);
            byte[] addressBytes      = new byte[6];
            for (int i = 0; i < addressBytes.Length; i++)
            {
                addressBytes[i] = addressBytesULong[5 - i];
            }
            BluetoothDevice device = BluetoothAdapter.GetRemoteDevice(addressBytes);

            return(BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(this, device));
        }
 public Task RunAcceptTask()
 {
     return(Task.Run(() =>
     {
         while (true)
         {
             var bluetoothSocket = ServerSocket.Accept();
             if (bluetoothSocket != null)
             {
                 var rxDevice = BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(BluetoothManager as BluetoothManager, bluetoothSocket.RemoteDevice);
                 RfcommConnection rfcommConnection = new RfcommConnection(rxDevice, bluetoothSocket);
                 _Connections.Add(rfcommConnection);
                 OnConnectionReceived?.Invoke(this, rfcommConnection);
             }
         }
     }));
 }
            public override void OnReceive(Context context, Intent intent)
            {
                string action = intent.Action;

                if (action == Android.Bluetooth.BluetoothAdapter.ActionDiscoveryStarted)
                {
                    Application.Context.UnregisterReceiver(this);
                }
                if (action == Android.Bluetooth.BluetoothDevice.ActionFound)
                {
                    Android.Bluetooth.BluetoothDevice droidDevice = intent.GetParcelableExtra(Android.Bluetooth.BluetoothDevice.ExtraDevice) as Android.Bluetooth.BluetoothDevice;
                    var deviceWrapper = BluetoothDeviceWrapper.GetBluetoothDeviceFromDroidDevice(RfcommScanner.BluetoothManager, droidDevice);
                    RfcommScanner.Added?.Invoke(RfcommScanner, deviceWrapper);
                }
                if (action == Android.Bluetooth.BluetoothAdapter.ActionDiscoveryFinished)
                {
                    Application.Context.UnregisterReceiver(this);
                    Application.Context.UnregisterReceiver(RfcommScanner.DevicesFoundReceiver);
                    RfcommScanner.Status = BluetoothRfcommScannerState.EnumerationCompleted;
                    RfcommScanner.EnumerationCompleted?.Invoke(RfcommScanner, null);
                }
            }