有没有办法弄清楚你是否连接到蓝牙设备?
我有我的应用程序连接,发送/接收就好了。但是我需要一种方法来看看我是否仍然保持连接说...如果我走出范围并走回范围。
我注意到蓝牙套接字中没有isConnected功能,就像TCP中的内容一样......有没有办法查看你是连接还是与你应该连接的设备进行通信?
答案 0 :(得分:0)
我能够解决这个问题的唯一方法是每秒发送一次“心跳”消息。如果它没有通过,那么我认为蓝牙已断开连接。
答案 1 :(得分:0)
发送尽可能少的数据,看看是否收到回复。如果你不这样做,那么你就没有联系。
答案 2 :(得分:0)
以下广播接收器值应告知您何时断开任何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);