首页 > 解决方案 > 连接到 BLE 设备

问题描述

所以我制作了这个应用程序,我可以在其中找到所有带有名称的 BLE 设备。但是我怎样才能使特定字段之一,可点击并自动连接到设备,以便我可以开始写入/读取呢?

适配器

public class ListAdapter_BTLE_Devices extends ArrayAdapter<BTLE_Device> {

    Activity activity;
    int layoutResourceID;
    ArrayList<BTLE_Device> devices;

    public ListAdapter_BTLE_Devices(Activity activity, int resource, ArrayList<BTLE_Device> objects) {
        super(activity.getApplicationContext(), resource, objects);
        this.activity = activity;
        layoutResourceID = resource;
        devices = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(layoutResourceID, parent, false);
        }

        BTLE_Device device = devices.get(position);
        String name = device.getName();
        String address = device.getAddress();
        int rssi = device.getRSSI();

        TextView BLE_name = (TextView) convertView.findViewById(R.id.BLE_name);
        if (name != null && name.length() > 0) {
            BLE_name.setText(device.getName());
        }
        else {
            BLE_name.setText("No Name");
        }

        TextView BLE_rssi = (TextView) convertView.findViewById(R.id.BLE_rssi);
        BLE_rssi.setText("RSSI: " + Integer.toString(rssi));

        TextView BLE_macaddr = (TextView) convertView.findViewById(R.id.BLE_macaddr);
        if (address != null && address.length() > 0) {
            BLE_macaddr.setText("MAC-addr: "+device.getAddress());
        }
        else {
            BLE_macaddr.setText("No Address");
        }

        return convertView;
    }
}

编辑

我想我现在可能已连接到 GATT,所以我所做的是.. 首先我从Mainactivity获取 MAC-addr ,然后我将它保存在一个意图中,并在单击时开始另一个活动。在这里我做了以下 DeviceAddress = intent.getStringExtra(MainActivity.EXTRAS_BLE_ADDRESS);

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(DeviceAddress);

device.connectGatt(this, false, mGattCallback);

当我调用 connectGatt 时,它会打印消息Log.d(TAG, "Connection State: 1");,这是正确的方法吗?


private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.d(TAG, "Connection State Change: "+status+" -> "+connectionState(newState));
            if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
                /*
                 * Once successfully connected, we must next discover all the services on the
                 * device before we can read and write their characteristics.
                 */
                Log.d(TAG, "Connection State: 1");
                gatt.discoverServices();
            } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
                /*
                 * If at any point we disconnect, send a message to clear the weather values
                 * out of the UI
                 */
                Log.d(TAG, "Connection State: 2");
            } else if (status != BluetoothGatt.GATT_SUCCESS) {
                /*
                 * If there is a failure at any stage, simply disconnect
                 */
                Log.d(TAG, "Connection State: 3");
                gatt.disconnect();
            }
        }

标签: androidandroid-studiobluetooth-lowenergy

解决方案


要首先连接到设备,您必须执行 BLE 扫描,该扫描(如果您使用启动代码)运行回调并将其添加到找到的设备列表中。

添加过滤器以仅允许您正在寻找的设置设备。当 BLE 发布一个最多 31 个字节的数据包时,您应该在此处有一些数据来识别您的设备,例如制造商 ID 或数据等。或者,如果您正在处理一个简单的项目,您可以在设备地址中以编程方式进行硬编码。

然后,当从扫描中发现此设备时,您可以停止 BLE 扫描并自动排队连接请求。这将要求发出 GATT 请求,因此授予您访问 GATT 服务的权限,从而访问设备上的特性。


推荐阅读