GetBytes() public static méthode

Creates a byte array from the hexadecimal string. Each two characters are combined to create one byte. First two hexadecimal characters become first byte in returned array. Non-hexadecimal characters are ignored.
public static GetBytes ( string hexString, int &discarded ) : byte[]
hexString string string to convert to byte array
discarded int number of characters in string ignored
Résultat byte[]
Exemple #1
0
        private void search()
        {
            Guid uuid = BluetoothService.L2CapProtocol;
            BluetoothDeviceInfo bdi;
            BluetoothAddress    ba;
            byte tmp;
            bool found = false;
            int  discarded;

            bc = new BluetoothClient();

            bc.InquiryLength = new TimeSpan(0, 0, 0, Int32.Parse(osae.GetObjectPropertyValue(pName, "Discover Length").Value), 0);
            nearosaeDevices  = bc.DiscoverDevices(10, false, false, true);

            for (int j = 0; j < nearosaeDevices.Length; j++)
            {
                string addr = nearosaeDevices[j].DeviceAddress.ToString();

                Object obj = osae.GetObjectByAddress(addr);

                if (obj == null)
                {
                    if (osae.GetObjectPropertyValue(pName, "Learning Mode").Value == "TRUE")
                    {
                        osae.ObjectAdd(nearosaeDevices[j].DeviceName, nearosaeDevices[j].DeviceName, "BLUETOOTH DEVICE", nearosaeDevices[j].DeviceAddress.ToString(), "", true);
                        osae.ObjectPropertySet(nearosaeDevices[j].DeviceName, "Discover Type", "0");
                        osae.AddToLog(addr + " - " + nearosaeDevices[j].DeviceName + ": added to OSA", true);
                    }
                }
            }

            List <OSAEObject> objects = osae.GetObjectsByType("BLUETOOTH DEVICE");

            foreach (OSAEObject obj in objects)
            {
                found = false;
                string address   = obj.Address;
                byte[] byteArray = HexEncoding.GetBytes(address, out discarded);
                tmp          = byteArray[0];
                byteArray[0] = byteArray[5];
                byteArray[5] = tmp;
                tmp          = byteArray[1];
                byteArray[1] = byteArray[4];
                byteArray[4] = tmp;
                tmp          = byteArray[2];
                byteArray[2] = byteArray[3];
                byteArray[3] = tmp;
                ba           = new BluetoothAddress(byteArray);
                bdi          = new BluetoothDeviceInfo(ba);
                osae.AddToLog("begin search for " + address, false);

                for (int j = 0; j < nearosaeDevices.Length; j++)
                {
                    if (nearosaeDevices[j].DeviceAddress.ToString() == address)
                    {
                        found = true;
                        osae.AddToLog(address + " - " + obj.Name + ": found with DiscoverDevices", false);
                    }
                }
                if (!found)
                {
                    osae.AddToLog(address + " - " + obj.Name + ": failed with DiscoverDevices", false);
                }

                try
                {
                    if (!found && (Int32.Parse(osae.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 2 || Int32.Parse(osae.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 0))
                    {
                        osae.AddToLog(address + " - " + obj.Name + ": attempting GetServiceRecords", false);

                        bdi.GetServiceRecords(uuid);
                        found = true;
                        osae.AddToLog(address + " - " + obj.Name + " found with GetServiceRecords", false);
                    }
                }
                catch (Exception ex)
                {
                    osae.AddToLog(address + " - " + obj.Name + " failed GetServiceRecords. exception: " + ex.Message, false);
                }

                try
                {
                    if (!found && (Int32.Parse(osae.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 3 || Int32.Parse(osae.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 0))
                    {
                        osae.AddToLog(address + " - " + obj.Name + ": attempting Connection", false);
                        //attempt a connect
                        BluetoothEndPoint ep;
                        ep = new BluetoothEndPoint(bdi.DeviceAddress, BluetoothService.Handsfree);
                        //MessageBox.Show("attempt connect: " + pairedDevices[i].DeviceAddress);
                        bc.Connect(ep);
                        osae.AddToLog(address + " - " + obj.Name + " found with Connect attempt", false);
                        bc.Close();
                        found = true;
                    }
                }
                catch (Exception ex)
                {
                    osae.AddToLog(address + " - " + obj.Name + " failed with Connect attempt. exception: " + ex.Message, false);
                }



                if (found)
                {
                    osae.ObjectStateSet(obj.Name, "ON");
                    osae.AddToLog("Status Updated in osae", false);
                }
                else
                {
                    osae.ObjectStateSet(obj.Name, "OFF");
                    osae.AddToLog("Status Updated in osae", false);
                }
            }
        }
Exemple #2
0
        private void search()
        {
            try
            {
                Guid uuid = BluetoothService.L2CapProtocol;
                BluetoothDeviceInfo bdi;
                BluetoothAddress    ba;
                byte tmp;
                bool found = false;
                int  discarded;

                try
                {
                    bc = new BluetoothClient();
                }
                catch
                {
                    Log.Error("No Bluetooth Adapters found!");
                    OSAEMethodManager.MethodQueueAdd(gAppName, "OFF", "", "", gAppName);
                    return;
                }

                bc.InquiryLength = new TimeSpan(0, 0, 0, int.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(gAppName, "Discover Length").Value), 0);
                nearosaeDevices  = bc.DiscoverDevices(10, false, false, true);

                for (int j = 0; j < nearosaeDevices.Length; j++)
                {
                    string addr = nearosaeDevices[j].DeviceAddress.ToString();
                    Object obj  = OSAEObjectManager.GetObjectByAddress(addr);

                    if (obj == null)
                    {
                        if (OSAEObjectPropertyManager.GetObjectPropertyValue(gAppName, "Learning Mode").Value == "TRUE")
                        {
                            OSAEObjectManager.ObjectAdd(nearosaeDevices[j].DeviceName, "", nearosaeDevices[j].DeviceName, "BLUETOOTH DEVICE", nearosaeDevices[j].DeviceAddress.ToString(), string.Empty, 50, true);
                            OSAEObjectPropertyManager.ObjectPropertySet(nearosaeDevices[j].DeviceName, "Discover Type", "0", gAppName);
                            if (gDebug)
                            {
                                Log.Debug(addr + " - " + nearosaeDevices[j].DeviceName + ": added to OSA");
                            }
                        }
                    }
                }

                OSAEObjectCollection objects = OSAEObjectManager.GetObjectsByType("BLUETOOTH DEVICE");

                foreach (OSAEObject obj in objects)
                {
                    found = false;
                    string address   = obj.Address;
                    byte[] byteArray = HexEncoding.GetBytes(address, out discarded);
                    tmp          = byteArray[0];
                    byteArray[0] = byteArray[5];
                    byteArray[5] = tmp;
                    tmp          = byteArray[1];
                    byteArray[1] = byteArray[4];
                    byteArray[4] = tmp;
                    tmp          = byteArray[2];
                    byteArray[2] = byteArray[3];
                    byteArray[3] = tmp;
                    ba           = new BluetoothAddress(byteArray);
                    bdi          = new BluetoothDeviceInfo(ba);
                    //if (gDebug) Log.Debug("Begin search for " + address);

                    for (int j = 0; j < nearosaeDevices.Length; j++)
                    {
                        if (nearosaeDevices[j].DeviceAddress.ToString() == address)
                        {
                            found = true;
                            if (gDebug)
                            {
                                Log.Debug(address + " - " + obj.Name + ": found with DiscoverDevices");
                            }
                        }
                    }
                    if (!found)
                    {
                        if (gDebug)
                        {
                            Log.Debug(address + " - " + obj.Name + ": failed with DiscoverDevices");
                        }
                    }

                    try
                    {
                        if (!found && (int.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 2 || Int32.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 0))
                        {
                            if (gDebug)
                            {
                                Log.Debug(address + " - " + obj.Name + ": attempting GetServiceRecords");
                            }

                            bdi.GetServiceRecords(uuid);
                            found = true;
                            if (gDebug)
                            {
                                Log.Debug(address + " - " + obj.Name + " found with GetServiceRecords");
                            }
                        }
                    }
                    catch (Exception ex)
                    { if (gDebug)
                      {
                          Log.Debug(address + " - " + obj.Name + " failed GetServiceRecords. exception: " + ex.Message);
                      }
                    }

                    try
                    {
                        if (!found && (int.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 3 || int.Parse(OSAEObjectPropertyManager.GetObjectPropertyValue(obj.Name, "Discover Type").Value) == 0))
                        {
                            if (gDebug)
                            {
                                Log.Debug(address + " - " + obj.Name + ": attempting Connection");
                            }
                            //attempt a connect
                            BluetoothEndPoint ep;
                            ep = new BluetoothEndPoint(bdi.DeviceAddress, BluetoothService.Handsfree);
                            //MessageBox.Show("attempt connect: " + pairedDevices[i].DeviceAddress);
                            bc.Connect(ep);
                            if (gDebug)
                            {
                                Log.Debug(address + " - " + obj.Name + " found with Connect attempt");
                            }
                            bc.Close();
                            found = true;
                        }
                    }
                    catch (Exception ex)
                    { Log.Error(address + " - " + obj.Name + " failed with Connect attempt. exception: " + ex.Message); }

                    if (found)
                    {
                        OSAEObjectStateManager.ObjectStateSet(obj.Name, "ON", gAppName);
                        if (gDebug)
                        {
                            Log.Debug(obj.Name + " Status Updated in osae");
                        }
                    }
                    else
                    {
                        OSAEObjectStateManager.ObjectStateSet(obj.Name, "OFF", gAppName);
                        if (gDebug)
                        {
                            Log.Debug(obj.Name + " Status Updated in osae");
                        }
                    }
                }
            }
            catch (Exception ex)
            { Log.Error("Error searching for devices", ex); }
        }