Ria*_*Ria 14 java connection android bluetooth device
我可以在Android中的蓝牙设备中看到两种状态.1.配对2.已连接.-
我正在尝试在Android中获取当前连接的蓝牙设备.但我只需要配对设备列表,adapter.getBondedDevices();我需要当前连接的设备.我怎么能得到这个.请有人帮助我实现这一目标.提前致谢.
这很直截了当.Android 蓝牙管理器有方法
getConnectedDevices()
实施如下:
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
List<BluetoothDevice> connected = manager.getConnectedDevices(GATT);
Log.i("Connected Devices: ", connected.size()+"");
Run Code Online (Sandbox Code Playgroud)
如果您想了解有关已连接设备的更多详细信息,可以使用上面的列表方法将其置于for循环中,并获取所连接的每个蓝牙设备的内部详细信息.
日志:
12-20 18:04:09.679 14933-14933/com.salman.dleague.blescanning I/Connected Devices:: 2
Run Code Online (Sandbox Code Playgroud)
希望它有用:)
将其添加到您的清单文件中
<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>
Run Code Online (Sandbox Code Playgroud)
添加该类
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)) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2049 次 |
| 最近记录: |