Пример #1
0
        internal string GetLeScanResultDeviceName(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            string deviceName;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetLeScanResultDeviceName(ref scanDataStruct, packetType, out deviceName);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No DeviceName in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Device name- " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "DeviceName: " + deviceName);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(deviceName);
        }
Пример #2
0
        internal int StartScan()
        {
            _adapterLeScanResultChangedCallback = (int result, ref BluetoothLeScanDataStruct scanData, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "Inside Le scan callback ");
                BluetoothLeScanData scanDataInfo = BluetoothUtils.ConvertStructToLeScanData(scanData);

                BluetoothLeDevice device = new BluetoothLeDevice(scanDataInfo);
                BluetoothError    res    = (BluetoothError)result;

                AdapterLeScanResultChangedEventArgs e = new AdapterLeScanResultChangedEventArgs(res,
                                                                                                device);
                _adapterLeScanResultChanged?.Invoke(null, e);
            };

            IntPtr data = IntPtr.Zero;
            int    ret  = Interop.Bluetooth.StartScan(_adapterLeScanResultChangedCallback, data);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to start BLE scan - " + (BluetoothError)ret);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }
            _scanStarted = true;
            return(ret);
        }
Пример #3
0
        internal ManufacturerData GetScanResultManufacturerData(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int    dataId     = 0;
            int    dataLength = 0;
            IntPtr manufData;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);
            ManufacturerData          data           = new ManufacturerData();

            int ret = Interop.Bluetooth.GetScanResultManufacturerData(ref scanDataStruct, packetType, out dataId,
                                                                      out manufData, out dataLength);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Manufacturer data - " + (BluetoothError)ret);
                return(null);
            }

            data.Id         = dataId;
            data.DataLength = dataLength;
            if (data.DataLength > 0)
            {
                data.Data = new byte[data.DataLength];
                Marshal.Copy(manufData, data.Data, 0, data.DataLength);
            }

            return(data);
        }
Пример #4
0
        internal int GetScanResultTxPowerLevel(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            int powerLevel = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultTxPowerLevel(ref scanDataStruct, packetType, out powerLevel);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No TxPowerLevel data in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(-1);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get tx power level- " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "TxPowerLevel: " + powerLevel);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(powerLevel);
        }
Пример #5
0
        internal int GetScanResultAppearance(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            int appearance = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultAppearance(ref scanDataStruct, packetType, out appearance);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No Appearance in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(-1);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Appearance value- " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(appearance);
        }
Пример #6
0
        internal IList <string> GetScanResultSvcSolicitationUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            IntPtr uuidListArray;
            int    count;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScaResultSvcSolicitationUuids(ref scanDataStruct, packetType, out uuidListArray, out count);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get service solicitation uuids " + (BluetoothError)ret);
                return(null);
            }

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            IList <string> list = new List <string>();

            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            return(list);
        }
Пример #7
0
        internal IList <string> GetLeScanResultServiceUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            IntPtr uuidListArray = IntPtr.Zero;
            int    count         = -1;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultServiceUuid(ref scanDataStruct, packetType,
                                                                 ref uuidListArray, ref count);

            if (ret != (int)BluetoothError.None)
            {
                Log.Info(Globals.LogTag, "Failed to service uuids list- " + (BluetoothError)ret);
                return(null);
            }

            Log.Info(Globals.LogTag, "count of uuids :  " + count);

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            IList <string> list = new List <string>();

            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            return(list);
        }
Пример #8
0
        internal int GetScanResultAppearance(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int appearance;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultAppearance(ref scanDataStruct, packetType, out appearance);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Appearance value- " + (BluetoothError)ret);
            }
            return(appearance);
        }
Пример #9
0
        internal int GetScanResultTxPowerLevel(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            int powerLevel = -1;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultTxPowerLevel(ref scanDataStruct, packetType, out powerLevel);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get tx power level- " + (BluetoothError)ret);
            }
            Log.Info(Globals.LogTag, "TxPowerLevel is --- " + powerLevel);
            return(powerLevel);
        }
