コード例 #1
0
        public async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            bool foundNewCar = false;

            byte[]            localName  = null;
            DeviceInformation DeviceInfo = null;
            bool   connectFailure        = false;
            string outputBuffer          = "";

            string strMacAddr = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
                                              (eventArgs.BluetoothAddress >> (8 * 5)) & 0xff,
                                              (eventArgs.BluetoothAddress >> (8 * 4)) & 0xff,
                                              (eventArgs.BluetoothAddress >> (8 * 3)) & 0xff,
                                              (eventArgs.BluetoothAddress >> (8 * 2)) & 0xff,
                                              (eventArgs.BluetoothAddress >> (8 * 1)) & 0xff,
                                              (eventArgs.BluetoothAddress >> (8 * 0)) & 0xff);

            outputBuffer  = "Advertisement:\r\n";
            outputBuffer += "MAC: " + strMacAddr + " - Signal Strength: " + eventArgs.RawSignalStrengthInDBm + "dBm\r\n";

            /*
             * foreach (var temp in eventArgs.Advertisement.ServiceUuids)
             * {
             *  displayOutput += "Service UUID: " + temp.ToString());
             *  if(temp.ToString() == "be15beef-6186-407e-8381-0bd89c4d8df4")
             *  {
             *      try
             *      {
             *          BluetoothLEDevice bleDevice = Connect(eventArgs.BluetoothAddress).Result;
             *          localName = Encoding.ASCII.GetBytes(bleDevice.Name);
             *          DeviceInfo = bleDevice.DeviceInformation;
             *          bleDevice?.Dispose();
             *          bleDevice = null;
             *      }
             *      catch(Exception ex)
             *      {
             *          connectFailure = true;
             *      }
             *  } else
             *  {
             *      return;
             *  }
             * }
             */
            // Does this exist in our list...
            var car = lstAnki.Where(c => c.mac_address == eventArgs.BluetoothAddress).FirstOrDefault();

            if (car == null)
            {
                car             = new anki_vehicle();
                car.mac_address = eventArgs.BluetoothAddress;
                foundNewCar     = true;
            }

            if (eventArgs.Advertisement.LocalName != "")
            {
                localName = Encoding.ASCII.GetBytes(eventArgs.Advertisement.LocalName);
                if (rawBytes)
                {
                    outputBuffer += "LocalName: ";
                    outputBuffer += GetHexStringFrom(localName) + "\r\n";
                }
                var    state = localName[0] & 0xff;
                byte[] name  = new byte[localName.Length - 8 - 1];
                Array.Copy(localName, 8, name, 0, localName.Length - 8 - 1); // Minus 1 additional to avoid null terminator...
                outputBuffer    += "Name: " + Encoding.ASCII.GetString(name) + "\r\n";
                outputBuffer    += "Battery Full: " + IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_FULL_BATTERY) + "\r\n";
                outputBuffer    += "Battery Low: " + IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_LOW_BATTERY) + "\r\n";
                outputBuffer    += "On Charger: " + IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_ON_CHARGER) + "\r\n";
                car.name         = Encoding.ASCII.GetString(name);
                car.full_battery = IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_FULL_BATTERY);
                car.low_battery  = IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_LOW_BATTERY);
                car.on_charger   = IS_VEHICLE_STATE_SET(state, VEHICLE_STATE_ON_CHARGER);
            }

            var i = 0;

            foreach (var temp in eventArgs.Advertisement.ManufacturerData)
            {
                var data = new byte[temp.Data.Length];
                using (var reader = DataReader.FromBuffer(temp.Data))
                {
                    reader.ReadBytes(data);
                }
                if (rawBytes)
                {
                    outputBuffer += "Manuf Data " + i.ToString() + ": ";
                    outputBuffer += GetHexStringFrom(data) + "\r\n";
                }
                i++;
            }
            i = 0;
            foreach (var temp in eventArgs.Advertisement.DataSections)
            {
                var data = new byte[temp.Data.Length];
                using (var reader = DataReader.FromBuffer(temp.Data))
                {
                    reader.ReadBytes(data);
                }
                if (rawBytes)
                {
                    outputBuffer += "Data Section " + i.ToString() + ": ";
                    outputBuffer += GetHexStringFrom(data) + "\r\n";
                }
                var str = Encoding.ASCII.GetString(data);
                if (i == 2)
                {
                    outputBuffer  += "ID: " + (data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24)) + "\r\n";
                    outputBuffer  += "Model: " + Enum.GetName(typeof(AnkiModel), data[3]) + "\r\n";
                    outputBuffer  += "Product ID: " + (data[1] | (data[0] << 8)) + "\r\n";
                    car.identifier = (uint)((data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24)));
                    car.model_id   = (AnkiModel)data[3];
                    car.product_id = (uint)(data[1] | (data[0] << 8));
                }
                i++;
            }

            /*
             * if (connectFailure)
             *  Console.WriteLine("Failure to connect to vehicle.  Try restarting bluetooth on your PC or reseting the vehicle by holding down the power button until it flashes.");
             */
            if (foundNewCar && (car.product_id == 48879 || car.name == "Drive"))
            {
                lstAnki.Add(car);
                if (displayOutput)
                {
                    Console.WriteLine(outputBuffer);
                }
                Console.Write("\r");
                Console.Write("Searching for cars, press any key to stop...  ({0} found)", lstAnki.Count());
            }
        }
コード例 #2
0
        public static async void Interact(anki_vehicle vehicle, GattCharacteristic readChar, GattCharacteristic writeChar)
        {
            try
            {
                var    writer   = new Windows.Storage.Streams.DataWriter();
                byte[] rawBytes = null;
                GattCommunicationStatus result;
                readChar.ValueChanged += VehicleResponse;
                if (readChar.CharacteristicProperties == (GattCharacteristicProperties.Read | GattCharacteristicProperties.Notify))
                {
                    result = await readChar.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
                }

                while (true)
                {
                    Console.Write("Please enter your command: ");
                    string input = Console.ReadLine();
                    if (input == "exit")
                    {
                        return;
                    }

                    switch (input)
                    {
                    case "ping":
                    {
                        anki_vehicle_msg msg = new anki_vehicle_msg();
                        msg.size   = ANKI_VEHICLE_MSG_BASE_SIZE;
                        msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_PING_REQUEST;
                        rawBytes   = getBytes(msg);
                        writer.WriteBytes(rawBytes);
                        result = await writeChar.WriteValueAsync(writer.DetachBuffer());

                        break;
                    }

                    case "get-version":
                    {
                        anki_vehicle_msg msg = new anki_vehicle_msg();
                        msg.size   = ANKI_VEHICLE_MSG_BASE_SIZE;
                        msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_VERSION_REQUEST;
                        rawBytes   = getBytes(msg);
                        writer.WriteBytes(rawBytes);
                        result = await writeChar.WriteValueAsync(writer.DetachBuffer());

                        break;
                    }

                    case "get-battery":
                    {
                        anki_vehicle_msg msg = new anki_vehicle_msg();
                        msg.size   = ANKI_VEHICLE_MSG_BASE_SIZE;
                        msg.msg_id = (byte)AnkiMessage.ANKI_VEHICLE_MSG_C2V_BATTERY_LEVEL_REQUEST;
                        rawBytes   = getBytes(msg);
                        writer.WriteBytes(rawBytes);
                        result = await writeChar.WriteValueAsync(writer.DetachBuffer());

                        break;
                    }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }