轻松搞定Android蓝牙4.0(Bluetooth Low Energy)

基础知识参考:Android 蓝牙4.0(1)

Android 蓝牙3大操作

  • 扫描蓝牙设备
  • 连接蓝牙设备
  • 蓝牙蓝牙通信

一、扫描蓝牙设备

蓝牙扫描Android4.x方式:startLeScan (LeScanCallback)
蓝牙扫描Android5.x方式:startScan (ScanCallback callback)
简化扫描操作:像调用View.OnClickListener一样简单

    mBleScanner = new BleScanner(activity, new SimpleScanCallback() {
        @Override
        public void onBleScan(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
           //device :设备信息 mac、name
           //rssi :可以认为是蓝牙信号值
           //scanRecord :广播报文
           doSomething();
        }

        @Override
        public void onBleScanFailed(BleScanState scanState) {
           //scanState 扫描错误码
        }
    });
    //开始扫描(一直扫描)
    mBleScanner.startBleScan();
    //开始扫描并设置扫描时长(超时回调onBleScanFailed())
    mBleScanner.onStartBleScan(long timeoutMillis);
    //停止扫描
    mBleScanneronStopBleScan();

二、连接蓝牙设备

1.扫描设备成功后,bluetoothDevice可以直接连接设备(如果不需要后续操作的话)
  BluetoothDevice.connectGatt(Context context, boolean autoConnect, new BluetoothGattCallback() {
      @Override
      public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
          BleLog.i(TAG, "onConnectionStateChange: State = " + BleUtils.getBleConnectStatus(status)
                  + " newState = " + BleUtils.getBleConnectStatus(newState));

          if (newState == BluetoothProfile.STATE_CONNECTED) {
              //连接成功
          } else {
              //连接成功失败

          }
      }
  });
2.扫描设备成功后,连接设备需要后续的操作,通过下面方式)
  mBleHelper = new AppBluetoothHelper(Context context, BleCallback bleCallback);
  mBleHelper.bindService(new AppBluetoothHelper.OnBindListener() {
      @Override
      public void onServiceConnected() {
          connectDevice(String deviceMac);
      }
  });
  public void connectDevice(final String deviceMac) {
    mBleHelper.connectDevice(deviceMac, new ConnectCallback() {
        @Override
        public void onConnectSuccess() {
            mBleHelper.mConnCallback = null;//不再需要回调了
            
        }

        @Override
        public void onConnectFailed(ConnectError error) {

        }
    });
  }

三、蓝牙设备通信

依然像调用View.OnClickListener一样简单,发送消息之后响应回调回来
目前只实现了常用的三个操作

    //写~
    mBleHelper.writeCharacteristic(UUID serviceUUID, UUID CharacteristicUUID, byte[] values)
    //开关notify
    mBleHelper.updateCharacteristicNotification(UUID serviceUUID, UUID characteristicUUID, UUID descriptorUUID, boolean enable)
    //读取特征值
    mBleHelper.readFromCharacteristic(UUID serviceUUID, UUID CharacteristicUUID) 
    //bleCallback中接收蓝牙响应~可以通过uuid来区分消息
    BluetoothHelper(Context context, BleCallback bleCallback)
    //Callback triggered as a result of a remote characteristic notification.
    //BluetoothGattCallback#onCharacteristicChanged --> onCharacteristicNotification
    public void onCharacteristicNotification(UUID uuid, byte[] data) {

    }

    //Callback reporting the result of a characteristic read operation.
    //BluetoothGattCallback#onCharacteristicChanged
    public void onCharacteristicRead(UUID uuid, byte[] data) {

    }

    //Callback indicating the result of a characteristic write operation.
    //BluetoothGattCallback#onCharacteristicWrite
    public void onCharacteristicWrite(UUID uuid, int status) {

    }

搞定收工

GitHub地址:https://github.com/captain-miao/bleYan
apk下载地址:http://fir.im/bleYan

轻松搞定Android蓝牙4.0(Bluetooth Low Energy)》有3个想法

  1. 我的程序在链接的时候 其他设备还能链接上,但是唯独蓝牙打印机一直是返回133 我也试过用gatt.close()但是没用
    测试机型包括: 三星盖世note5 , 小米 MIX2,华为,nexus

发表评论

电子邮件地址不会被公开。 必填项已用*标注