2

I'm currently working on an application, which would send a message to a Bluetooth LE device. Everything works fine on the first start up, but on the second start up, I get an exception.

Application Code

getDevices() function

 public async Task<List<string>> getDevices()
    {
        Debug.WriteLine("C");
        foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("0000ffe0-0000-1000-8000-00805f9b34fb"))))
        {
            Debug.WriteLine("D");
            Debug.WriteLine(di.Name);
            Debug.WriteLine(di.Id);
            Debug.WriteLine(di.IsEnabled);
            GattDeviceService bleService = await GattDeviceService.FromIdAsync(di.Id);
            Debug.WriteLine(bleService.Device.ConnectionStatus.ToString());
            Debug.WriteLine("E");
            Debug.WriteLine(GattCharacteristic.ConvertShortIdToUuid(0xFFE1).ToString());
            // TODO: Throws exception if not paired before startup because pairing info is not stored on device
            accConfig = bleService.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0xFFE1))[0];
            Debug.WriteLine("F");
        }
        return deviceList;
    }

sendMessage() function

 public async void sendMessage(string messageToSend)
        {
            // TODO: implement
            await accConfig.WriteValueAsync((Encoding.UTF8.GetBytes(messageToSend)).AsBuffer());
            Debug.WriteLine("G");
        }

Errors

At the await accConfig line I get an exception:

An exception of type 'System.Exception' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: [...] (Exception from HRESULT: 0x80070572)

Another thing is that on the Settings tab, my Bluetooth Device keeps switching from Connected to Paired all the time. I have no idea what causes that.

UPDATE 2017/04/23 Device Code and Info

I include the code on the Arduino. I'm using an original HM-10, on this page the first version on the top. Mine is unbranded (so it doesn't have the Keyes logo) but other than that it should not have any difference. (Though it could be a clone, but I doubt it as much as I know).

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(9, 10); // RX, TX
  char commandbuffer[50];
      int j = 0;
void setup()
{

  memset(commandbuffer, 0, sizeof(commandbuffer));
  analogWrite(12, 255);
  analogWrite(11, 0);
  // Start the hardware serial port
  Serial.begin(19200);
  bluetooth.begin(9600);
  // un REM this to set up a Master and connect to a Slave

  Serial.println("BLE CC41A Bluetooth");
  Serial.println("----------------------------------");
  Serial.println("");
  Serial.println("Trying to connect to Slave Bluetooth");
  delay(1000);
  bluetooth.print("AT"); // just a check
  delay(2000);

  delay(2000);
  bluetooth.print("AT+ROLE0");
  delay(2000);
  /*
    bluetooth.println("AT+HELP");
    delay(2000);
    bluetooth.println("AT+CONA0xE4F89CF8AB87");*/
  /*bluetooth.println("AT+CONE4F89CF8AB87");
    delay(2000);*/
  // discover

  /*
    bluetooth.println("AT+INQ"); // look for nearby Slave
    delay(5000);
    bluetooth.println("AT+CONN1"); // connect to it */
}
void loop()
{
  bluetooth.listen();
  // while there is data coming in, read it
  // and send to the hardware serial port:
  while (bluetooth.available() > 0) {
    char inByte = bluetooth.read();
    Serial.write(inByte);
  }
  // Read user input if available.
  if (Serial.available()) {
    delay(10); // The DELAY!
    char temp = Serial.read();
    if (temp == '\n')
    {
      bluetooth.print(commandbuffer);
      Serial.print('\n'); // Because it needs it even if Bluetooth received command happened earlier
      Serial.println(commandbuffer);
      memset(commandbuffer, 0, sizeof(commandbuffer));
      j = 0; // Reset
    }
    else
    {
      commandbuffer[j++] = temp;
    }
    delay(500);
  }

}
boomkin
  • 292
  • 1
  • 12

1 Answers1

0

This always happens if the pairing info is not stored on the device. Every time you reconnect you must un-pair and re-pair, because on Windows 10 your device is paired, but your device is not.

GrooverFromHolland
  • 768
  • 1
  • 11
  • 15
  • I can program the Bluetooth device, so how is it possible to store that information given this set of commands in the datasheet: [here](http://fab.cba.mit.edu/classes/863.15/doc/tutorials/programming/bluetooth/bluetooth40_en.pdf) I would like to keep the device in peripheral mode if possible. – boomkin Apr 21 '17 at 23:41
  • If it is a real JNHuaMao product you would have no problems, but if it is a clone like "'Bolutek" it is not going to work. There is a possibility to to change the firmware, but I did not have success with that. Spend a dollar more and get the real thing like Keyestudio Mini HM-10 Bluetooth V4.0 . – GrooverFromHolland Apr 22 '17 at 17:40
  • I know about the different clones of the product, I think I have the second most original version (the original but without the Keyes logo), do you think it is possible to get it store the pairing info somehow on that one? – boomkin Apr 22 '17 at 21:40
  • On http://www.martyncurrey.com/bluetooth-modules/ , I have the first one – boomkin Apr 22 '17 at 23:12