当前位置:   article > 正文

Android之开发BLE 详细步骤_android.bluetooth.device.action.uuid action_uuid

android.bluetooth.device.action.uuid action_uuid

开发之前我们必须知道蓝牙广播常量所代表的含义:

参考文档

  • String ACTION_ACL_CONNECTED=”android.bluetooth.device.action.ACL_CONNECTED” 与远程设备建立连接(低级别)
  • String ACTION_ACL_DISCONNECTED= “android.bluetooth.device.action.ACL_DISCONNECTED”与远程设备断开连接(低级别)
  • String ACTION_ACL_DISCONNECT_REQUESTED=”android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED”设备提出断开连接请求,即将断开连接
  • String ACTION_BOND_STATE_CHANGED= “android.bluetooth.device.action.BOND_STATE_CHANGED”远程设备连接状态发生改变
  • String ACTION_CLASS_CHANGED=”android.bluetooth.device.action.CLASS_CHANGED” 远程设备被改变
  • String ACTION_FOUND=”android.bluetooth.device.action.FOUND” 发现一个远程设备
  • String ACTION_NAME_CHANGED=”android.bluetooth.device.action.NAME_CHANGED” 远程设备名字发送改变或者第一次发现远程蓝牙设备的名称
  • String ACTION_PAIRING_REQUEST=”android.bluetooth.device.action.PAIRING_REQUEST” 广播配对请求
  • String ACTION_UUID=”android.bluetooth.device.action.UUID” 远程设备取回UUID广播

  • String ACTION_BOND_STATE_CHANGED = “android.bluetooth.device.action.BOND_STATE_CHANGED”; 获取配对状态
    int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); //当前的配对的状态
    int state = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.BOND_NONE); //前一次的配对状态
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //配对的设备信息

    • 已配对:int BOND_BONDED =12;
    • 配对中:int BOND_BONDING=11;
    • 配对未成功:int BOND_NONE =10;
  • String ACTION_CONNECTION_STATE_CHANGED = “android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED”; 配对后的连接状态
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR); //当前的连接状态
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, BluetoothAdapter.ERROR); //前一次的连接状态
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //连接的设备信息
    • 未连接 int STATE_DISCONNECTED = 0;
    • 连接中 int STATE_CONNECTING = 1;
    • 连接成功 int STATE_CONNECTED = 2;
  • 蓝牙类型常量:
    • 传统蓝牙:int DEVICE_TYPE_CLASSIC = 1;
    • 低功耗蓝牙(BLE):int DEVICE_TYPE_LE= 2;
    • 双模蓝牙:int DEVICE_TYPE_DUAL = 3;
    • 未知蓝牙:int DEVICE_TYPE_UNKNOWN =0;
  • 本地蓝牙开关状态广播: String ACTION_STATE_CHANGED = “android.bluetooth.adapter.action.STATE_CHANGED”
    得到状态值: int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,BluetoothAdapter.STATE_OFF);
    • 蓝牙关闭 int STATE_OFF = 10;
    • 蓝牙打开 int STATE_ON = 12;
    • 蓝牙正在关闭 STATE_TURNING_OFF = 13;
    • 蓝牙正在打开 int STATE_TURNING_ON = 11;
  • String DISCOVERY_START = BluetoothAdapter.ACTION_DISCOVERY_STARTED; //开始搜索
  • String END_FIND_DEVICE = BluetoothAdapter.ACTION_DISCOVERY_FINISHED; //搜索结束
  • String FIND_DEVICE = BluetoothDevice.ACTION_FOUND; //搜索到设备
  • String PAIRING_REQUEST = BluetoothDevice.ACTION_PAIRING_REQUEST; //请求配对

开发流程

开发步骤详解

第一步:在Manifests文件中注册权限

  1. <!--需要硬件支持低功耗蓝牙-->
  2. <uses-feature android:name"android.permission.BLUETOOTH_ADMIN"/>
  3. <!--蓝牙权限-->
  4. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  5. <uses-permission android:name="android.permission.BLUETOOTH"/>
  6. <!--Android 5.0以上蓝牙好需要位置权限-->
  7. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  8. <uses-permission android:name="andriod.permission.ACCESS_FINE_LOCATION"/>
  •  

第二步打开蓝牙

  1. /**
  2. * 检查BLE是否起作用
  3. */
  4. private void checkBLEFeature() {
  5. //判断是否支持蓝牙4.0
  6. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  7. Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
  8. finish();
  9. }
  10. //获取蓝牙适配器
  11. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  12. mBluetoothAdapter = bluetoothManager.getAdapter();
  13. //判断是否支持蓝牙
  14. if (mBluetoothAdapter == null) {
  15. //不支持
  16. Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
  17. finish();
  18. return;
  19. }else
  20. //打开蓝牙
  21. if (!mBluetoothAdapter.isEnabled()) {//判断是否已经打开
  22. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  23. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  24. }
  25. }
  •  

