title: Android 4.3: BLE: Filtering behaviour of startLeScan()
body: <p>I'm working on a BluetoothLE sensor device, for which I need to form a one-to-many broadcast of data.  As per the spec, peripherals may only have a single master, and due to limitations of the chip and stack I'm designing on, a master can only have three slaves.  From what I understand, Android cannot become a BLE slave anyway, so having my device as a master is not an option.</p>
  
  <p>Both the BT4 spec and manufacturer documentation talk about another mode of operation, referred to as Broadcast mode.  In broadcast mode, a connection is never made, and the application data is transmitted as part of the advertising packet.  This will exactly fit my needs, as many Android/iOS phones can simultaneously scan down each packet.  An advertising packet is transmitted multiple times in bursts, so I suspect reception of data to be mostly reliable.  If a packet is lost here and there, it can be tolerated.</p>
  
  <p>Where this gets interesting, is that I want these packets to carry live sensor data, that updates at a rate of 10-20Hz.  From examples I've found on the web, BLE in this mode is mostly being used for "iBeacon" type implementations, where they are broadcasting static data.  I cannot find any information on how advertising packets are filtered within the Android stack.  It could be that they return one result per Bluetooth hardware address, or it could be the unique combination of address and data.  The second option would work for this application.  If starting and stopping the scan resets the filter, I can make something work as well.</p>
  
  <p>The Android documentation mentions nothing about how device filtering in the scan method works.  I've been able to find one post on the net attempting to solve this same problem, which has an unresolved response: <a href="https://groups.google.com/forum/#!msg/android-platform/vTPV7GFqTic/OyWTCLrvGE8J" rel="noreferrer">BLE: Multiple discovery of the same peripheral during scan</a>.  In iOS, my colleague informs me that there is a parameter that can be passed to the scan function that makes this possible.</p>
  
  <p>I've attempted to trace the code back from the startLeScan() call in the Android source, but the code is quite complex, and the use of abstraction has made it difficult to identify the implementation of the object that contains it.  The farthest I've gotten is to an IBluetoothGatt object returned from the BluetoothManagerService class method getBluetoothGatt().  This object  receives the request to start scanning.  It is being instantiated around <a href="https://github.com/android/platform_frameworks_base/blob/dbc51de44fe4f9a7f81528204250de32ec405d39/services/java/com/android/server/BluetoothManagerService.java#L790" rel="noreferrer">line 790 of BluetoothManagerService.java</a> on the current revision live on github.  The object is being cast from the result of a message, so I suspect maybe the result is phone/driver specific. It is beyond my expertise to be able to trace it any further.</p>
  
  <p>Another question I would like to resolve is how rapidly the scanning can be switched on and off.  Scanning is a power intensive operation, yet the broadcast of data will happen periodically on a fairly precise, real time timer.  As a result, it would be a great optimization if the scan can be switched on and off, such that the broadcast and scan are synchronized, with the scanner shut down the other 90%+ of the time.  This will likely need to be tested experimentally.</p>
  
  <p>I'm still doing feasibility research to see if this is possible for our accessory for Android.  My present phone cannot yet run version 4.3, so I have no way of testing/hacking this experimentally.</p>