安卓:如何找出蓝牙设备的连接名称? [英] Android: How to find out name of the bluetooth device connected?
问题描述
基本上我试着在这里两件事情,我试着开始时,我的蓝牙设备连接到特定设备的烤面包(所以需要检查是否是特定的蓝牙名称),如果是这样的特定设备,然后我想当连接到特定的蓝牙设备显示敬酒。我也想展示敬酒时,我的蓝牙断开连接到特定的蓝牙设备。
这里是我的code:
在的manifest.xml
Basically im trying 2 things here, Im trying to start a toast when my bluetooth device is connected to a particular device (so need to check if that is the particular bluetooth name), if that is the particular device then i want to show a toast when connected to that particular bluetooth device. I also want to show a toast when my bluetooth is disconnected to that particular bluetooth device. Here's my code: in manifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<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>
类的code:
公共类MyBluetoothReceiver扩展广播接收器{
public class MyBluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "RECEIVER CALLED!!", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(
"android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
// code for Bluetooth connect
Toast.makeText(context, "CONNECTED!!", Toast.LENGTH_LONG).show();
}
if(intent.getAction().equals(
"android.bluetooth.device.action.ACL_DISCONNECTED")){
//code for Bluetooth disconnect;
Toast.makeText(getApplicationContext(),"DISCONNECTED",Toast.LENGTH_LONG).show();
}
}
}
在我的code即时得到接收器被称为土司正确,甚至断开的敬酒也正在而是连接永远不会奏效敬酒。
In my code im getting receiver called toast properly and even the toast for disconnected is also working but toast of connected never works.
请让我知道为什么关连敬酒不工作,当连接到特定设备如何使这个code的工作(我不想显示此敬酒所有设备)
推荐答案
您广播接收机更改为:
private final BroadcastReceiver mReceiver = new 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)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//you can get name by device.getName()
} else if (BluetoothAdapter.ACL_DISCONNECTED
.equals(action)) {
}
}
};
这篇关于安卓:如何找出蓝牙设备的连接名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!