Android蓝牙通讯(1)——Blelib开源库的使用

学习自: 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发详解


前言

项目需要添加一个蓝牙设备并实现监听,所以记录一下学习的过程并记录一下自己遇到过的坑吧。首先,用BLE的话我们就先学习一下使用BleLib。


一、BleLib是什么?

BleLib是Android低功耗蓝牙4.0及以上开发的辅助库,一行代码解决Ble初始化、扫描、连接、特性读写、设置通知等操作。

二、使用步骤

1.添加BleLib库依赖

首先:添加依赖需要去build.gradle文件中添加如下代码:

dependencies {
    compile 'com.junkchen.blelib:blelib:1.0.4'
}

然后注意到,项目的gradle里面是有两个build.gradle文件的。
此处注意应选择括号里标注Module的那个。

2.绑定BleLib服务

还不是很明白绑定服务具体指的是什么意思,反正现照葫芦画瓢加上去吧。后续再好好学习一下服务具体是个啥吧。留下了没文化的眼泪

private BleService mBleService;
private boolean mIsBind;
private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mBleService = ((BleService.LocalBinder) service).getService();
        if (mBleService.initialize()) {
            if (mBleService.enableBluetooth(true)) {
                mBleService.scanLeDevice(true);
                Toast.makeText(BleScanActivity.this, "Bluetooth was opened", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(BleScanActivity.this, "not support Bluetooth", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mBleService = null;
        mIsBind = false;
    }
};

private void doBindService() {
   Intent serviceIntent = new Intent(this, BleService.class);
   bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}

private void doUnBindService() {
   if (mIsBind) {
       unbindService(serviceConnection);
       mBleService = null;
       mIsBind = false;
   }
}

3.初始化操作

当服务绑定后可进行初始化操作,判断该机是否支持蓝牙,调用如下方法:

    mBleService.initialize();//Ble初始化操作  

该方法会返回一个boolean值,返回true表示初始化成功,支持蓝牙;返回false表示初始化操作失败,则后续的所有操作都不能进行。


总结

先搞这么多,后面没太想明白怎么接收信息。。。换一个思路搞一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
BluetoothKit是一款功能强大的Android蓝牙通信框架,支持经典蓝牙和低功耗蓝牙设备混合扫描,提供了一系列简单易用的接口用于低功耗蓝牙设备的连接,数据读写,通知等。 特点 一、支持经典蓝牙和BLE蓝牙混合扫描,支持自定义扫描策略  作为智能家居平台,接入的设备包括经典蓝牙和BLE,因此扫描设备时需要两者混合进行,而设备扫描场景不同,扫描策略也会不一样,因此需要支持扫描策略可配置。 二、充分解决了Android中BLE兼容性和稳定性问题  Android系统对蓝牙4.0支持得并不完善,有许多bug, BluetoothKit很好地解决了其中大部分已知的问题。  三、简单易用,接口简洁明了  BluetoothKit采用异步串行化策略处理所有设备操作,并支持任务超时及出错重试。  技术 一、实现了一个完整的跨进程异步任务队列,支持任务超时、出错重试及防御队列溢出 二、拦截并Hook系统层蓝牙Binder,实现对所有蓝牙设备通信的监控,当同时连接设备数过多时会自动断掉活跃度最低的设备 三、整个框架封装在一个service中,可灵活指定service所在进程。通过client与service通信,client可源于多个不同进程,因此适用于多进程架构的app 四、屏蔽了接口异步回调可能持有调用端Activity引用导致的内存泄露 五、利用动态代理自动将所有操作封闭在工作线程,所以整个框架无锁 使用 // 首先,需要按如下方式初始化BluetoothClient: BluetoothClient mClient = BluetoothClient.getInstance(context); // 扫描设备:支持经典蓝牙和BLE设备混合扫描,可自由定制扫描策略如下: SearchRequest request = new SearchRequest.Builder() .searchBluetoothLeDevice(3000, 3) .searchBluetoothClassicDevice(5000) .searchBluetoothLeDevice(2000) .build(); mClient.search(request, new SearchResponse() { @Override public void onSearchStarted() { } @Override public void onDeviceFounded(SearchResult device) { } @Override public void onSearchStopped() { } @Override public void onSearchCanceled() { } }); // 停止蓝牙扫描 mClient.stopSearch(); // BLE设备连接 mClient.connect(MAC, new BleConnectResponse() { @Override public void onResponse(int code, Bundle data) { if (code == REQUEST_SUCCESS) { } } }); // BLE设备断开连接 mClient.disconnect(MAC); // 读取BLE设备 mClient.read(MAC, serviceUUID, characterUUID, new BleReadResponse() { @Override public void onResponse(int code, byte[] data) { if (code == REQUEST_SUCCESS) { } } }); // 写BLE设备 mClient.write(MAC, serviceUUID, characterUUID, bytes, new BleWriteResponse() { @Override public void onResponse(int code) { if (code == REQUEST_SUCCESS) { } } }); // 打开设备通知 mClient.notify(MAC, serviceUUID, characterUUID, new BleNotifyResponse() { @Override public void onNotify(UUID service, UUID character, byte[] value) { } @Override public void onResponse(int code) { if (code == REQUEST_SUCCESS) { } } }); // 关闭设备通知 mClient.unnotify(MAC, serviceUUID, characterUUID, new BleUnnotifyResponse() { @Override public void onResponse(int code) { if (code == REQUEST_SUCCESS) { } } }); // 读取rssi mClient.readRssi(MAC, new BleReadRssiResponse() { @Override public void onResponse(int code, Integer rssi) { if (code == REQUEST_SUCCESS) { } } }); 标签:BluetoothKit
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值