[英]Currently connected bluetooth device android
我可以在Android中的藍牙設備中看到兩種狀態。 1.配對2.已連接。 -
我正在嘗試在Android中獲取當前連接的藍牙設備。 但我只從adapter.getBondedDevices();
獲得配對設備列表adapter.getBondedDevices();
我需要當前連接的設備。 我怎么能得到這個。 請有人幫助我實現這一目標。 提前致謝。
這很直截了當。 Android 藍牙管理器有方法
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
希望它有用:)
在清單文件中添加此項
<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)) {
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.