Android:如何找出连接的蓝牙设备的名称? [英] Android: How to find out name of the bluetooth device connected?

查看:28
本文介绍了Android:如何找出连接的蓝牙设备的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我在这里尝试两件事,当我的蓝牙设备连接到特定设备时,我尝试开始敬酒(因此需要检查这是否是特定的蓝牙名称),如果那是特定的设备,那么我想连接到该特定蓝牙设备时显示祝酒词.我还想在我的蓝牙与该特定蓝牙设备断开连接时展示敬酒.这是我的代码:在 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>  

班级代码:

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

在我的代码中,我让接收器正确地调用了 toast,甚至断开连接的 toast 也可以正常工作,但连接的 toast 永远不起作用.

In my code im getting receiver called toast properly and even the toast for disconnected is also working but toast of connected never works.

请告诉我为什么 CONNECTED toast 不起作用以及如何在连接到特定设备时使此代码起作用(我不想为所有设备显示此 toast).

Please let me know why CONNECTED toast doesn't work and how to make this code work when connected to a particular device ( I don't want to show this toast for all the devices ).

推荐答案

将您的广播接收器更改为:

Change your broadcast receiver to:

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

        }
    }
 };

这篇关于Android:如何找出连接的蓝牙设备的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