Пример #10
0
        internal BluetoothDevice GetBondedDevice(string address)
        {
            IntPtr deviceInfo;
            int    ret = Interop.Bluetooth.GetBondedDeviceByAddress(address, out deviceInfo);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get bonded device by address, Error - " + (BluetoothError)ret);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }
            BluetoothDeviceStruct device = (BluetoothDeviceStruct)Marshal.PtrToStructure(deviceInfo, typeof(BluetoothDeviceStruct));

            return(BluetoothUtils.ConvertStructToDeviceClass(device));
        }
Пример #11
0
        internal string GetLeScanResultDeviceName(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            string deviceName;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetLeScanResultDeviceName(ref scanDataStruct, packetType, out deviceName);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Device name- " + (BluetoothError)ret);
                return(null);
            }
            Log.Info(Globals.LogTag, "Device name " + deviceName);
            return(deviceName);
        }
Пример #12
0
        internal ManufacturerData GetScanResultManufacturerData(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            int    dataId     = 0;
            int    dataLength = 0;
            IntPtr manufData;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultManufacturerData(ref scanDataStruct, packetType, out dataId, out manufData, out dataLength);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No ManufacturerData in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get Manufacturer data - " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "Count of ManufacturerData: " + dataLength);

            ManufacturerData data = new ManufacturerData();

            data.Id         = dataId;
            data.DataLength = dataLength;
            if (data.DataLength > 0)
            {
                data.Data = new byte[data.DataLength];
                Marshal.Copy(manufData, data.Data, 0, data.DataLength);
            }

            Interop.Libc.Free(manufData);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(data);
        }
Пример #13
0
        private void RegisterBondCreatedEvent()
        {
            _bondCreatedCallback = (int result, ref BluetoothDeviceStruct device, IntPtr userData) =>
            {
                if (_bondCreated != null)
                {
                    BluetoothError res = (BluetoothError)result;
                    _bondCreated(null, new BondCreatedEventArgs(res, BluetoothUtils.ConvertStructToDeviceClass(device)));
                }
            };
            int ret = Interop.Bluetooth.SetBondCreatedCallback(_bondCreatedCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set bond created callback, Error - " + (BluetoothError)ret);
            }
        }
Пример #14
0
        internal IList <BluetoothLeServiceData> GetScanResultServiceDataList(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            IntPtr serviceListArray;

            _serviceListCount = 0;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultServiceDataList(ref scanDataStruct, packetType, out serviceListArray, out _serviceListCount);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No ServiceDataList in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Info(Globals.LogTag, "Failed to get Service Data List, Error - " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "Count of ServiceDataList: " + _serviceListCount);

            IList <BluetoothLeServiceData> list = new List <BluetoothLeServiceData>();
            int sizePointerToABC = Marshal.SizeOf(new BluetoothLeServiceDataStruct());

            for (int i = 0; i < _serviceListCount; i++)
            {
                var svc = (BluetoothLeServiceDataStruct)Marshal.PtrToStructure(new IntPtr(serviceListArray.ToInt32() + (i * sizePointerToABC)), typeof(BluetoothLeServiceDataStruct));
                list.Add(BluetoothUtils.ConvertStructToLeServiceData(svc));
            }

            Interop.Bluetooth.FreeServiceDataList(serviceListArray, _serviceListCount);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(list);
        }
