WinCE: 10053 error during connect - bluetooth

I wrote a Bluetooth client program for a wince 4.2 device. The device discovery works fine. However, when I attempt to connect to a PC, the connect function immediately returns with error code 10053. The connection request was being processed by the PC with a prompt to enter the authentication code but the wince device doesn’t seem to wait.
What could be causing this issue? I am using the following steps (removed error handling for simplicity):
WSAStartup(..)…////was successful.
SOCKET m_Socket =Socket (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); //was successful.
SOCKADDR_BTH sClinet;
memset (&sClinet, 0, sizeof(sClinet));
sClinet.addressFamily = AF_BTH;
sClinet.btAddr = btd.btaddr; ; //BT_ADDR of the PC obtained via Device Discovery..
sClinet.port = BT_PORT_ANY; //I did try 0, did not help!
sClinet.serviceClassId = RFCOMM_PROTOCOL_UUID;
int nConErr = connect (m_Socket, (SOCKADDR *)&sClinet, sizeof(sClinet));
nConErr returns 10053 immediately. It doesn’t even wait, even though the PC recognized the connection and prompted a message to enter the PIN for authentication.

From what you are describing it looks like it is a pairing/authentication problem. You should consider in using also setsockopt function: http://msdn.microsoft.com/en-us/library/ms863347.aspx
You should try to use SO_BTH_AUTHENTICATE option which according to MSDN:
On connected socket, triggers authentication. On not connected socket, forces authentication on connection. For incoming connection this means that connection is rejected if authentication cannot be performed.
So before calling connect function you should set options to your socket.

Related

Azure Cosmos DB: 503 Service Unavailable

