繁体   English   中英

获取连接的设备地址蓝牙

[英]Get Connected device address 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;

我昨天进入了这个,在尝试了几次之后,下面有些工作。

BluetoothManager myBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = myBluetoothManager.getAdapter();
mBluetoothAdapter.enable();
Set s = mBluetoothAdapter.getBondedDevices();
Iterator it = s.iterator();
    while (it.hasNext()){
            BluetoothDevice d = (BluetoothDevice) it.next();
            System.out.println("device is "+d.getName());
            }          

现在上面只说明绑定了哪些设备不一定连接。 如果我只有一个耳机绑定(配对)然后。

int conState = mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
    System.out.println("connection state headset is "+conState);

如果我绑定了两个相同类型的设备,现在我不知道如何确定哪个已连接,哪个未连接。

connected.get(1)将返回第二个连接的设备。 你应该使用connected.get(0)

还要添加对connected.size()的检查,否则如果您没有连接的设备,它会崩溃。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM