当前连接的蓝牙设备安卓

2022-09-03 07:05:33

我可以在Android的蓝牙设备中看到两种状态。1. 配对 2.连接。-
我正在尝试在Android中获取当前连接的蓝牙设备。但我只从我需要当前连接的设备中获取配对设备列表。我怎么能得到这个。请有人帮助我实现这一目标。提前致谢。adapter.getBondedDevices();


答案 1

这很简单。安卓蓝牙管理器有方法

getConnectedDevices()

实现如下:

BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    List<BluetoothDevice> connected = manager.getConnectedDevices(GATT);
    Log.i("Connected Devices: ", connected.size()+"");

如果您想了解有关已连接设备的更多详细信息,则可以使用上面的列表方法将其放入for循环中,并获取已连接的每个蓝牙设备的内部详细信息。

原木:

12-20 18:04:09.679 14933-14933/com.salman.dleague.blescanning I/Connected Devices:: 2

希望:)有帮助


答案 2

将其添加到清单文件中

<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" 
/>
<action 
android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" 
/>           
</intent-filter>  
</receiver>  

添加此类

public class MyBluetoothReceiver extends BroadcastReceiver {
 @Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    // When discovery finds a device
    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

    BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

     Toast.makeText(getApplicationContext(),device.getName() +" CONNECTED",Toast.LENGTH_LONG).show();

    } else if (BluetoothAdapter.ACL_DISCONNECTED
            .equals(action)) {

    }
}
}

推荐