C# : Bluetooth GATT communication. GattCharacteristic.ValueChanged doesnt gets hit after sometime - c#

I am working on wpf application that uses GATT for bluetooth communication. I am using GattCharacteristic.ValueChanged to handle any value from device. It works for sometime and the event handler doesnt gets called after 1 minute. This problem observed in windows 8.1, but working in windows 10.
I am made GattCharacteristic member variable, so the GC doesnt collect the variable. Still the problem exists.
Can anyone tell me what could be the potential problem here?
private GattCharacteristic m_DataSenderCharacteristics;
foreach (var service in serviceInfo)
{
var uuid = MjGattDeviceService.UuidFromServiceInformation(service);
//Battery Service
if (uuid.ToString() == BatteryUUID)
{
var bService = await GattDeviceService.FromIdAsync(service.Id);
m_Device.batteryService = bService;
}
if (uuid.ToString() == CustomServiceUUID)
{
m_Device.gattService = await GattDeviceService.FromIdAsync(service.Id);
Guid controlGUID = new Guid(ControlUUID);
var controlGattCharacteristics = m_Device.gattService.GetCharacteristics(controlGUID);
Byte[] bsdata = new Byte[] { 0x0b, 0x00, 0x02 };
IBuffer buffUTF8 = CryptographicBuffer.CreateFromByteArray(bsdata);
GattCommunicationStatus status = await controlGattCharacteristics[0].WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
if (status == GattCommunicationStatus.Unreachable)
{
m_Device.isConnected = false;
nextPage = "Unreachable";
}
else if (status == GattCommunicationStatus.Success)
{
var ctrlStatus = await controlGattCharacteristics[0].WriteValueAsync(buffUTF8);
Guid guuid1 = new Guid(DataSenderUUID);
m_DataSenderCharacteristics = m_Device.gattService.GetCharacteristics(guuid1)[0];
GattCommunicationStatus notifyStatus = await m_DataSenderCharacteristics.
WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
//This event handler will be called for some 1 minute. After it falls dead
m_DataSenderCharacteristics[0].ValueChanged += GestureValueChanged;
if (notifyStatus == GattCommunicationStatus.Success)
{
nextPage = "Connected";
}
}
}
}

Related

DSharpPlus - WaitForReaction on existing Messages

I am trying to create a discord bot using DSharpPlus library where if you react on a message with specific emoji, you will get a specific role. The concept is pretty straight forward but I fail to figure out one rather important concept. That is, how do I get the bot to listen for a reaction on an existing message all the time.
I tried to do it via commands and I got it to work, however the problem with this approach as I learned is that the bot only listens for reactions after I type a command and it only lasts a minute or so (based on configuration).
public class RoleCommands : BaseCommandModule
{
[Command("join")]
public async Task Join(CommandContext ctx)
{
var joinEmbed = new DiscordEmbedBuilder
{
Title = "Reaction with thumbs up!",
Color = DiscordColor.Green
};
var joinMessage = await ctx.Channel.SendMessageAsync(embed: joinEmbed).ConfigureAwait(false);
var thumbsUpEmoji = DiscordEmoji.FromName(ctx.Client, ":+1:");
var thumbsDownEmoji = DiscordEmoji.FromName(ctx.Client, ":-1:");
await joinMessage.CreateReactionAsync(thumbsUpEmoji).ConfigureAwait(false);
await joinMessage.CreateReactionAsync(thumbsDownEmoji).ConfigureAwait(false);
var interactivity = ctx.Client.GetInteractivity();
var reactionResult = await interactivity.WaitForReactionAsync(x =>
x.Message == joinMessage
&& x.User == ctx.User
&& x.Emoji == thumbsUpEmoji);
if (reactionResult.Result.Emoji == thumbsUpEmoji)
{
var role = ctx.Guild.GetRole(773965440913375282);
await ctx.Member.GrantRoleAsync(role).ConfigureAwait(false);
await joinMessage.DeleteAsync().ConfigureAwait(false);
}
}
}
How can I do this outside of a command where I can pass it a message Id and then it listens to that message for reactions all the time as oppose to a limited time?
The full answer to my question is to use DiscordClient.MessageReactionAdded += OnReactionAdded; and to implement the method as such:
private async Task OnReactionAdded(DiscordClient sender, MessageReactionRemoveEventArgs e)
{
var messageId = e.Message.Id;
var guild = e.Message.Channel.Guild;
var reactionName = e.Emoji.GetDiscordName();
var reactionDetail = ReactionDetails.FirstOrDefault(x =>
x.MessageId == messageId
&& x.GuildId == guild.Id
&& x.ReactionName == reactionName);
if (reactionDetail != null)
{
var member = e.User as DiscordMember;
if (member != null)
{
var role = guild.Roles.FirstOrDefault(x => x.Value.Id == reactionDetail.RoleId).Value;
await member.GrantRoleAsync(role).ConfigureAwait(false);
}
}
}
Store the message id somewhere then hook the MessageReactionAdded event on your DiscordClient and do your logic there.

