Skip to content

Getting the profile proxy object

Ivan Baranov edited this page Jun 21, 2017 · 2 revisions

To get profile proxy object:

rxBluetooth.observeBluetoothProfile(myProfile)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .subscribe(new Consumer<ServiceEvent>() {
    @Override public void accept(ServiceEvent serviceEvent) throws Exception {
      switch (serviceEvent.getState()) {
       case CONNECTED:
            BluetoothProfile bluetoothProfile = serviceEvent.getBluetoothProfile();
            List<BluetoothDevice> devices = bluetoothProfile.getConnectedDevices();
            for ( final BluetoothDevice dev : devices ) {
              //..
            }
            break;
       case DISCONNECTED:
            //serviceEvent.getBluetoothProfile() returns null
            break;
        }
      }
    });

myProfile can be one of BluetoothProfile.HEALTH, BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.GATT or BluetoothProfile.GATT_SERVER

Clients should close profile proxy when they are no longer using the proxy obtained from observeBluetoothProfile:

rxBluetooth.closeProfileProxy(int profile, BluetoothProfile proxy);