BluetoothAdapter.getBondedDevices返回零设备虽然配对,为什么?

问题描述 投票:0回答:2

我目前正在尝试编写一个Android应用程序,它使用蓝牙连接到设备并打开串行连接。要连接到设备,我需要将设备传递给一些建立和维护连接的代码。我的问题是我似乎没有达到这一点,因为我的代码总是返回NullPointer作为设备。

我已经尝试调试代码,现在发现获取绑定设备的函数返回零设备。为了缓解这种情况,我确保蓝牙适配器已存在且已打开。我还确保在android菜单中有连接的设备,我想要建立连接的Android设备已配对。

以下是应该识别并返回设备的代码:

public class BluetoothDeviceRecognition {
BluetoothDevice blDevice(BluetoothAdapter myBluetoothAdapter) {


    Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();
    BluetoothDevice myDevice = null;
    if(pairedDevices.size() > 0){
        for (BluetoothDevice device : pairedDevices){
            if (device.getName().equals("ESP32_LED_Control")) {
                myDevice = device;

            }
        }
    }
    return myDevice;
}

这是代码,应该确保有一个蓝牙适配器,如果不是这样的话应该启用它:

    public void startBluetooth(){
    if(mBluetoothAdapter == null)
    {
        Log.d(TAG, "connectBluetooth: Does not have Bluetooth Capabilities");
        return;
    }
    if(!mBluetoothAdapter.isEnabled())
    {
        Log.d(TAG, "connectBluetooth: Enabling Bluetooth");
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableBTIntent);
    }
}

这是启动上面代码的代码:

        connect.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            try{
                startBluetooth();
                BluetoothDevice myDevice = mBluetoothDevice.blDevice(mBluetoothAdapter);

                connectBluetooth(myDevice, MY_UUID_INSECURE);
            }
            catch(NullPointerException e){
                Log.d(TAG, "connectBluetooth: Bluetooth Device cannot be Null");
                sent.setText("Error, Bluetooth Device cannot be Null");
            }


        }
    });

在第一个代码片段中的if循环开头设置断点时,我希望配对设备的大小大于0,但配对设备的实际大小为零。

编辑:

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的目标SDK应该是21。

编辑:所以我没有真正解决这个问题,而是采用了不同的方法。我没有寻找与我的设备配对的设备,而是寻找目前可在我附近发现的设备。使用此代码:https://github.com/mitchtabian/Sending-and-Receiving-Data-with-Bluetooth

java android android-bluetooth
2个回答
0
投票

在调用mBluetoothDevice.blDevice(mBluetoothAdapter)之前,必须确保蓝牙已打开。因此,您只需要使用startActivityForResult(enableBTIntent)来启用蓝牙并从onActivityResult()接收结果,一旦您收到用户打开蓝牙的结果,您就可以调用mBluetoothDevice.blDevice(mBluetoothAdapter)。


0
投票

你错过了位置许可,

正如文档中所述:https://developer.android.com/guide/topics/connectivity/bluetooth#Permissions

如果目标Sdk为23或更高,则应在运行时请求此权限

© www.soinside.com 2019 - 2024. All rights reserved.