Setting MediaStreamProperties on HoloLens does not work

I am working on an UWP app for the HoloLens to read single frames from the devices camera. I want to use the camera mode with the lowest resolution available.
I took a look at the following links and examples and tried to create a minimal working app:
https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/set-media-encoding-properties
https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/use-opencv-with-mediaframereader
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraResolution
https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraOpenCV
This is the code snippet from MainPage.xaml.cs:
public async Task<int> Start()
{
// Find the sources
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
var sourceGroups = allGroups.Select(g => new
{
Group = g,
SourceInfo = g.SourceInfos.FirstOrDefault(i => i.SourceKind == MediaFrameSourceKind.Color)
}).Where(g => g.SourceInfo != null).ToList();
if (sourceGroups.Count == 0)
{
// No camera sources found
return 0;
}
var selectedSource = sourceGroups.FirstOrDefault();
// Initialize MediaCapture
_mediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedSource.Group,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu
};
await _mediaCapture.InitializeAsync(settings);
// Query all properties of the device
IEnumerable<StreamResolution> allVideoProperties = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord).Select(x => new StreamResolution(x));
// Order them by resolution then frame rate
allVideoProperties = allVideoProperties.OrderBy(x => x.Height * x.Width).ThenBy(x => x.FrameRate);
await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, allVideoProperties.ElementAt(0).EncodingProperties);
// Create the frame reader
MediaFrameSource frameSource = _mediaCapture.FrameSources[selectedSource.SourceInfo.Id];
_reader = await _mediaCapture.CreateFrameReaderAsync(frameSource, MediaEncodingSubtypes.Bgra8);
_reader.FrameArrived += ColorFrameReader_FrameArrivedAsync;
await _reader.StartAsync();
return 1;
}
private async void ColorFrameReader_FrameArrivedAsync(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
var frame = sender.TryAcquireLatestFrame();
if (frame != null)
{
var inputBitmap = frame.VideoMediaFrame?.SoftwareBitmap;
}
}
On my local machine (MacBookPro with Bootcamp partition) this code works using the webcam. It detects three supported video modes. I can change the resolution of the bitmap image in FrameArrivedAsync by changing the index from 0 to 1 or 2 at:
_mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, allVideoProperties.ElementAt(0).EncodingProperties);
On HoloLens this code does not work. It detects the different modes like explained here (https://learn.microsoft.com/en-us/windows/mixed-reality/locatable-camera). But setting the MediaStreamProperties does not change anything regarding the received bitmap image. The bitmap is always 1280x720.
Just in case, we want to share how we setup the capture profile, you can refer to the following code with annotate to modify your project for testing. If in doubt, please feel free to add comments.
private async void SetupAndStartMediaCapture()
{
string deviceId = string.Empty;
_mediaCapture = new MediaCapture();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
foreach (var device in devices)
{
if(MediaCapture.IsVideoProfileSupported(device.Id))
{
deviceId = device.Id;
break; // The video device for which supported video profile support is queried.
}
}
MediaCaptureInitializationSettings mediaCapSettings = new MediaCaptureInitializationSettings
{
VideoDeviceId = deviceId
};
IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(deviceId);
var profileMatch = (
from profile in profiles
from desc in profile.SupportedRecordMediaDescription
where desc.Width == 896 && desc.Height == 504 && desc.FrameRate == 24 // HL1
select new { profile, desc }
).FirstOrDefault();// Select the Profile with the required resolution from all available profiles.
if (profileMatch != null)
{
mediaCapSettings.VideoProfile = profileMatch.profile;
mediaCapSettings.RecordMediaDescription = profileMatch.desc;
}
await _mediaCapture.InitializeAsync(mediaCapSettings); //Initializes the MediaCapture object.
}

WCF Websocket project fails upon Entity Framework data access attempt

I am new to WebSockets (this AM) and have set up a WCF WebSocket app that works when doing a trivial example I found online (http://www.codeproject.com/Articles/619343/Using-WebSocket-in-NET-Part).
I added Entity Framework and as soon as I add code to try to access data the process (just sending a message back and forth) no longer works.
Could there be some fundamental concept I could be missing?
Does anyone have any good ideas for troubleshooting?
namespace PBWebSocket
{
public class PBWebSocket : IBWebSocket
{
private SPEntities db = new SPEntities();
public async Task SendMessageToServer(Message msg)
{
var callback = OperationContext.Current.GetCallbackChannel<IPBCallback>();
if (msg.IsEmpty || ((IChannel)callback).State != CommunicationState.Opened)
{
return;
}
byte[] body = msg.GetBody<byte[]>();
string msgTextFromClient = Encoding.UTF8.GetString(body);
var reqId = Int32.Parse(msgTextFromClient);
// *** The below line breaks it ***
var req = db.Requests.Where(r => r.Id == 164).FirstOrDefault();
reqId = reqId + 2;
Message newMsg = ByteStreamMessage.CreateMessage(
new ArraySegment<byte>(Encoding.UTF8.GetBytes(reqId.ToString())));
newMsg.Properties["WebSocketMessageProperty"] =
new WebSocketMessageProperty
{ MessageType = WebSocketMessageType.Text };
await callback.SendMessageToClient(newMsg);
}
}
}

How to batch queue records and execute them in a different thread and wait till it;s over?

I am using push sharp version PushSharp 4.0.4.
I am using it in a windows application.
I have three main methods
1- BroadCastToAll
2- BrodcatsToIOS
3- BrodcatsToAndriod
I have a button calld send. On the click event of the button. I am calling the
BroadCastToAll function.
private void btnSend_Click(object sender, EventArgs e)
{
var url = "www.mohammad-jouhari.com"
var promotion = new Promotion ();
BroadCastToAll(promotion, url);
}
Here is the BrodcastToAll Function
public void BroadCastToAll(Promotion promotion, string url)
{
var deviceCatalogs = GetDeviceCatalog();
BroadCastToIOS(promotion, url, deviceCatalogs.Where(d => d.OS == "IOS").ToList());
BroadCastToAndriod(promotion, url, deviceCatalogs.Where(d => d.OS == "Android").ToList());
}
Here is the BrodcastToIOS Function
public void BroadCastToIOS(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
{
if (deviceCatalogs.Count == 0)
return;
lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
{
QueueAllAppleDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
}
}
Here is the BrodcastToAndriod Function
public void BroadCastToAndriod(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
{
if (deviceCatalogs.Count == 0)
return;
lock (_lock)// Added this lock because there is a potential chance that PushSharp callback execute during registering devices
{
QueueAllGcmDevicesForNotification(promotion, url, deviceCatalogs, logsMessage);
}
}
Here is the QueueAllAppleDevicesForNotification function
private void QueueAllAppleDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs)
{
var apnsServerEnviroment = UseProductionCertificate ? ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox;
var fileService = new FileService();
var filePath = Application.StartupPath+ "/Certifcates/" + (UseProductionCertificate ? "prod.p12" : "dev.p12");
var buffer = fileService.GetFileBytes(filePath);
var config = new ApnsConfiguration(apnsServerEnviroment, buffer, APPLE_CERTIFICATE_PWD);
apnsServiceBroker = new ApnsServiceBroker(config);
apnsServiceBroker.OnNotificationFailed += (notification, aggregateEx) => {
aggregateEx.Handle (ex => {
// Log the Resposne
});
};
apnsServiceBroker.OnNotificationSucceeded += (notification) => {
// Log The Response
};
apnsServiceBroker.Start();
foreach (var deviceToken in deviceCatalogs) {
var title = GetTitle(promotion, deviceToken);
//title += DateTime.UtcNow.TimeOfDay.ToString();
var NotificationPayLoadObject = new NotificationPayLoadObjectApple();
NotificationPayLoadObject.aps.alert = title;
NotificationPayLoadObject.aps.badge = 0;
NotificationPayLoadObject.aps.sound = "default";
NotificationPayLoadObject.url = url;
var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
apnsServiceBroker.QueueNotification(new ApnsNotification
{
Tag = this,
DeviceToken = deviceToken.UniqueID,
Payload = payLoad
});
}
var fbs = new FeedbackService(config);
fbs.FeedbackReceived += (string deviceToken, DateTime timestamp) =>
{
// This Token is no longer avaialble in APNS
new DeviceCatalogService().DeleteExpiredIosDevice(deviceToken);
};
fbs.Check();
apnsServiceBroker.Stop();
}
And here is the QueueAllGcmDevicesForNotification
private void QueueAllGcmDevicesForNotification(Promotion promotion, string url, List<DeviceCatalog> deviceCatalogs, )
{
var config = new GcmConfiguration(ANDROID_SENDER_ID, ANDROID_SENDER_AUTH_TOKEN, ANDROID_APPLICATION_ID_PACKAGE_NAME);
gcmServiceBroker = new GcmServiceBroker(config);
gcmServiceBroker.OnNotificationFailed += (notification, aggregateEx) => {
aggregateEx.Handle (ex => {
// Log Response
return true;
});
};
gcmServiceBroker.OnNotificationSucceeded += (notification) => {
// Log Response
};
var title = GetTitle(shopexPromotion);
gcmServiceBroker.Start ();
foreach (var regId in deviceCatalogs) {
var NotificationPayLoadObject = new NotificationPayLoadObjectAndriod(url, title, "7", promotion.ImageUrl);
var payLoad = JObject.Parse(JsonConvert.SerializeObject(NotificationPayLoadObject));
gcmServiceBroker.QueueNotification(new GcmNotification
{
RegistrationIds = new List<string> {
regId.UniqueID
},
Data = payLoad
});
}
gcmServiceBroker.Stop();
}
Now When I click the send button. The event will start executing.
The BrodcastToAll function will be called. I am calling BrodcastToIOS devices first and then BrodcatsToAndriod.
Is there any way in which I can call BrodcastToIOS and wait until all the devices have been Queued and notification has been pushed by the library and the call back events fired fully then start executing the BrodcastToAndriod Fucntion ?
What lines of code I need to add ?
also Is there any way to batch the number of devices to be Queued ?
For example.
Let us say I have 1000 Devices
500 IOS
500 Andriod
Can I queue 100, 100,100,100,100 for IOS and when it's done
I queue 100,100,100,100,100 for Andriod.
Any Help is appreciated.
Thanks.
The call to broker.Stop () by default will block until all the notifications from the queue have been processed.

StreamSocket crash on Windows 8.1 store app

I have a server-client connection which works on both sides with Windows.Networking.Sockets.StreamSocket. On Windows 10, as Universal App, the connection is successful and data flows in back and forth without problems. On Windows 8.1, as a Windows Store app, the reading part of the StreamSocket fails at first attempt to read incoming data. The app closes and VS 2015 do not report any Exception nor the Output Window contains any useful information, other than Program has exited with code 1. Also, putting a breakpoint and then stepping through code doesn't work. Locals are not displayed and VS shows a message dialog:
Unable to start debugging. The object invoked has disconnected from its clients.
Here is the reading code:
public IAsyncAction Read()
{
return Task.Run(() =>
{
try
{
const uint length = 65536;
string request = string.Empty;
var socket = _signalingSocketService.GetSocket();
var readBuf = new Windows.Storage.Streams.Buffer(length);
var readOp = socket.InputStream.ReadAsync(readBuf, length, InputStreamOptions.Partial);
readOp.Completed = (IAsyncOperationWithProgress<IBuffer, uint> asyncAction, AsyncStatus asyncStatus) =>
{
if(asyncStatus == AsyncStatus.Completed)
{
var localBuffer = asyncAction.GetResults();
var dataReader = DataReader.FromBuffer(localBuffer);
request = dataReader.ReadString(dataReader.UnconsumedBufferLength);
_signalingSocketService.HandoffSocket(socket);
List<string> requests;
var fileTask = BufferFileExists().AsTask();
fileTask.Wait();
if (fileTask.Result)
{
var bufferFileTask = GetBufferFile().AsTask();
bufferFileTask.Wait();
var bufferFile = bufferFileTask.Result;
var task = FileIO.AppendTextAsync(bufferFile, request).AsTask();
task.Wait();
var readLinesTask = FileIO.ReadLinesAsync(bufferFile).AsTask();
readLinesTask.Wait();
requests = (readLinesTask.Result).ToList();
var deleteTask = bufferFile.DeleteAsync().AsTask();
deleteTask.Wait();
}
else
{
requests =
request.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
} // if (asyncStatus == AsyncStatus.Completed)
}; // readOp.Completed
}
catch (Exception ex)
{
}
}
}
What can cause such an odd behavior?

Categories