Failed to connect to a Bluetooth Bluetooth mobile device using Android BluetoothProfile

I want to connect to a Fora thermometer via a Bluetooth Bluetooth file and get readings. The following is my approach: -

  • In OnCreate (), I wrote this piece of code: -

    if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
            BluetoothProfile.HEALTH)) {
        Toast.makeText(this, "No Health Profile Supported",
                Toast.LENGTH_LONG);
        return;
    }
    
  • This calls the mBluetoothServiceListener callback, which is listed below: -

    @SuppressLint("NewApi")
    private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
    new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
    
        Log.d(TAG, "onServiceConnected to profile: " + profile + " while health is " + BluetoothProfile.HEALTH);
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = (BluetoothHealth) proxy;
            if (Log.isLoggable(TAG, Log.DEBUG))
                Log.d(TAG, "onServiceConnected to profile: " + profile);
        }
        else if(profile == BluetoothProfile.HEADSET)
        {
            Log.d(TAG, "onServiceConnected to profile: " + profile);
        }
    }
    
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEALTH) {
            mBluetoothHealth = null;
        }
    }
    };
    
  • After that, the code searches for neighboring Bluetooth devices and displays it in the list. The onclicklistener of this list item calls the following code: -

    boolean bool = mBluetoothHealth.registerSinkAppConfiguration(TAG, HEALTH_PROFILE_SOURCE_DATA_TYPE, mHealthCallback);
    
    // where HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1008 (it a body thermometer)
    
  • As soon as the registration process is completed, I will try to connect the device as follows: -

        boolean bool = mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
    
  • Once all of the above steps have been completed, BluetoothHealthCallback is called whenever the device's connection status changes

            private final BluetoothHealthCallback mHealthCallback = new BluetoothHealthCallback() {
    // Callback to handle application registration and unregistration events.  The service
    // passes the status back to the UI client.
    public void onHealthAppConfigurationStatusChange(BluetoothHealthAppConfiguration config,
            int status) {
        if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_FAILURE) {
            mHealthAppConfig = null;
        } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
            mHealthAppConfig = config;
        } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
                status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
        }
    }
    
    // Callback to handle channel connection state changes.
    // Note that the logic of the state machine may need to be modified based on the HDP device.
    // When the HDP device is connected, the received file descriptor is passed to the
    // ReadThread to read the content.
    public void onHealthChannelStateChange(BluetoothHealthAppConfiguration config,
            BluetoothDevice device, int prevState, int newState, ParcelFileDescriptor fd,
            int channelId) {
        if (Log.isLoggable(TAG, Log.DEBUG))
            Log.d(TAG, String.format("prevState\t%d ----------> newState\t%d",
                    prevState, newState));
        if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED &&
                newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
            if (config.equals(mHealthAppConfig)) {
                mChannelId = channelId;
    
                (new ReadThread(fd)).start();
            } else {
    
            }
        } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
                newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
    
        } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
            if (config.equals(mHealthAppConfig)) {
    
            } else {
    
            }
        }
    }
        };
    

In the above case, I get the BluetoothHealthCallback exactly 2 times: -

  • prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED newState = BluetoothHealth.STATE_CHANNEL_CONNECTING

  • prevState = BluetoothHealth.STATE_CHANNEL_CONNECTING newState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED

. , . ParcelFileDescriptor null

* :

+4
1

, , ""?

if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED && & &             newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

( Bluetooth)

if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING && & &             newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

,

if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

if.

, ? , .

+1

Source: https://habr.com/ru/post/1534638/


All Articles