有沒有辦法確定您是否連接到藍牙設備?Android藍牙:IsConnected?
我有我的應用程序連接,發送/接收就好了。不過,我需要一種方法來查看我是否仍然連接,如果我走出範圍並走回範圍。
我注意到,藍牙套接字中沒有isConnected功能,就像TCP中的東西一樣......有沒有辦法看你是否連接,或者與設備通信,你應該連接?
有沒有辦法確定您是否連接到藍牙設備?Android藍牙:IsConnected?
我有我的應用程序連接,發送/接收就好了。不過,我需要一種方法來查看我是否仍然連接,如果我走出範圍並走回範圍。
我注意到,藍牙套接字中沒有isConnected功能,就像TCP中的東西一樣......有沒有辦法看你是否連接,或者與設備通信,你應該連接?
我能夠解決這個問題的唯一方法是每秒發送一次「心跳」消息。如果它沒有通過,那麼我認爲藍牙已斷開連接。
發送您可以查看的最小數量的數據並查看是否收到響應。如果你不這樣做,那麼你就沒有聯繫。
下面的廣播接收器的值應該告訴你,當任何BT設備斷開連接:
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); // API 5
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); // API 5
你或許應該實現一個BluetoothProfile.ServiceListener代理偵聽器,如果你有興趣在一個特定的設備:
private class MyBluetoothHeadsetListener //
implements BluetoothProfile.ServiceListener
{
@Override
public void onServiceDisconnected(int profile)
{
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
if (profile == BluetoothProfile.A2DP)
{
BluetoothA2dp bluetoothA2dp = (BluetoothA2dp) proxy;
mDevicesA2dp = bluetoothA2dp.getConnectedDevices();
for (BluetoothDevice deviceA2dp : mDevicesA2dp)
{
boolean isA2dpPlaying = bluetoothA2dp.isA2dpPlaying(deviceA2dp);
}
return;
}
if (profile == BluetoothProfile.HEADSET)
{
BluetoothHeadset bluetoothHeadset = (BluetoothHeadset) proxy;
mDevicesNonA2dp = bluetoothHeadset.getConnectedDevices();
if (mDevicesNonA2dp.size() > 0)
{
for (BluetoothDevice deviceNonA2dp : mDevicesNonA2dp)
{
BluetoothClass bluetoothClass = deviceNonA2dp.getBluetoothClass();
String bluetoothDeviceClass = bluetoothClassToString(bluetoothClass);
boolean isAudioConnected = bluetoothHeadset.isAudioConnected(deviceNonA2dp);
}
}
return;
}
}
}
...
private MyBluetoothHeadsetListener mProfileListener = new MyBluetoothHeadsetListener();
...
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.HEADSET);
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.A2DP);