Android 8.1连接到调用connectGatt的BLE外设。回调总是说断开连接。从不连接

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

我是Android编程的新手,并试图编写一个适用于BLE外设的应用程序。我有一个我写的iOS应用程序,它做同样的事情,我的iOS应用程序已经按照我想要的方式工作。

我写了一个活动来扫描我的外围设备列表。到目前为止,这似乎工作正常。

然后我选择一个外设并将BluetoothDevice对象传递给“设备详细信息”活动,该活动应该连接到外围设备然后用它做一些事情。

在我的设备详细信息活动中,我获取对连接按钮的引用,然后添加一个单击侦听器。 Click侦听器创建MyBluetoothGattCallback的实例,然后将其用作调用connectGatt()的参数。此调用确实返回看起来像有效BluetoothGatt对象的内容。 MyBluetoothGattCallback被调用,但只调用一次,状态为133,状态为0.这似乎是BluetoothProfile.STATE_DISCONNECTED。永远不会再次调用回调。每次按下连接按钮都会发生这种情况。

我已经确保只进行了一次扫描操作,并且它已从应用程序的主要活动中停止。所以此时扫描不会发生。

我的问题是,为了使连接正常工作,我应该做些什么?

public class DeviceDetails extends AppCompatActivity {

public BluetoothDevice mBluetoothDevice;
public BluetoothSocket mBluetoothSocket;
public BGXBluetoothGattCallback mGattCallback;
public BluetoothGatt mBluetoothGatt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_device_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TextView titleTextView = findViewById(R.id.DeviceDetailsTitleTextView);

    mBluetoothDevice = (BluetoothDevice) getIntent().getExtras().getParcelable("BLUETOOTH_DEVICE");
    String sdeviceName = mBluetoothDevice.getName();
    if (null == sdeviceName) {
        sdeviceName = "No device name";
    }

    titleTextView.setText(sdeviceName);

    Button connectButton = findViewById(R.id.connectButton);
    connectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("debug", "Connect button pressed");
            if (null == mGattCallback) {
                mGattCallback = new MyBluetoothGattCallback();
            }

            mBluetoothGatt = mBluetoothDevice.connectGatt(DeviceDetails.this, false, mGattCallback);

        }
    });
}

这是我的BluetoothGattCallback子类:

public class MyBluetoothGattCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
    super.onConnectionStateChange(gatt,status,newState);

    Log.d("debug", "onConnectionStateChange status: " + status + " newState: "+newState);

    switch(newState) {
        case BluetoothProfile.STATE_CONNECTING:
            Log.d("debug", "connection state: CONNECTING.");
            break;
        case BluetoothProfile.STATE_CONNECTED:
            Log.d("debug", "connection state: CONNECTED.");
            break;
        case BluetoothProfile.STATE_DISCONNECTING:
            Log.d("debug", "connection state: DISCONNECTING.");
            break;
        case BluetoothProfile.STATE_DISCONNECTED:
            Log.d("debug", "connection state: DISCONNECTED.");
            break;
        default:
            Log.d("debug", "connection state: OTHER.");
            break;
    }
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);
    Log.d("debug", "onServicesDiscovered.");
    }
}
android bluetooth-lowenergy android-8.1-oreo
1个回答
0
投票

事实证明,导致结果的代码没有任何问题。我清除了嵌入式设备的绑定信息,重新启动了手机,然后连接,发现了服务等。

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