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);
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() {
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) {
}
}
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
* :