Skip to content

Getting the profile proxy object

Ivan Baranov edited this page Dec 15, 2015 · 2 revisions

To get profile proxy object:

rxBluetooth.observeBluetoothProfile(myProfile)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribeOn(Schedulers.computation())
      .subscribe(new Action1<ServiceEvent>() {
        @Override public void call(ServiceEvent serviceEvent) {
          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);