Пример #15
0
        internal IList <string> GetScanResultSvcSolicitationUuids(BluetoothLeScanData scanData, BluetoothLePacketType packetType)
        {
            if (!BluetoothAdapter.IsBluetoothEnabled || !Globals.IsInitialize)
            {
                BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled);
            }

            IntPtr uuidListArray;
            int    count;
            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            int ret = Interop.Bluetooth.GetScanResultSvcSolicitationUuids(ref scanDataStruct, packetType, out uuidListArray, out count);

            if (ret == (int)BluetoothError.NoData)
            {
                Log.Info(Globals.LogTag, "No ServiceSolicitationUuids in " + packetType);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }
            else if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get service solicitation uuids " + (BluetoothError)ret);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }

            Log.Info(Globals.LogTag, "Count of ServiceSolicitationUuids: " + count);

            IList <string> list = new List <string>();

            IntPtr[] uuidList = new IntPtr[count];
            Marshal.Copy(uuidListArray, uuidList, 0, count);
            foreach (IntPtr uuids in uuidList)
            {
                list.Add(Marshal.PtrToStringAnsi(uuids));
                Interop.Libc.Free(uuids);
            }

            Interop.Libc.Free(uuidListArray);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(list);
        }
Пример #16
0
        private void RegisterServiceSearchedEvent()
        {
            _serviceSearchedCallback = (int result, ref BluetoothDeviceSdpStruct sdp, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "Servicesearched cb is called");
                if (_serviceSearched != null)
                {
                    BluetoothError res = (BluetoothError)result;
                    _serviceSearched(null, new ServiceSearchedEventArgs(res, BluetoothUtils.ConvertStructToSdpData(sdp)));
                }
            };
            int ret = Interop.Bluetooth.SetServiceSearchedCallback(_serviceSearchedCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set service searched callback, Error - " + (BluetoothError)ret);
            }
        }
Пример #17
0
        internal IList <BluetoothLeServiceData> GetScanResultServiceDataList(BluetoothLeScanData scanData,
                                                                             BluetoothLePacketType packetType, out int serviceCount)
        {
            int    ret = 0;
            IntPtr serviceListArray;

            _serviceListCount = 0;

            BluetoothLeScanDataStruct scanDataStruct = BluetoothUtils.ConvertLeScanDataToStruct(scanData);

            ret = Interop.Bluetooth.GetScanResultServiceDataList(ref scanDataStruct, packetType, out serviceListArray, out _serviceListCount);
            Log.Info(Globals.LogTag, "ServiceListCount :  " + _serviceListCount + " PacketType : " + packetType + " Error: " + (BluetoothError)ret);
            if (ret != (int)BluetoothError.None)
            {
                Log.Info(Globals.LogTag, "Failed to get Service Data List, Error - " + (BluetoothError)ret);
                serviceCount = 0;
                Marshal.FreeHGlobal(serviceListArray);
                Marshal.FreeHGlobal(scanDataStruct.AdvData);
                Marshal.FreeHGlobal(scanDataStruct.ScanData);
                return(null);
            }

            BluetoothLeServiceDataStruct[] svcList = new BluetoothLeServiceDataStruct[_serviceListCount];
            int sizePointerToABC = Marshal.SizeOf(new BluetoothLeServiceDataStruct());

            for (int i = 0; i < _serviceListCount; i++)
            {
                svcList[i] = (BluetoothLeServiceDataStruct)Marshal.PtrToStructure(new IntPtr(serviceListArray.ToInt32() + (i * sizePointerToABC)), typeof(BluetoothLeServiceDataStruct));
                Log.Info(Globals.LogTag, " Uuid : " + svcList[i].ServiceUuid + "length :  " + svcList[i].ServiceDataLength);

                _list.Add(BluetoothUtils.ConvertStructToLeServiceData(svcList[i]));
            }

            serviceCount = _serviceListCount;

            Interop.Libc.Free(serviceListArray);
            Marshal.FreeHGlobal(scanDataStruct.AdvData);
            Marshal.FreeHGlobal(scanDataStruct.ScanData);
            return(_list);
        }
