android indicate,GitHub - widegalaxy/FastBle: Android BLE 蓝牙开发框架,使用回调方式处理,搜索、连接、notify、indicate、读、写等...

####初始化

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这个类中开放的方法中找到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值