低功耗蓝牙(学习笔记)

清单文件写入蓝牙低功耗蓝牙权限:
<uses-feature
        android:name="android.hardware.bluetooth_le"
        android:required="true" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 
***************************************************************************************************************************
 
 获取蓝牙实例:
 BluetoothManager mBluetoothManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);  //开启使用蓝牙服务
 BluetoothAdapter mBluetoothAdapter= mBluetoothManager.getAdapter();   //获取蓝牙适配器
 
 
***************************************************************************************************************************
 
 扫描连接蓝牙需要通过蓝牙适配器
 
 扫描蓝牙:
 mBluetoothAdapter.startLeScan(mLeScanCallback); // 开启扫描开关
 mBluetoothAdapter.stopLeScan(mLeScanCallback);   //关闭
 
 当开启扫描后,需等待一段时间扫描附近设备,此时扫描到了则调用扫描回调方法:
 /* 扫描回调5.0以前 */
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {     //返回device中存储name和mac
            runOnUiThread(new Runnable() {
                public void run() {
                        mBLEDeviceList = device;      //将扫描到的设备信息传出方法,mBLEDeviceList和device同一类型
                }
            });
        }
    };
    

***************************************************************************************************************************
    
连接蓝牙:
mBluetoothAdapter.stopLeScan(mLeScanCallback);   //关闭蓝牙扫描
mBLE.connect(mBLEDeviceList.getAddress());    //传入蓝牙mac以连接到蓝牙


***************************************************************************************************************************

此时需要了解客户端和硬件数据交互规则:
客户端发送有效指令----->设备接收处理指令
设备根据指令反馈数据----->客户端接收数据转码

***************************************************************************************************************************

此时需要了解操作低功耗蓝牙的规则:
GATT服务,全称BluetoothGattService(蓝牙Gatt协定服务),当通过mac连接到ble后还不能与硬件数据交互,由GATT服务管理着硬件数据交互
特征值,全程BluetoothGattCharacteristic(蓝牙Gatt协定的特点),特征值就是蓝牙提供给外设进行数据交互的接口.通过写特征值,
就可以把数据传给对方,通过读特征值就能读到对方的数据

gatt下可有多个特征值

获取GATT服务和特征值需通过调用方法传入UUID获取,UUID即通用唯一识别码,用于确保唯一性
UUID_SERVICE = "0000fff0-0000-1000-8000-00805f9b34fb";  //gatt服务
UUID_CHAR6 = "0000fff6-0000-1000-8000-00805f9b34fb";    //数据描述符
获取gatt:BluetoothGattService gatt.getService(UUID.fromString(UUID_SERVICE));
获取数据描述符:BluetoothGattCharacteristic gattCharacteristic_char6 = mService.getCharacteristic(UUID.fromString(UUID_CHAR6));

***************************************************************************************************************************
发送数据:
向设备发送数据前需要打开蓝牙设备可写入开关(操控获得的数据描述符)

此方法当识别到连接蓝牙后立即回调
需提前在oncreate中加载:
BluetoothLeClass mBLE = new BluetoothLeClass(this);
mBLE.setOnServiceDiscoverListener(mOnServiceDiscover);

private BluetoothLeClass.OnServiceDiscoverListener mOnServiceDiscover = new OnServiceDiscoverListener() {
        public void onServiceDiscover(BluetoothGatt gatt) {
            mService = gatt.getService(UUID.fromString(UUID_SERVICE));
            gattCharacteristic_char6 = mService.getChar
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值