Пример #18
0
        internal IEnumerable <BluetoothDevice> GetBondedDevices()
        {
            List <BluetoothDevice> deviceList = new List <BluetoothDevice>();

            _bondedDeviceCallback = (ref BluetoothDeviceStruct deviceInfo, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "Bonded devices cb is called");
                if (!deviceInfo.Equals(null))
                {
                    deviceList.Add(BluetoothUtils.ConvertStructToDeviceClass(deviceInfo));
                }
                return(true);
            };
            int ret = Interop.Bluetooth.GetBondedDevices(_bondedDeviceCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to get bonded devices, Error - " + (BluetoothError)ret);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }
            return(deviceList);
        }
Пример #19
0
        private void RegisterDataReceivedEvent()
        {
            _dataReceivedCallback = (ref SocketDataStruct socketData, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "DataReceivedCallback is called");
                if (_dataReceived != null)
                {
                    GCHandle handle2 = (GCHandle)userData;
                    _dataReceived(handle2.Target as IBluetoothServerSocket, new SocketDataReceivedEventArgs(BluetoothUtils.ConvertStructToSocketData(socketData)));
                }
            };
            GCHandle handle1 = GCHandle.Alloc(this);
            IntPtr   uData   = (IntPtr)handle1;
            int      ret     = Interop.Bluetooth.SetDataReceivedCallback(_dataReceivedCallback, uData);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set data received callback, Error - " + (BluetoothError)ret);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }
        }
Пример #20
0
        private static void RegisterAcceptStateChangedEvent()
        {
            _connectionStateChangedCallback = (int result, BluetoothSocketState connectionState, ref SocketConnectionStruct socketConnection, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "AcceptStateChanged cb is called");
                BluetoothSocket socket = new BluetoothSocket();
                socket.connectedSocket = socketConnection.SocketFd;
                socket.remoteAddress   = socketConnection.Address;
                socket.serviceUuid     = socketConnection.ServiceUuid;
                _acceptStateChanged?.Invoke(null, new AcceptStateChangedEventArgs((BluetoothError)result, connectionState, BluetoothUtils.ConvertStructToSocketConnection(socketConnection), socket));
            };

            int ret = Interop.Bluetooth.SetConnectionStateChangedCallback(_connectionStateChangedCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set accept state changed callback, Error - " + (BluetoothError)ret);
                BluetoothErrorFactory.ThrowBluetoothException(ret);
            }
        }
Пример #21
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);
            }
        }
Пример #22
0
        private void RegisterConnectionChangedEvent()
        {
            _connectionChangedCallback = (bool connected, ref BluetoothDeviceConnectionStruct device, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "Connection state changed cb is called");
                if (_connectionChanged != null)
                {
                    _connectionChanged(null, new DeviceConnectionStateChangedEventArgs(connected, BluetoothUtils.ConvertStructToConnectionData(device)));
                }
            };

            int ret = Interop.Bluetooth.SetConnectionStateChangedCallback(_connectionChangedCallback, IntPtr.Zero);

            if (ret != (int)BluetoothError.None)
            {
                Log.Error(Globals.LogTag, "Failed to set connection state changed callback, Error - " + (BluetoothError)ret);
            }
        }
Пример #23
0
        private void RegisterConnectionStateChangedEvent()
        {
            _connectionStateChangedCallback = (int result, BluetoothSocketState connectionState, ref SocketConnectionStruct socketConnection, IntPtr userData) =>
            {
                Log.Info(Globals.LogTag, "ConnectionStateChangedCallback is called");
                if (_connectionStateChanged != null)
                {
                    connectedSocket = socketConnection.SocketFd;
                    GCHandle handle2 = (GCHandle)userData;
                    _connectionStateChanged(handle2.Target as IBluetoothServerSocket, new SocketConnectionStateChangedEventArgs((BluetoothError)result, connectionState, BluetoothUtils.ConvertStructToSocketConnection(socketConnection)));
                }
            };
            GCHandle handle1 = GCHandle.Alloc(this);
            IntPtr   data    = (IntPtr)handle1;
            int      ret     = Interop.Bluetooth.SetConnectionStateChangedCallback(_connectionStateChangedCallback, data);

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