Giter Site home page Giter Site logo

fastbletool's Introduction

FastBleTool

基于FastBle开发的蓝牙快速开发框架。

使用简单的方式进行搜索、连接、读写、通知的订阅与取消等一系列蓝牙操作,并实时地得到操作反馈。

更新日志

V1.0.2:修改BleManager为单例模式 添加扫描时间设置

集成依赖

compile 'com.qyh.fastble:fastbletool:1.0.2'

一. 申请权限

扫描BLE设备需要动态获取以下权限

Manifest.permission.ACCESS_FINE_LOCATION

二. 初始化操作

1. 在Application初始化BleManager

BleManager.init(this);

2. 启动并绑定service

第一次执行需要启动并绑定service

BleManager.getInstance().startAndBindService(this, mFhrSCon);

如果service已经启动过了, 且界面需要获取数据, 只需绑定service

BleManager.getInstance().bindService(this, mFhrSCon);

3. 绑定service的回调操作

  • 获取service对象
  • 添加回调, 与BLE的交互结果都会通过回调返回
/**
 * service绑定回调
 */
private ServiceConnection mFhrSCon = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mBluetoothService = ((BluetoothLeService.BluetoothBinder) service).getService();
        mBluetoothService.addCallBack(serviceCallBack);
    }
    @Override
    public void onServiceDisconnected(ComponentName name) {
        mBluetoothService = null;
    }
};

4. 回调数据

/**
 * 连接回调
 */
private BleServiceCallBack serviceCallBack = new BleServiceCallBack() {

    @Override
    public void onStartScan() {
        // 开始扫描
    }

    @Override
    public void onScanError() {
        // 扫描报错
    }

    @Override
    public void onLeScan(BleDevice device) {
        // 扫描到设备时回调
    }

    @Override
    public void onScanComplete(BleDevice[] results) {
        // 扫描结束
    }

    @Override
    public void onConnecting() {
        // 正在链接
    }

    @Override
    public void onConnectFail(BleException exception) {
        // 连接失败
    }

    @Override
    public void onConnectSuccess() {
        // 连接成功
    }

    @Override
    public void onDisConnected(final BluetoothDevice device) {
        Toast.makeText(TextActivity.this, "连接断开", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onChanged(BluetoothGattCharacteristic characteristic) {
        // 接收设备刷新返回的数据
    }

    @Override
    public void onHRMNotify(BluetoothGattCharacteristic characteristic) {
        // 接收心率设备返回数据
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        // 服务被发现, 可以与设备进行交互
    }
};

三. BLE操作

1. 扫描BLE设备

// 默认扫描5000毫秒
mBluetoothService.scanDevice();
// 自定义扫描时间
mBluetoothService.scanDevice(long timeoutMillis);

2. 取消扫描

mBluetoothService.cancelScan();

3. 连接设备

根据设备进行连接, 可选断开后是否自动连接

/**
 * 连接设备
 *
 * @param scanResult    设备
 * @param isAutoConnect 连接断开后, 是否自动连接
 * @param delay         重连间隔(毫秒)
 */
public void connectDevice(BleDevice scanResult, booleanisAutoConnect, long delay)

根据设备MAC地址进行连接, 可选断开后是否自动连接

/**
 * 连接设备
 *
 * @param address       设备MAC地址
 * @param isAutoConnect 连接断开后, 是否自动连接
 * @param delay         重连间隔(毫秒)
 */
public void connectDevice(String address, booleanisAutoConnect, long delay)

4. 断开设备连接

mBluetoothService.closeConnect();

四. 释放资源

1. 退出Activity

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mBluetoothService != null) {
        // 取消绑定
        bleManager.unBindService(this, mFhrSCon);
        // 移除回调
        mBluetoothService.removeCallBack(serviceCallBack);
    }
}

2. 退出程序

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mBluetoothService != null) {
        // 取消绑定
        bleManager.unBindService(this, mFhrSCon);
        // 停止service
        bleManager.stopService(this);
        // 断开设备连接
        mBluetoothService.closeConnect();
        // 移除回调
        mBluetoothService.removeCallBack(serviceCallBack);
    }
}

五. 使用示例

public class TextActivity extends AppCompatActivity {

    private TextView tvHeart;
    private BleManager bleManager;
    private BluetoothLeService mBluetoothService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        tvHeart = (TextView) findViewById(R.id.tv_heart);


        bleManager = new BleManager(this);
        bleManager.bindService(this, mFhrSCon);
    }

    /**
     * service绑定回调
     */
    private ServiceConnection mFhrSCon = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBluetoothService = ((BluetoothLeService.BluetoothBinder) service).getService();
            mBluetoothService.addCallBack(serviceCallBack);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBluetoothService = null;
        }
    };

    /**
     * 连接回调
     */
    private BleServiceCallBack serviceCallBack = new BleServiceCallBack() {

        @Override
        public void onLeScan(BleDevice device) {

        }

        @Override
        public void onScanComplete(BleDevice[] results) {
        }

        @Override
        public void onChanged(BluetoothGattCharacteristic characteristic) {

        }

        @Override
        public void onHRMNotify(BluetoothGattCharacteristic characteristic) {
            byte[] value = characteristic.getValue();
            BleLog.e("==", HexUtil.bytesToHexString(value));
            tvHeart.setText(HexUtil.bytesToHexString(value));
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
	    // 只需在连接设备时调用一次
            mBluetoothService.notify(UUIDConstant.HRM_SERVICE.toString(), UUIDConstant.HRM_CHAR.toString());
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBluetoothService != null) {
            bleManager.unBindService(this, mFhrSCon);
            mBluetoothService.removeCallBack(serviceCallBack);
        }
    }
}

感谢

  1. https://github.com/Jasonchenlijian/FastBle
  2. https://github.com/litesuits/android-lite-bluetoothLE
  3. https://github.com/Alex-Jerry/BleDemo

License

   Copyright 2016 chenlijian

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

fastbletool's People

Contributors

qiu-yongheng avatar

Stargazers

 avatar mention avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.