首页 > 解决方案 > 如何不丢失连接BLE

问题描述

我正在尝试在 TI 设备和我的应用程序之间进行 BLE 通信。此时,不启用配对,一切正常,一旦启用配对,我就有问题。

1)我的应用程序要求我配对。
2) 使用密码进入
3) APP 不工作
4) 我离开APP,返回,它工作
5) 我从APP 重新退出,重新进入不工作,需要配对。

如何解决这些错误?

我在 MainActivity 中使用它:

private void getpaireddevices(){
    Set<BluetoothDevice> devicesArray = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    if(devicesArray.size() > 0) {
        for(BluetoothDevice device : devicesArray) {
            device.getName();
            device.getAddress();
        }
    }
}

这在 SelectedDevice 中:

private final BroadcastReceiver mBroadcastReceiver4 = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
        int prevstate = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);

        String msg = "Bond state change: state" + pairstate(state) + "previous state" + pairstate(prevstate);
        Log.w("Bond state receiver", msg);
    }

    private String pairstate(int state) {

        switch (state) {
            case BluetoothDevice.BOND_BONDING:
                Log.i("Bondind status:", "Bonding..");
                break;
            case BluetoothDevice.BOND_BONDED:
                Log.i("Bondind status:", "Bonded");
                break;
            case BluetoothDevice.BOND_NONE:
                Log.i("Bondind status:", "Fail");
            default:
                return String.valueOf(state);

        }
        return null;
    }
};

在扫描设备时的主要活动中,我调用了createBond ()SelectedDevice 中的mBroadcastReceiver4.

标签: androidbluetooth-lowenergy

解决方案


与 BLE 设备交互的第一步是连接到它——更具体地说,连接到设备上的 GATT 服务器。

bluetoothGatt = device.connectGatt(context, false, gattCallback);


private final BluetoothGattCallback gattCallback =
            new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status,
                int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                connectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" +
                        bluetoothGatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                connectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            } else {
                Log.w(TAG, "onConnectionStateChange() status = " + status + " newState = " + newState);
            }
        }

    @Override
    // New services discovered
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    // Result of a characteristic read operation
    public void onCharacteristicRead(BluetoothGatt gatt,
            BluetoothGattCharacteristic characteristic,
            int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }
}

当您尝试连接到您的设备时,您在 onConnectionStateChange() 中看到了什么日志?

您是否在您的设备上发现了任何服务?

有关连接到 BLE 设备的更多信息: https ://developer.android.com/guide/topics/connectivity/bluetooth-le.html


推荐阅读