我想通过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);
完成上述所有步骤后,随时都会调用BluetoothHealthCallback 设备连接状态发生变化
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为空
*注意:设备已配对
答案 0 :(得分:1)
你认为你的下面"如果"声明是对的吗?
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){
对其他"同样的纠正"如果"言。
另外, 设备是否支持配对和连接选项?如果是,那么您可以尝试手动连接。