Exemplo n.º 1
0
        internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
        {
            BluetoothDevice     resultDevice        = new BluetoothDevice();
            Collection <string> uuidList            = null;
            const int           DeviceNameLengthMax = 248;
            const int           UuidLengthMax       = 50;

            if (structDevice.ServiceCount > 0)
            {
                IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
                Marshal.Copy(structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
                uuidList = new Collection <string> ();
                Log.Info(Globals.LogTag, "UUID Count: " + structDevice.ServiceCount);
                foreach (IntPtr extension in extensionList)
                {
                    if (extension != IntPtr.Zero)
                    {
                        string uuid = Marshal.PtrToStringAnsi(extension, UuidLengthMax);
                        Log.Info(Globals.LogTag, "UUID: " + uuid);
                        uuidList.Add(uuid);
                    }
                }
            }

            resultDevice.RemoteDeviceAddress = structDevice.Address;
            if (structDevice.Name != IntPtr.Zero)
            {
                resultDevice.RemoteDeviceName = Marshal.PtrToStringAnsi(structDevice.Name, DeviceNameLengthMax);
                Log.Info(Globals.LogTag, "Device Name: " + resultDevice.RemoteDeviceName);
            }

            resultDevice.RemoteDeviceClass = new BluetoothClass();
            resultDevice.Class.MajorType   = structDevice.Class.MajorDeviceClassType;
            resultDevice.Class.MinorType   = structDevice.Class.MinorDeviceClassType;
            resultDevice.Class.Mask        = structDevice.Class.MajorServiceClassMask;

            resultDevice.RemoteDeviceRssi = structDevice.Rssi;
            resultDevice.RemoteAppearance = structDevice.AppearanceType;

            if (structDevice.ServiceCount > 0)
            {
                resultDevice.RemoteDeviceService = uuidList;
                resultDevice.RemoteDeviceCount   = structDevice.ServiceCount;
            }

            resultDevice.RemotePaired      = structDevice.IsPaired;
            resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;

            if (structDevice.ManufacturerData != IntPtr.Zero)
            {
                resultDevice.RemoteManufData = Marshal.PtrToStringAnsi(structDevice.ManufacturerData, structDevice.ManufacturerDataLength);
            }

            return(resultDevice);
        }
Exemplo n.º 2
0
        internal static BluetoothDevice ConvertStructToDiscoveredDevice(BluetoothDiscoveredDeviceStruct structDevice)
        {
            BluetoothDevice     resultDevice = new BluetoothDevice();
            Collection <string> uuidList     = null;

            if (structDevice.ServiceCount > 0)
            {
                IntPtr[] extensionList = new IntPtr[structDevice.ServiceCount];
                Marshal.Copy(structDevice.ServiceUuidList, extensionList, 0, structDevice.ServiceCount);
                uuidList = new Collection <string> ();
                foreach (IntPtr extension in extensionList)
                {
                    string uuid = Marshal.PtrToStringAnsi(extension);
                    uuidList.Add(uuid);
                }
            }

            resultDevice.RemoteDeviceAddress = structDevice.Address;
            resultDevice.RemoteDeviceName    = structDevice.Name;

            resultDevice.RemoteDeviceClass = new BluetoothClass();
            resultDevice.Class.MajorType   = structDevice.Class.MajorDeviceClassType;
            resultDevice.Class.MinorType   = structDevice.Class.MinorDeviceClassType;
            resultDevice.Class.Mask        = structDevice.Class.MajorServiceClassMask;

            resultDevice.RemoteDeviceRssi = structDevice.Rssi;
            resultDevice.RemoteAppearance = structDevice.AppearanceType;

            if (structDevice.ServiceCount > 0)
            {
                resultDevice.RemoteDeviceService = uuidList;
                resultDevice.RemoteDeviceCount   = structDevice.ServiceCount;
            }

            resultDevice.RemotePaired      = structDevice.IsPaired;
            resultDevice.RemoteManufData   = structDevice.ManufacturerData;
            resultDevice.RemoteManufLength = structDevice.ManufacturerDataLength;
            return(resultDevice);
        }
Exemplo n.º 3
0
        private void RegisterDiscoveryStateChangedEvent()
        {
            _discoveryStateChangedCallback = (int result, BluetoothDeviceDiscoveryState state, IntPtr deviceInfo, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "Discovery state changed callback is called");
                if (_discoveryStateChanged != null)
                {
                    BluetoothError res = (BluetoothError)result;
                    switch (state)
                    {
                    case BluetoothDeviceDiscoveryState.Started:
                        _discoveryStateChanged(null, new DiscoveryStateChangedEventArgs(res, state));
                        break;

                    case BluetoothDeviceDiscoveryState.Finished:
                    {
                        _discoveryStateChanged(null, new DiscoveryStateChangedEventArgs(res, state));
                        break;
                    }

                    case BluetoothDeviceDiscoveryState.Found:
                    {
                        BluetoothDiscoveredDeviceStruct info = (BluetoothDiscoveredDeviceStruct)Marshal.PtrToStructure(deviceInfo, typeof(BluetoothDiscoveredDeviceStruct));
                        _discoveryStateChanged(null, new DiscoveryStateChangedEventArgs(res, state, BluetoothUtils.ConvertStructToDiscoveredDevice(info)));
                        break;
                    }

                    default:
                        break;
                    }
                }
            };
            int ret = Interop.Bluetooth.SetDiscoveryStateChangedCallback(_discoveryStateChangedCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set discovery state changed callback, Error - " + (BluetoothError)ret);
            }
        }