internal Track GetTrackInfo() { Track trackdata = new Track(); TrackInfoStruct trackinfo; IntPtr infoptr; int ret = Interop.Bluetooth.GetTrackInfo(out infoptr); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get track data" + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } trackinfo = (TrackInfoStruct)Marshal.PtrToStructure(infoptr, typeof(TrackInfoStruct)); trackdata.Album = trackinfo.Album; trackdata.Artist = trackinfo.Artist; trackdata.Genre = trackinfo.Genre; trackdata.Title = trackinfo.Title; trackdata.TotalTracks = trackinfo.total_tracks; trackdata.TrackNum = trackinfo.number; trackdata.Duration = trackinfo.duration; ret = Interop.Bluetooth.FreeTrackInfo(infoptr); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to free track data" + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } return(trackdata); }
private void targetInitialize() { if (Globals.IsInitialize) { _targetConnectionChangedCallback = (bool connected, string deviceAddress, IntPtr userData) => { if (_targetConnectionChanged != null) { _targetConnectionChanged(null, new TargetConnectionStateChangedEventArgs(connected, deviceAddress)); } }; int ret = Interop.Bluetooth.InitializeAvrcp(_targetConnectionChangedCallback, IntPtr.Zero); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to initialize bluetooth avrcp, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } else { Globals.IsAudioInitialize = true; } } else { Log.Error(Globals.LogTag, "Failed to initialize Avrcp, BT not initialized"); BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotInitialized); } }
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); }
internal BluetoothOobData GetLocalOobData() { BluetoothOobData oobData = new BluetoothOobData(); IntPtr hash; IntPtr randomizer; int hashLength; int randomizerLength; int ret = Interop.Bluetooth.GetOobData(out hash, out randomizer, out hashLength, out randomizerLength); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get the local oob data, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } if (hashLength > 0) { byte[] hashArr = new byte[hashLength]; Marshal.Copy(hash, hashArr, 0, hashLength); oobData.HashValue = hashArr; Interop.Libc.Free(hash); } if (randomizerLength > 0) { byte[] randomizerArr = new byte[randomizerLength]; Marshal.Copy(randomizer, randomizerArr, 0, randomizerLength); oobData.RandomizerValue = randomizerArr; Interop.Libc.Free(randomizer); } return(oobData); }
/// <summary> /// Register the Opp Server with the Opp service. /// </summary> /// <remarks> /// The device must be bonded with remote device by CreateBond(). /// If connection request is received from OPP Client, ConnectionRequested event will be invoked. /// </remarks> /// <returns>The BluetoothOppServer instance.</returns> /// <param name="FilePath"> Path to store the files.</param> /// <feature>http://tizen.org/feature/network.bluetooth.opp</feature> /// <exception cref="NotSupportedException">Thrown when the required feature is not Supported.</exception> /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not Supported.</exception> /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not Enabled or Other Bluetooth Errors.</exception> /// <since_tizen> 4 </since_tizen> public static BluetoothOppServer StartServer(string FilePath) { if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) { if (_instance == null) { BluetoothOppServer server = new BluetoothOppServer(); if (server != null) { _instance = server; } } int ret = _impl.StartServer(FilePath); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to Opp Start Server - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } return(_instance); } else { BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); } return(null); }
internal Task <bool> WriteValueAsyncTask(BluetoothGattAttributeHandle handle) { TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(); Interop.Bluetooth.BtGattClientRequestCompletedCallback cb = (result, requestHandle, userData) => { if (result == (int)BluetoothError.None) { tcs.SetResult(true); } else { tcs.SetResult(false); } }; int err = Interop.Bluetooth.BtGattClientWriteValue(handle, cb, IntPtr.Zero); if (err.IsFailed()) { GattUtil.Error(err, "Failed to write value to remote device"); tcs.SetResult(false); BluetoothErrorFactory.ThrowBluetoothException(err); } return(tcs.Task); }
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); }
/// <summary> /// Gets the connected profiles. /// </summary> /// <remarks> /// The Bluetooth must be enabled. /// </remarks> /// <returns>The connected Bluetooth profiles.</returns> /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled /// or when there is no BT connection.</exception> /// <since_tizen> 3 </since_tizen> public IEnumerable <BluetoothProfileType> GetConnectedProfiles() { if (BluetoothAdapter.IsBluetoothEnabled) { List <BluetoothProfileType> profileList = new List <BluetoothProfileType>(); _connectedProfileCallback = (int profile, IntPtr userData) => { if (!profile.Equals(null)) { profileList.Add((BluetoothProfileType)profile); } return(true); }; int ret = Interop.Bluetooth.GetConnectedProfiles(RemoteDeviceAddress, _connectedProfileCallback, IntPtr.Zero); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to get connected profiles, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } return(profileList); } else { return(null); } }
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); }
/// <summary> /// Adds a service data to the advertise or the scan response data. /// The maximum advertised or responded data size is 31 bytes /// including data type and system wide data. /// </summary> /// <remarks> /// The Bluetooth must be enabled. /// </remarks> /// <param name="packetType">The packet type.</param> /// <param name="data"> The service data to be added to advertising.</param> /// <exception cref="NotSupportedException">Thrown when the Bluetooth LE is not supported.</exception> /// <exception cref="InvalidOperationException">Thrown when the Bluetooth LE is not enabled /// or when the add advertising data procedure fails.</exception> /// <since_tizen> 3 </since_tizen> public void AddAdvertisingServiceData(BluetoothLePacketType packetType, BluetoothServiceData data) { if (BluetoothAdapter.IsBluetoothEnabled && Globals.IsInitialize) { IntPtr serviceDataPtr; serviceDataPtr = Marshal.AllocHGlobal(data.DataLength); Marshal.Copy(data.Data, 0, serviceDataPtr, data.DataLength); for (int i = 0; i < data.DataLength; i++) { Log.Error(Globals.LogTag, " service data is " + data.Data [i]); } int ret = Interop.Bluetooth.AddAdvertisingServiceData(GetHandle(), packetType, data.Uuid, serviceDataPtr, data.DataLength); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to add service data to advertising data- " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } } else { BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.NotEnabled); } }
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); }
internal static void ThrowForError(int err, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0) { if (err.IsFailed()) { Log.Error(Globals.LogTag, string.Format("{0}, err: {1}", message, (BluetoothError)err), file, func, line); BluetoothErrorFactory.ThrowBluetoothException(err); } }
/// <summary> /// Registers a specified service to this server. /// </summary> /// <param name="service">The service, which needs to be registered with this server.</param> /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception> /// <exception cref="InvalidOperationException">Thrown when the register service fails.</exception> /// <since_tizen> 3 </since_tizen> public void RegisterGattService(BluetoothGattService service) { if (service.IsRegistered()) { BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter); } _impl.RegisterGattService(this, service); }
/// <summary> /// Unregisters a specified service from this server. /// </summary> /// <param name="service">The service, which needs to be unregistered from this server.</param> /// <remarks> /// Once unregistered, the service object will become invalid and should not be used to access sevices or any children attribute's methods/members. /// </remarks> /// <exception cref="NotSupportedException">Thrown when the BT/BTLE is not supported.</exception> /// <exception cref="InvalidOperationException">Thrown when the unregister service fails.</exception> /// <since_tizen> 3 </since_tizen> public void UnregisterGattService(BluetoothGattService service) { if (service.GetGattServer() != this) { BluetoothErrorFactory.ThrowBluetoothException((int)BluetoothError.InvalidParameter); } _impl.UnregisterGattService(service); }
internal void NotifyRepeatMode(RepeatMode repeat) { int ret = Interop.Bluetooth.NotifyRepeatMode((int)repeat); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to notify repeat mode to remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void Disconnect(string address) { int ret = Interop.Bluetooth.AvrcpControlDisconnect(address); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to disconnect " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
/// <summary> /// Cancels the bonding process. /// </summary> /// <remarks> /// Bonding must be in progress by CreateBond(). /// </remarks> /// <exception cref="InvalidOperationException">Thrown when the BT/BTLE is not enabled /// or when the cancel bonding procedure to remote device fails.</exception> /// <since_tizen> 3 </since_tizen> public void CancelBonding() { int ret = Interop.Bluetooth.CancelBonding(); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to cancel bonding process, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SetScanMode(ScanMode mode) { int ret = Interop.Bluetooth.SetScanMode(mode); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to set scan mode to " + mode + " - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SetEqualizerState(EqualizerState state) { int ret = Interop.Bluetooth.SetEqualizerState(state); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to set equalizer state to " + state + " - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SendDelayReport(uint delay) { int ret = Interop.Bluetooth.SendDelayReport(delay); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to send delay report" + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void DecreaseVolume() { int ret = Interop.Bluetooth.DecreaseVolume(); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to decrease volume" + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SetAbsoluteVolume(uint volume) { int ret = Interop.Bluetooth.SetAbsoluteVolume(volume); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to set absolute volume to level " + volume + " - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
private void Deinitialize() { int ret = Interop.Bluetooth.DeactivateHidDevice(); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to deactivate to the remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SetAttMtu(int mtu) { int err = Interop.Bluetooth.BtGattClientSetAttMtu(_handle, mtu); if (err.IsFailed()) { GattUtil.Error(err, "Failed to set MTU value"); BluetoothErrorFactory.ThrowBluetoothException(err); } }
internal void NotifyPlayerState(PlayerState state) { int ret = Interop.Bluetooth.NotifyPlayerState((int)state); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to notify player state to remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SendHidDeviceKeyEvent(string deviceAddress, BluetoothHidKeyData keyData) { int ret = Interop.Bluetooth.SendHidDeviceKeyEvent(deviceAddress, keyData); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to send key event to the remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void NotifyScanMode(ScanMode scan) { int ret = Interop.Bluetooth.NotifyScanMode((int)scan); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to notify scan mode to remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SendPlayerCommand(PlayerCommand command) { int ret = Interop.Bluetooth.SendPlayerCommand(command); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to send player command " + command + " - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void NotifyCurrentPosition(uint position) { int ret = Interop.Bluetooth.NotifyCurrentPosition(position); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to notify position to remote device, Error - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }
internal void SendPlayerCommandTo(PlayerCommand command, string remoteAddress) { int ret = Interop.Bluetooth.SendPlayerCommandTo(command, remoteAddress); if (ret != (int)BluetoothError.None) { Log.Error(Globals.LogTag, "Failed to send player command " + command + " to remote address " + remoteAddress + " - " + (BluetoothError)ret); BluetoothErrorFactory.ThrowBluetoothException(ret); } }