首页 > 解决方案 > 在 android 上检查特定设备上的蓝牙状态

问题描述

我试图在我的应用程序启动时知道配对设备是否已连接。在实践中,主应用程序给了我一个 MAC 地址,如果我们连接或没有连接,我必须做出回应。

BroadcastReceiver 不能是一个完整的选项,因为如果用户在启动应用程序之前启动与设备的连接,我将无法知道我是否连接到设备。

我在论坛上找到了这个:如何以编程方式判断蓝牙设备是否已连接?

无法在应用程序启动时检索已连接设备的列表。蓝牙 API 只会让您收听连接更改。

但我认为这不再准确,因为使用此代码:

public class cServiceListener  implements BluetoothProfile.ServiceListener  {
private static final int[] states={ BluetoothProfile.STATE_DISCONNECTING,
        BluetoothProfile.STATE_DISCONNECTED,
        BluetoothProfile.STATE_CONNECTED,
        BluetoothProfile.STATE_CONNECTING};
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {


    //List<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

    Log.i("myTag","\n\n<==============profile connexion state =============> : "+ mBluetoothAdapter.getProfileConnectionState(1));

    for (BluetoothDevice loop:devices){
        Log.i("myTag","Nom du device :" +loop.getName()+" Etat du Device:"+bluetoothProfile.getConnectionState(loop)
        + "  Type du device : "+ loop.getType() + " Classe du device : " + loop.getBluetoothClass().toString() );
    }
}

@Override
public void onServiceDisconnected(int profile) {
}}

我能够判断某些设备是否已连接,但不能全部连接!我尝试使用便携式扬声器和其他设备负责呼叫和声音的代码并且它工作完美:如果设备已连接,则返回 2,否则返回 0。但是当我在我的真正目标上尝试时(这是一辆车)它随时返回 0。测试车只接听电话(你不能用蓝牙连接播放音乐,太旧了)。所以我认为更现代的汽车可以工作..

首先我认为那辆车使用 BLE 技术,所以我尝试使用这段代码

         List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
            Log.d("myTag","\n Taille de la liste : " + devices.size());
            for(BluetoothDevice device : devices) {

                if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
                    Log.d("myTag","\n BLE <===> Nom du device :" + device.getName());
                }
                Log.d("myTag","\n<====NONE BLE=====> // \n\n Nom du device :" + device.getName()+"\n\n");
            }

但实际上这不是重点,这是行不通的。这很奇怪,因为

 mBluetoothAdapter.getProfileConnectionState(1)

仅当我连接到汽车时返回正确的值。我真的不知道该怎么做以及为什么 bluetoothProfile.getConnectionState() 返回错误的值。如果有人有任何想法,我会很高兴知道。

标签: javaandroidbluetoothandroid-bluetooth

解决方案


最后我完成了工作。蓝牙汽车被认为像耳机,所以我专注于它:

public static void startListening() {
final BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Bundle info = intent.getExtras();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        String deviceAddress =  device.getAddress();
        if (action.equals( BluetoothDevice.ACTION_ACL_CONNECTED)){
            //do some stuff with connected device
        }
        if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)){
            // do things with disconnected
        }
    }
};}

推荐阅读