android - 获取连接设备地址蓝牙

标签 android bluetooth android-bluetooth

我发现我们可以通过以下代码使用 BluetoothManager 获取已连接的蓝牙设备:

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

但是,我收到“无法解析符号‘GATT’”。应该怎么办?

更新:

package in.justrobotics.jrbluetoothcontrol;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.List;

public class TerminalActivity extends AppCompatActivity {

    TextView connectedDevices;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terminal);
        connectedDevices=(TextView) findViewById(R.id.connected_devices);
        BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        List<BluetoothDevice> connected = manager.getConnectedDevices(BluetoothGatt.GATT);
//        if (connected.size()>=1) {
        Log.i("Connected Devices: ", connected.get(0).toString() + "");
        connectedDevices.setText(connected.get(0).toString());
//        }
    }

}

我的应用因以下错误而崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.justrobotics.jrbluetoothcontrol/in.justrobotics.jrbluetoothcontrol.TerminalActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1514)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6221)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:411)
    at in.justrobotics.jrbluetoothcontrol.TerminalActivity.onCreate(TerminalActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:6875)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
    ... 9 more

即使我通过 BT 连接了扬声器。

最佳答案

import static android.bluetooth.BluetoothProfile.GATT;

关于android - 获取连接设备地址蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221881/

相关文章:

安卓蓝牙打印机

android - 联系信标 : Inconsistent and unreliable results when determining beacon distance

java - 在android中创建一个内部文件夹

android - 我真的不应该在 Android 中使用 px 而不是 dp 吗?

java - 如何通过蓝牙从 Android 应用程序向电脑发送键盘命令?

iPhone - 检测附近的蓝牙设备

重构 App 文件夹后 Android Studio 结构发生变化

Android:保存从相机拍摄的裁剪图像时出现问题

android - 与远程蓝牙低功耗 (BLE) 设备配对时断开连接

android - 如何将消息从另一个 Activity 传递到蓝牙 Activity