在 android 上检查特定设备上的蓝牙状态

Check Bluetooth State on specific device on android

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

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

我在论坛上找到了这个:How to programmatically tell if a Bluetooth device is connected?

There is no way to retrieve the list of connected devices at application startup. The Bluetooth API will only let you listen to connection changes.

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

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) {
}}

我可以判断某些设备是否已连接,但不能判断所有设备!! 我用便携式扬声器和其他设备尝试了代码,在这些设备中,通话和声音负责并且它完美地工作:如果设备连接它 return 2,如果没有连接它 return 0。 但是当我在我真正的 objective(这是一辆汽车)上尝试它时,它随时都 return 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)

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

我终于完成了工作。蓝牙汽车被认为是耳机所以我专注于它:

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
        }
    }
};}