第三步:扫描BEL设备

  1. //扫描BLE设备
  2. private void scanLeDevice(final boolean enable) {
  3. if (enable) {
  4. if (mBluetoothAdapter.isEnabled()) {
  5. if (mScanning)
  6. return;
  7. mScanning = true;
  8. mLeDeviceListAdapter.clear();
  9. mHandler.postDelayed(mScanRunnable, 5000);//五秒后关闭扫描
  10. mBluetoothAdapter.startLeScan(mLeScanCallback);
  11. } else {
  12. ToastUtil.showMsg(getActivity(), R.string.scan_bt_disabled);
  13. }
  14. } else {
  15. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  16. mRefreshLayout.setRefreshing(false);
  17. mHandler.removeCallbacks(mScanRunnable);
  18. mScanning = false;
  19. }
  20. }
  21. //关闭扫描
  22. private final Runnable mScanRunnable = new Runnable() {
  23. @Override
  24. public void run() {
  25. scanLeDevice(false);
  26. }
  27. };
  •  

第四步:查看扫描后的回调

  1. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
  2. /**
  3. * 简单说一下这三个参数的含义:
  4. * @param device:识别的远程设备
  5. * @param rssi: RSSI的值作为对远程蓝牙设备的报告; 0代表没有蓝牙设备;
  6. * @param scanRecord:远程设备提供的配对号(公告)
  7. */
  8. @Override
  9. public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
  10. getActivity().runOnUiThread(new Runnable() {
  11. @Override
  12. public void run() {
  13. //保存到本地:用来展示扫描得到的内容
  14. mLeDeviceListAdapter
  15. .addDevice(new LeDevice(device.getName(), device.getAddress(), rssi, scanRecord));
  16. mLeDeviceListAdapter.notifyDataSetChanged();
  17. }
  18. });
  19. }
  20. };
  •  

第五步:连接设备
BluetoothGatt常规用到的几个操作示例:
* connect() :连接远程设备。
* discoverServices() : 搜索连接设备所支持的service。
* disconnect():断开与远程设备的GATT连接。
* close():关闭GATTClient端。
* readCharacteristic(characteristic) :读取指定的characteristic。
* setCharacteristicNotification(characteristic, enabled):设置当指定characteristic值变化时,发出通知。
* getServices() :获取远程设备所支持的services。

  1. //获取BluetoothDevice对象后调用coonnectGatt()进行连接
  2. mBluetoothDevice.connectGatt(context,false,mGattCallbask);//第一个参数:上下文,第二个参数:断开连接是否重连,第三个
  3. //mBluetoothGatt.discoverServices();//此步骤根据业务需求添加
  4. 参数连接状态回调。
  5. private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
  6. //连接状态改变的回调
  7. @Override
  8. public void onConnectionStateChange(BluetoothGatt gatt, int status,
  9. int newState) {
  10. if (newState == BluetoothProfile.STATE_CONNECTED) {
  11. // 连接成功后启动服务发现
  12. Log.e("AAAAAAAA", "启动服务发现:" + mBluetoothGatt.discoverServices());
  13. }
  14. };
  15. //发现服务的回调
  16. public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  17. if (status == BluetoothGatt.GATT_SUCCESS) {
  18. Log.e(TAG, "成功发现服务");
  19. }else{
  20. Log.e(TAG, "服务发现失败,错误码为:" + status);
  21. }
  22. };
  23. //写操作的回调
  24. public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
  25. if (status == BluetoothGatt.GATT_SUCCESS) {
  26. Log.e(TAG, "写入成功" +characteristic.getValue());
  27. }
  28. };
  29. //读操作的回调
  30. public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
  31. if (status == BluetoothGatt.GATT_SUCCESS) {
  32. Log.e(TAG, "读取成功" +characteristic.getValue());
  33. }
  34. }
  35. //数据返回的回调(此处接收BLE设备返回数据)
  36. public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
  37. };
  38. };
  39. }
  •  

第六步:发送数据()回调也是BluetoothGattCallback
* 读操作,读操作比较简单,只需将相应的特征值传入即可得到该特征值下的数据,如下代码:

mBluetoothGatt.readCharacteristic(characteristic);
读取的结果通过onCharacteristicRead回调返回:(通过characteristic.getValue()就可以得到读取到的值了)
// 读操作的回调
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "读取成功" +characteristic.getValue());
}
}


* 写操作,写操作是我们的重点,我们可以通过向characteristic写入指令(发送指令)以此来达到控制BLE终端设备的目的:

mBluetoothGatt.setCharacteristicNotification(characteristic,true)//设置该特征具有Notification功能
//将指令放置进特征中
characteristic.setValue(new byte[] {数据});
//设置回复形式
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
//开始写数据
mBluetoothGatt.writeCharacteristic(chharacteristic);


* 注:
BLE传输过程每次最大只能传输20个字节,所以如果发送的指令大于20字节的话要分包发送,例如现在要发送28个字节的,可以先write(前20个字节),开启线程sleep(几十毫秒)后在write(后面8个字节)。
第七步:关闭蓝牙

BluetoothAdapter. disable();//关闭蓝牙
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/192150
推荐阅读
相关标签
  

闽ICP备14008679号