8000 GitHub - qiu-yongheng/Bluetooth_BLE: 一个蓝牙BLE的操作库
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

qiu-yongheng/Bluetooth_BLE

Repository files navigation

Bluetooth_BLE


一个蓝牙BLE的操作库

1. import Module -> blelib包到项目中

2. 在build.gradle中添加

dependencies {
    compile project(':blelib')
}

使用


初始化BLE管理类

bleManager = BleManager.getInstance(this);

扫描设备

bleManager.scanDevice(new BleScanCallback(设置扫描时间) {
    @Override
    public void onDeviceFound(List<BluetoothDevice> deviceList) {
          //返回搜索到的设备
    }
});

连接设备

bleManager.connectDevice(mBlueDevices.get(0), false, new BleGattCallback(handler) {
    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        //向蓝牙设备写入描述符, 请求获取数据
        bleManager.writeDevice(gatt, UUIDCons.s1, UUIDCons.c1, UUIDCons.d1, null);
        //读取设备返回的数据, 数据通过handler返回
        bleManager.readDevice(gatt, UUIDCons.s1, UUIDCons.c1);
    }

    @Override
    public void onConnectSuccess(BluetoothGatt gatt, int status) {
        //搜索service, 成功后回调onServicesDiscovered
        gatt.discoverServices();
    }

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

    }
});

获取返回的数据

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0x2:
                byte[] datas = (byte[]) msg.obj;
                mTv_hearbeat.setText("当前心率: " + datas[1]);
                break;
        }
    }
};

About

一个蓝牙BLE的操作库

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0