安卓读取蓝牙BLE设备信息

简介

目前,许多项目都会涉及与BLE设备进行交互的功能,接下来说一下读取BLE设备信息的具体实现流程。安卓BLE相关接口介绍详见官网链接:
https://developer.android.google.cn/guide/topics/connectivity/bluetooth-le

轮询方式代码实现

这里实现的示例代码,每60秒通过bluetoothManager查询当前是否有已连接的BLE设备,如果有,则与该设备建立GATT连接,读取设备信息,读完之后断开GATT连接。

package com.komect.gatttest2;

import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private String TAG = "BLE";
    private BluetoothGatt mBluetoothGatt;
    private BluetoothGattCharacteristic mGattCharacteristic;
    private BluetoothGattService mGattService;
    private boolean alreadyConnected = false;

    //service的UUID
    public static final UUID DEVICE_INFO_SERVICE_UUID = UUID.fromString("0000180A-0000-1000-8000-00805f9b34fb");

    //characteristic的UUID
    public static final UUID MANUFACTURER_Name_UUID = UUID.fromString("00002A29-0000-1000-8000-00805f9b34fb");
    public static final UUID SN_UUID = UUID.fromString("00002A25-0000-1000-8000-00805f9b34fb");
    public static final UUID MODEL_NAME_UUID = UUID.fromString("00002A24-0000-1000-8000-00805f9b34fb");
    public static final UUID SOFTWARE_VERSION_UUID = UUID.fromString("00002A27-0000-1000-8000-00805f9b34fb");
    public static final UUID FIRMWARE_VERSION_UUID = UUID.fromString("00002A26-0000-1000-8000-00805f9b34fb");
    public static final UUID MAC_UUID = UUID.fromString("00002A23-0000-1000-8000-00805f9b34fb");

    private final static UUID[] characteristicUUIDList = {
            MANUFACTURER_Name_UUID,
            SN_UUID,
            MODEL_NAME_UUID,
            SOFTWARE_VERSION_UUID,
            FIRMWARE_VERSION_UUID
    };

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        while(true) {
            if(alreadyConnected == false) {
                findGattDevice();
            }

            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

	//GATT连接回调接口
    BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            super.onConnectionStateChange(gatt, status, newState);
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                //GATT连接成功
                Log.i(TAG, "GATT connect success");
                alreadyConnected = true;
                if(gatt != null) {
                    gatt.discoverServices();
                }
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                //GATT连接断开
                Log.i(TAG, "GATT disconnect");
                closeGatt();
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            super.onServicesDiscovered(gatt, status);
            Log.i(TAG, "onServicesDiscovered status:" + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                //根据uuid获取指定的service
                mGattService = gatt.getService(DEVICE_INFO_SERVICE_UUID);

                if (mGattService == null) {
                    Log.i(TAG, "service is null, disconnect GATT");
                    closeGatt();
                    return;
                } else {
                    Log.i(TAG, "find service!");
                }

                //根据uuid获取指定的characteristic
                mGattCharacteristic = mGattService.getCharacteristic(characteristicUUIDList[0]);

                if (mGattCharacteristic == null) {
                    Log.i(TAG, "characteristic is null");
                    closeGatt();
                    return;
                } else {
                    //读取蓝牙特征值
                    Log.i(TAG, "start read characteristic");
                    mBluetoothGatt.readCharacteristic(mGattCharacteristic);
                }

            } else {
                Log.i(TAG, "onServicesDiscovered status false");
                closeGatt();
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                String value = new String(characteristic.getValue()).trim().replace(" ", "");
                Log.i(TAG, "read data:" + value);

                if(mGattService == null) {
                    Log.i(TAG, "service is null, disconnect GATT");
                    closeGatt();
                    return;
                }

                index ++;
                if(index < characteristicUUIDList.length) {
                    mGattCharacteristic = mGattService.getCharacteristic(characteristicUUIDList[index]);
                } else {
                    closeGatt();
                    return;
                }

                if (mGattCharacteristic == null) {
                    Log.i(TAG, "characteristic is null");
                    closeGatt();
                    return;
                } else {
                    //读取蓝牙特征值
                    Log.i(TAG, "start read characteristic");
                    mBluetoothGatt.readCharacteristic(mGattCharacteristic);
                }
            } else {
                Log.i(TAG, "onCharacteristicRead status false");
                closeGatt();
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.i(TAG, "onCharacteristicWrite:" + characteristic.getUuid().toString());
        }
    };

	//查找是否有已连接的BLE设备
    private void findGattDevice() {
        Log.i(TAG, "findGattDevice start");
        BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
        List<BluetoothDevice> connectedList = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
        if(connectedList == null) {
            Log.i(TAG, "connectedList is null");
            return;
        }
        int i = 0;
        Log.i(TAG, "connectedList size:" + connectedList.size());
        while(connectedList.size() > i) {
            BluetoothDevice bluetoothDevice = connectedList.get(i);
            if(bluetoothDevice != null) {
                Log.i(TAG, "find device:" + bluetoothDevice.getName());
                try {
                	//建立GATT连接
                    mBluetoothGatt = bluetoothDevice.connectGatt(this, false, mGattCallback);
                } catch (Exception e) {
                    Log.i(TAG, "findGattDevice e:" + e.getMessage());
                }
            }
            i++;
        }
    }

	//关闭GATT连接
    private void closeGatt() {
        Log.i(TAG, "closeGatt start");
        alreadyConnected = false;
        mGattService = null;
        mGattCharacteristic = null;
        index = 0;
        if(mBluetoothGatt != null) {
            Log.i(TAG, "closeGatt do close");
            try {
                mBluetoothGatt.disconnect();
                mBluetoothGatt.close();
            } catch (Exception e) {
                Log.i(TAG, "closeGatt e:" + e.getMessage());
            }
            mBluetoothGatt = null;
        }
    }
}

在AndroidManifest.xml中声明需要用到的权限:

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

监听广播方式代码实现

todo

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android微信小程序中读写BLE设备,您需要使用微信小程序提供的wx.createBLEConnection API连接设备,然后使用wx.writeBLECharacteristicValue和wx.readBLECharacteristicValue API来读写设备。 以下是实现步骤: 1. 初始化模块 ``` wx.openBluetoothAdapter({ success: function(res) { console.log("初始化模块成功") }, fail: function(res) { console.log("初始化模块失败") } }) ``` 2. 扫描设备 ``` wx.startBluetoothDevicesDiscovery({ success: function(res) { console.log("设备扫描成功") }, fail: function(res) { console.log("设备扫描失败") } }) ``` 3. 连接设备 ``` wx.createBLEConnection({ deviceId: deviceId, success: function(res) { console.log("连接设备成功") }, fail: function(res) { console.log("连接设备失败") } }) ``` 4. 读取设备特征值 ``` wx.readBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, success: function(res) { console.log("读取设备特征值成功") }, fail: function(res) { console.log("读取设备特征值失败") } }) ``` 5. 写入设备特征值 ``` wx.writeBLECharacteristicValue({ deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, value: value, success: function(res) { console.log("写入设备特征值成功") }, fail: function(res) { console.log("写入设备特征值失败") } }) ``` 请注意,要使用以上API,您需要在小程序的app.json中声明权限: ``` "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" }, "scope.bluetooth": { "desc": "小程序需要使用功能" } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值