Code Monkey home page Code Monkey logo

fastble's Introduction

FastBle

Android BLE 蓝牙开发框架,使用回调方式处理搜索、连接、notify、indicate、读、写等一系列蓝牙操作。 传参,然后回调结果,就是这么简单。


Usage

  • ####初始化

      bleManager = BleManager.getInstance();
    
      bleManager.init(this);
    
  • ####扫描指定名称设备、并连接

      bleManager.connectDevice(
              DEVICE_NAME,
              TIME_OUT,
              new BleManagerConnectCallback() {
                  @Override
                  public void onConnectSuccess(BluetoothGatt gatt, int status) {
                      Log.i(TAG, "连接成功!");
                  }
    
                  @Override
                  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                      Log.i(TAG, "服务被发现!");
                  }
    
                  @Override
                  public void onConnectFailure(BleException exception) {
                      Log.i(TAG, "连接失败或连接中断:" + '\n' + exception.toString());
                  }
              });
    
  • ####notify

      bleManager.notifyDevice(
              UUID_SERVICE_LISTEN,
              UUID_LISTEN_NOTIFY,
              UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR,
              new BleManagerNotifyCallback() {
                  @Override
                  public void onNotifyDataChangeSuccess(BluetoothGattCharacteristic characteristic) {
                      Log.d(TAG, "特征值Notification通知数据回调: "
                              + '\n' + Arrays.toString(characteristic.getValue())
                              + '\n' + HexUtil.encodeHexStr(characteristic.getValue()));
                  }
    
                  @Override
                  public void onNotifyDataChangeFailure(BleException exception) {
                       Log.e(TAG, "特征值Notification通知回调失败: " + '\n' + exception.toString());
                  }
    
              });
    
  • ####indicate

      bleManager.indicateDevice(
              UUID_SERVICE_LISTEN,
              UUID_LISTEN_INDICATE,
              UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR,
              new BleManagerIndicateCallback() {
                  @Override
                  public void onIndicateDataChangeSuccess(BluetoothGattCharacteristic characteristic) {
                      Log.d(TAG, "特征值Indication通知数据回调: "
                              + '\n' + Arrays.toString(characteristic.getValue())
                              + '\n' + HexUtil.encodeHexStr(characteristic.getValue()));
                  }
    
                  @Override
                  public void onIndicateDataChangeFailure(BleException exception) {
                      Log.e(TAG, "特征值Indication通知回调失败: " + '\n' + exception.toString());
                  }
              });
    
  • ####写指令

      bleManager.writeDevice(
              UUID_SERVICE_OPERATE,
              UUID_OPERATE_WRITE,
              UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR,
              HexUtil.hexStringToBytes(SAMPLE_WRITE_DATA),
              new BleManagerWriteCallback() {
                 @Override
                 public void onDataWriteSuccess(BluetoothGattCharacteristic characteristic) {
                      Log.d(TAG, "写特征值成功: "
                              + '\n' + Arrays.toString(characteristic.getValue())
                              + '\n' + HexUtil.encodeHexStr(characteristic.getValue()));
                 }
    
                 @Override
                 public void onDataWriteFailure(BleException exception) {
                      Log.e(TAG, "写读特征值失败: " + '\n' + exception.toString());
                 }
              });
    
  • ####关闭操作

      bleManager.closeBluetoothGatt();
    
  • ####其他 其他蓝牙操作可参考示例代码,或从BleManager这个类中开放的方法中找到。

fastble's People

Contributors

jasonchenlijian avatar

Watchers

 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.