我想通过 Android BluetoothPROfile 连接到 Fora 温度计并获取读数。
以下是我的方法:-
-
在 OnCreate() 中我写了这段代码:-
if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
BluetoothProfile.HEALTH)) {
Toast.makeText(this, "No Health Profile Supported",
Toast.LENGTH_LONG);
return;
}
-
这会触发下面提到的 mBluetoothServiceListener 回调:-
@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;
}
}
};
-
此后,代码搜索附近的蓝牙设备并将其显示在列表中。
该列表项的 onclicklistener 调用以下代码:-
boolean bool = mBluetoothHealth.registerSinkAppConfiguration(TAG, HEALTH_PROFILE_SOURCE_DATA_TYPE, mHealthCallback);
// where HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1008 (it's a body thermometer)
-
注册过程完成后,我尝试像这样连接设备:-
boolean bool = mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
-
完成上述所有步骤后,每当
设备连接状态改变
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 {
}
}
}
};
在上述情况下,我精确地收到了 2 次 BluetoothHealthCallback :-
我第一次得到 prevState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED 和
newState = BluetoothHealth.STATE_CHANNEL_CONNECTING
在第二次时,我得到 prevState = BluetoothHealth.STATE_CHANNEL_CONNECTING 和
newState = BluetoothHealth.STATE_CHANNEL_DISCONNECTED
即尝试连接到设备但未成功。
此外,在这两个回调中,ParcelFileDescriptor 均为 null
*注意:设备已配对
您认为下面的“如果”陈述正确吗?
if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED &&
newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
它应该是其中之一(就像经典蓝牙设备一样)
if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
当您先连接然后再连接时
或者我可能只是
if (newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
对其他“if”语句进行同样的更正。
还,
设备是否支持配对并连接选项?如果是的话你可以尝试connect手动进行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)