0

My app scan Bluetooth devices and show in a table. But, sometimes it scan the same device two or more times and show the same device many times in the table.

I need to filter it. When the Name or UUID of the device is repeated the table will show just one time.

EDIT: This What I tried, but isn't work...

            CBPeripheral peripheral = this._peripherals [indexPath.Row];
        List<string> filter = new List<string>();
        filter.Add (peripheral.Identifier.AsString());

        string[] array = {};

        foreach (var c in filter) {
            if (!ReferenceEquals (c, array)) {
                int x = array.Length;
                filter.CopyTo (0, array, 0, x);
            }

        }

        foreach (string i in array) {
            Console.WriteLine ("ARRAY: "+i.ToString());
        }
  • Look the accepted answer to this question on the link ! http://stackoverflow.com/questions/1439564/iphone-getting-unique-values-from-nsarray-object – Nofel Mahmood Sep 15 '14 at 14:13
  • What does the data look like in the table view? Are you using model objects, or just the name of the device in an array? When you scan a device twice, do you have two model objects with identical values, or is it the same object in the array twice? If the latter, use an NSSet or NSOrderedSet instead of an array. – Sean Kladek Sep 15 '14 at 14:14
  • Nofel, I'm a Xamarin developer. You send me an topic with Xcode code. I'm a beginner – questioner thinker Sep 15 '14 at 14:32

3 Answers3

0

While browsing the devices in the area, store every device in an array.

Then you can either

  • clean the array of doubles and use that to populate the tableview
  • Add every object from the array to the tableview, and check if what you're adding to the tableview doesn't already exist in the array.

Or if you can, simply store their UDID or any unique code that you can use in a dictionary, and use the same technique as above before filling your tableview with the devices.

Ask me if i'm unclear or if you need more help

EDIT 1 :

You can find a way to clean an array of doubles on this post : The best way to remove duplicate values from NSMutableArray in Objective-C?

You can also use sort descriptors to sort the array and then compare every element to the next one and delete if necessary. you can find information about sorting arrays easily on the internet, as well as cleaning arrays.

Community
  • 1
  • 1
Gil Sand
  • 5,802
  • 5
  • 36
  • 78
  • Ok, but how I clean the array? – questioner thinker Sep 15 '14 at 14:33
  • Are you looking for code or the concept? I'd say loop on it and compare every object to every other object. If they match (with any unique ID that you can get), remove one of the twins. – Gil Sand Sep 15 '14 at 14:34
  • I edited my answer, are you doing ok now? you can edit your question with some code if you need more help, so we can reply with your own edited code. – Gil Sand Sep 15 '14 at 14:50
  • I use a list and add to it all the UUIDs from devices I can find with my iphone. Then I compare the UUIDs array with the variable array and if the name don't exist, it will copy to the variable array which was "null"; So it will apear on Console.WriteLine() What is wrong in it? – questioner thinker Sep 15 '14 at 18:27
0

If you can store you values in an NSSet (There is a Mutable Variant as well) sets are unique in that they don't allow duplicates. Be aware though that an NSSet is unordered so there's also a variant for that NSOrderedSet. Have a play around with sets i think they'll fit your purpose quite nicely and they can be always converted back to an array once you've got rid of the dupes.

pidge
  • 20
  • 3
0

Assuming your data is List<string>, you either need to

a. check for duplicates when you add new item to your list

if (!data.Contains(bt_id)) {
  data.Add(bt_id);
}

b. remove duplicates after the fact

// requires System.Linq
var display_data = data.Distinct();
Jason
  • 86,222
  • 15
  • 131
  • 146