public override void OnReceive(Context context, Intent intent)
        {
            var bondState = (Bond)intent.GetIntExtra(BluetoothDevice.ExtraBondState, (int)Bond.None);
            var device = new Device((BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice), null, null, 0);
            Console.WriteLine(bondState.ToString());

            if (BondStateChanged == null) return;

            switch (bondState)
            {
                case Bond.None:
                    BondStateChanged(this, new DeviceBondStateChangedEventArgs() { Device = device, State = DeviceBondState.NotBonded });
                    break;

                case Bond.Bonding:
                    BondStateChanged(this, new DeviceBondStateChangedEventArgs() { Device = device, State = DeviceBondState.Bonding });
                    break;

                case Bond.Bonded:
                    BondStateChanged(this, new DeviceBondStateChangedEventArgs() { Device = device, State = DeviceBondState.Bonded });
                    break;

            }
        }
示例#2
0
            public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
            {
                base.OnScanResult(callbackType, result);

                /* Might want to transition to parsing the API21+ ScanResult, but sort of a pain for now 
                List<AdvertisementRecord> records = new List<AdvertisementRecord>();
                records.Add(new AdvertisementRecord(AdvertisementRecordType.Flags, BitConverter.GetBytes(result.ScanRecord.AdvertiseFlags)));
                if (!string.IsNullOrEmpty(result.ScanRecord.DeviceName))
                {
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.CompleteLocalName, Encoding.UTF8.GetBytes(result.ScanRecord.DeviceName)));
                }
                for (int i = 0; i < result.ScanRecord.ManufacturerSpecificData.Size(); i++)
                {
                    int key = result.ScanRecord.ManufacturerSpecificData.KeyAt(i);
                    var arr = result.ScanRecord.GetManufacturerSpecificData(key);
                    byte[] data = new byte[arr.Length + 2];
                    BitConverter.GetBytes((ushort)key).CopyTo(data,0);
                    arr.CopyTo(data, 2);
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.ManufacturerSpecificData, data));
                }

                foreach(var uuid in result.ScanRecord.ServiceUuids)
                {
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.UuidsIncomplete128Bit, uuid.Uuid.));
                }

                foreach(var key in result.ScanRecord.ServiceData.Keys)
                {
                    records.Add(new AdvertisementRecord(AdvertisementRecordType.ServiceData, result.ScanRecord.ServiceData));
                }*/

                var device = new Device(result.Device, null, null, result.Rssi, result.ScanRecord.GetBytes());

                //Device device;
                //if (result.ScanRecord.ManufacturerSpecificData.Size() > 0)
                //{
                //    int key = result.ScanRecord.ManufacturerSpecificData.KeyAt(0);
                //    byte[] mdata = result.ScanRecord.GetManufacturerSpecificData(key);
                //    byte[] mdataWithKey = new byte[mdata.Length + 2];
                //    BitConverter.GetBytes((ushort)key).CopyTo(mdataWithKey, 0);
                //    mdata.CopyTo(mdataWithKey, 2);
                //    device = new Device(result.Device, null, null, result.Rssi, mdataWithKey);
                //}
                //else
                //{
                //    device = new Device(result.Device, null, null, result.Rssi, new byte[0]);
                //}

                _adapter.DeviceAdvertised(this, new DeviceDiscoveredEventArgs { Device = device });

                if (!_adapter._discoveredDevices.Contains(device))
                {
                    _adapter._discoveredDevices.Add(device);
                    _adapter.DeviceDiscovered(this, new DeviceDiscoveredEventArgs { Device = device });
                }
            }
        public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
        {
            base.OnConnectionStateChange(gatt, status, newState);
            IDevice device = null;

            if (status != GattStatus.Success)
            {
                Mvx.TaggedError("OnConnectionStateChange", "GattCallback error: {0}", status);
                DeviceConnectionError(this, new DeviceConnectionEventArgs(){ Device = device });
                // We don't return. Allowing to fall-through to the SWITCH, which will assume a disconnect, close GATT and clean up.
                // The above error event handles the case where the error happened during a Connect call, which will close out any waiting asyncs.
            }
            else
            {
                Mvx.Trace("GattCallback state: {0}", newState.ToString());
            }
            
            switch (newState)
            {
                // disconnected
                case ProfileState.Disconnected:

                    if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Mvx.Trace("Disconnected by user");

                        //Found so we can remove it
                        DeviceOperationRegistry.Remove(gatt.Device.Address);
                        ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();

                        DeviceDisconnected(this, new DeviceConnectionEventArgs { Device = device });
                        break;
                    }

                    //connection must have been lost, bacause our device was not found in the registry but was still connected
                    if (ConnectedDeviceRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        Mvx.Trace("Disconnected by lost connection");

                        ConnectedDeviceRegistry.Remove(gatt.Device.Address);
                        gatt.Close();

                        DeviceConnectionLost(this, new DeviceConnectionEventArgs() { Device = device });
                        break;
                    }

                    gatt.Close(); // Close GATT regardless, else we can accumulate zombie gatts.
                    Mvx.Trace("Disconnect. Device not found in registry. Not raising disconnect/lost event.");

                    break;
                // connecting
                case ProfileState.Connecting:
                    Mvx.Trace("Connecting");
                    break;
                // connected
                case ProfileState.Connected:
                    Mvx.Trace("Connected");

                    //Try to find the device in the registry so that the same instance is updated
                    if (DeviceOperationRegistry.TryGetValue(gatt.Device.Address, out device))
                    {
                        ((Device)device).Update(gatt.Device, gatt, this);

                        //Found so we can remove it
                        DeviceOperationRegistry.Remove(gatt.Device.Address);
                    }
                    else
                    {
                        //only for on auto-reconnect (device is not in operation registry)
                        device = new Device(gatt.Device, gatt, this, 0);
                    }

                    ConnectedDeviceRegistry[gatt.Device.Address] = device;
                    DeviceConnected(this, new DeviceConnectionEventArgs() { Device = device });

                    break;
                // disconnecting
                case ProfileState.Disconnecting:
                    Mvx.Trace("Disconnecting");
                    break;
            }
        }
示例#4
0
        public void OnLeScan(BluetoothDevice bleDevice, int rssi, byte[] scanRecord)
        {
            Mvx.Trace("Adapter.LeScanCallback: " + bleDevice.Name);

            var device = new Device(bleDevice, null, null, rssi, scanRecord);

            DeviceAdvertised(this, new DeviceDiscoveredEventArgs { Device = device });

            if (!_discoveredDevices.Contains(device))
            {
                _discoveredDevices.Add(device);

                DeviceDiscovered(this, new DeviceDiscoveredEventArgs { Device = device });
            }
        }