While connecting from my code outside of the company network, the Cosmos DB connection works just fine. But from the company network, it throws a 503 ServiceUnavailable status code. What are the possible issues?
Based on the comments:
System.Exception: 'Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: ServiceUnavailable (503); Substatus: 0;
ActivityId: ;
Reason: (The request failed because the client was unable to establish connections to 4 endpoints across 1 regions. Please check for client resource starvation issues and verify connectivity between client and server.
Normally, if you take that exception and store or view the ToString(), it will show you more information. But from the message itself, it means the client tried to connect to all known endpoints available and failed.
This normally means there is either something on the network blocking your request or the machine executing this code is completely overloaded (CPU at 100% or port exhaustion) and cannot process any request.
If this is consistently failing for all operations, check that your network has the correct port range open:
By default, the SDK works in Direct mode, so check that ports in the 10000 through 20000 range are open and available. If you have private endpoint enabled, the range is 0 to 65535.
As #GauravMantri mentioned, you can change to Gateway mode also if the network is restricted:
string connectionString = "<your-account-connection-string>";
CosmosClient client = new CosmosClient(connectionString,
new CosmosClientOptions
{
ConnectionMode = ConnectionMode.Gateway
});

How to create Bluetooth L2CAP connection between two devices?

Android 10 released support for BLE CoC connection so I wanted to try this out by making two simple android 10 apps, which would connect to each other with l2Cap and exchange "Hello World".
I wrote two apps, Server app and Client app, both having all permissions they need in their manifest files, and I run that apps on two Android 10 phones, and the connection was not established.
Here is the relevant part of my server app code:
try {
mServerSocket = bluetoothAdapter.listenUsingInsecureL2capChannel();
int psm = mServerSocket.getPsm();
} catch (IOException e) {
e.printStackTrace();
}
BluetoothSocket socket = mmServerSocket.accept();
Variable int psm is the PSM value I am using in client app.
Here is the relevant part of my client app code:
for (BluetoothDevice bluetoothDevice : deviceSet) {
if (!bluetoothDevice.getName().equals(PAIRED_DEVICE_NAME)) continue;
BluetoothDevice bd = adapter.getRemoteDevice(bluetoothDevice.getAddress());
bluetoothSocket = bd.createInsecureL2capChannel(psm_value);
break;
}
bluetoothSocket.connect();
where the string PAIRED_DEVICE_NAME is name of expected device, which is successfully found because devices are Bluetooth paired.
Int psm_value is the PSM value from server app. I suspect this might be a problem because I hard-coded this value from Server every time I tried to test this (every time was different value because this value is dynamically assigned and it lasts until you close server socket).
So my question is how to get remote PSM value? And how to connect these devices, because if I am using RFCOMM connection, this code works perfectly.
With this code I am getting error in bluetoothSocket.connect() line from client app saying:
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
Thanks!

First call to Microsoft.Azure.ServiceBus.Core.MessageSender.SendAsync times out, subsequent calls don't

I have some code written to communicate with an azure service bus. It sends messages to a queue. It's in a project targeting .net standard 2.0
When I run it from a .net core terminal app it runs fine. But, when the same code is called from a .net framework 4.7.2 project then the first attempt to send a message results in the following exception after 30 to 90 seconds:
"The remote party closed the WebSocket connection without completing the close handshake."
But any further messages will be sent without problem.
// This is using Microsoft.Azure.ServiceBus, if that makes any difference...
MessageSender MessageSender = new MessageSender(ConnectionString, SendQueueName;
try
{
await MessageSender.SendAsync(new Message(Encoding.UTF8.GetBytes("Test that won't work")));
}
catch(Exception e)
{
// Error will be caught here:
// "The remote party closed the WebSocket connection without completing the close handshake."
}
await MessageSender.SendAsync(new Message(Encoding.UTF8.GetBytes("Test that will work")));
Does anybody know why the first call fails? And how to make it not fail? Or fail quicker? I've tried changing the OperationTimeout and RetryPolicy but they don'e seem to have any effect.
These first connections are via port 5671/56712, which Trend antivirus intercepts. Once these have timed out then the framework falls back to using 443, which works fine.
We tried turning Trend off and running testing the connection and its pretty much instantaneous.

Trouble connecting NodeMCU to Microsoft Azure IoT Hub

I am trying to connect my ESP8266, running the latest NodeMCU build, to a Microsoft Azure IoT Hub via MQTT Protocol.
It appears that this is possible, as it is shown here...
http://thinglabs.io/workshop/esp8266/sending-d2c-messages/
I am using the correct syntax as far as I can see from the MS Azure help...
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support
Unlike the example in ThingLabs, which creates a SAS token using NodeMCU, I have followed the MS document and generated a SAS token using the Device Explorer, for testing purposes.
My LUA code is as follows...
-- Create variables
DEVICE = "testdevice"
IOTHUB = "mynewiothub.azure-devices.net"
PORT = 8883
USER = IOTHUB.."/"..DEVICE.."/api-version=2016-11-14"
PASS = "SharedAccessSignature sr=mynewiothub.azure-devices.net%2Fdevices%2Ftestdevice&sig=Roa5P8BPiGj...v2Vu%2Bm1j9sas%3D&se=1485704099"
-- Create an MQTT Client
azure = mqtt.Client(DEVICE, 60, USER, PASS)
-- Connect to IoTHub via MQTT
azure:connect(IOTHUB, PORT, 1, 0,
-- Callback for a successful connection
function(client)
print("Connected")
end,
-- Error callback, if connection fails
function(client, reason)
print("Error Connecting: "..reason)
end
)
The response is always Error Connecting: -5 which translates to "There is no broker listening at the specified IP Address and Port".
I suspected that NodeMCU was struggling with the SSL (port 8883) connection. So I ensured that I was able to make HTTPS connections (after verifying the SSL root certificate) with the following code, this worked but did not help.
http.get("https://example.com/info", nil,
function (code, resp)
print(code, resp)
end)
I cannot seem to get this to work! #Marcel Stör - any thoughts?
Many thanks in advance.
I had the same error Error Connecting: -5 , I have set the time on the ESP and it fix this error.
You will find some information about sntp on http://thinglabs.io/workshop/esp8266/sending-d2c-messages/ (as you mention it).

Windows 10 - how to detect when a Bluetooth device is in range

I have previously paired with a Bluetooth device that supports RFCOMM.
When my app is opened I continuously try to connect to the device by opening the RFCOMM. This way my app automatically connects when the device comes in range.
deviceInfoCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
LogData(String.Format("Number of mldp devices is {0}", deviceInfoCollection.Count));
foreach (DeviceInformation deviceInfo in deviceInfoCollection)
{
LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
Then run this on a timer:
try
{
// The first time this method is invoked by a store app, it should be called
// from a UI thread in order to display the consent prompt
// https://msdn.microsoft.com/en-us/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.fromidasync
RfcommDeviceService rfcommService = await RfcommDeviceService.FromIdAsync(deviceInfo.Id);
LogData(String.Format("ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
catch (Exception)
{
LogData(String.Format("Can not request rfcomm service from device ID:{0}, NAME:{1}", deviceInfo.Id, deviceInfo.Name));
}
Is there any way to query when the device is in range , rather than trying to connect? I would prefer to only attempt connection when the device is in range.
For RFCOMM (BT2.0, BT2.1) you can run a device enumeration periodically, see also Get bluetooth devices in range
However your actual implementation with a connection attempt may work a little better.
For Bluetooth 4.0, you can listen to the advertisements of the BT module, see also https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BluetoothAdvertisement
If you're talking to an embedded device (e.g. some robot, or homebrew appliances using RFCOMM) I am afraid there is no better solution than what you're doing.
If you're taking to a phone (which supports both BT4.0 and BT2.1) you can use the BT4 advertisements to signal the proximity of the device, then connect via RFCOMM.

Resources