2013-12-26 4 views
2

Я хочу фильтровать BluetoothDevice при обнаружении и сохранять только те, которые запускаются при прослушивании одним и тем же программным обеспечением. Например, образец Android BluetoothChat, он должен показывать только те устройства bluetooth, которые запускаются BluetoothChat тоже.Как отфильтровать BluetoothDevice при обнаружении

Вот мой код.

public class DeviceListActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Setup UI here 
     ...... 

     // Register for broadcasts when a device is discovered 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(mReceiver, filter); 

     // Register for broadcasts when discovery has finished 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(mReceiver, filter); 

     // Get the local Bluetooth adapter 
     mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 

     // Request discover from BluetoothAdapter 
     mBtAdapter.startDiscovery(); 
    } 

    // The BroadcastReceiver that listens for discovered devices and 
    // changes the title when discovery is finished 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

       // *** My problem is here. I got a BluetoothDevice now, but how can I 
       // *** check whether it is created by my software? 
       // *** if I can't, I have to add all of them to my list, and when user 
       // *** chooses it, connection will fail if it's not created by my software. 
       mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
      // When discovery is finished, change the Activity title 
      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       // Discovery is finished... 
       // wrap up code here... 
       ...... 
      } 
     } 
    }; 
} 

ответ

0

Вы можете сделать одну вещь:

При запуске Bluetooth с помощью программного обеспечения, изменить имя вашего Bluetooth к определенному шаблону, так что вы можете идентифицировать устройство запуска вашего программного обеспечения.

Затем, при поиске устройства Bluetooth, проверьте, не совпадает ли имя устройства Bluetooth, которое вы набрали во время поиска, из шаблона, который вы определили.

Чтобы изменить имя устройства:

Change the Android bluetooth device name

Смежные вопросы