android ble notify,GitHub - asd12l/FastBle: Android BLE 蓝牙快速开发框架,使用回调方式处理:scan、connect、notify、indica...

####初始化 (默认开启蓝牙)

bleManager = BleManager.getInstance();

bleManager.init(this);

扫描出周围所有蓝牙可连接设备

可获得周围蓝牙设备BluetoothDevice对象数组

bleManager.scanDevice(new ListScanCallback(TIME_OUT) {

@Override

public void onDeviceFound(BluetoothDevice[] devices) {

Log.i(TAG, "共发现" + devices.length + "台设备");

for (int i = 0; i < devices.length; i++) {

Log.i(TAG, "name:" + devices[i].getName() + "------mac:" + devices[i].getAddress());

}

bluetoothDevices = devices;

}

@Override

public void onScanTimeout() {

super.onScanTimeout();

Log.i(TAG, "Time Out");

}

});

直连某一个设备

当搜索到周围设备之后,可以选择选择某一个设备和其连接,传入的参数即为这个BluetoothDevice对象

bleManager.connectDevice(sampleDevice, new BleGattCallback() {

@Override

public void onConnectSuccess(BluetoothGatt gatt, int status) {

Log.i(TAG, "连接成功!");

gatt.discoverServices();

}

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

Log.i(TAG, "服务被发现!");

bleManager.getBluetoothState();

}

@Override

public void onConnectFailure(BleException exception) {

Log.i(TAG, "连接失败或连接中断:" + exception.toString());

bleManager.handleException(exception);

}

});

####扫描指定名称的设备、并连接

如果你确定周围有已知名称的蓝牙设备,或只需要连接指定名称的蓝牙设备,而忽略其他名称的设备,可以选择直接对指定名称进行搜索,搜索到即连接,搜索不到则回调超时接口。

bleManager.connectDevice(

DEVICE_NAME,

TIME_OUT,

new BleGattCallback() {

@Override

public void onConnectSuccess(BluetoothGatt gatt, int status) {

Log.i(TAG, "连接成功!");

gatt.discoverServices();

}

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

Log.i(TAG, "服务被发现!");

bleManager.getBluetoothState();

}

@Override

public void onConnectFailure(BleException exception) {

Log.i(TAG, "连接失败或连接中断:" + exception.toString());

bleManager.handleException(exception);

}

});

####扫描指定MAC地址的设备、并连接

如果你确定周围有已知地址的蓝牙设备,或只需要连接指定地址的蓝牙设备,而忽略其他地址的设备,可以选择直接对指定名称进行搜索,搜索到即连接,搜索不到则回调超时接口。

bleManager.connectMac(

DEVICE_MAC,

TIME_OUT,

false,

new BleGattCallback() {

@Override

public void onConnectSuccess(BluetoothGatt gatt, int status) {

Log.i(TAG, "连接成功!");

gatt.discoverServices();

}

@Override

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

Log.i(TAG, "服务被发现!");

bleManager.getBluetoothState();

}

@Override

public void onConnectFailure(BleException exception) {

Log.i(TAG, "连接失败或连接中断:" + exception.toString());

bleManager.handleException(exception);

}

});

####构造某一character的callback

BleCharacterCallback notifyCallback = new BleCharacterCallback() {

@Override

public void onSuccess(BluetoothGattCharacteristic characteristic) {

Log.d(TAG, "notifyCallback success: " + String.valueOf(HexUtil.encodeHex(characteristic.getValue())));

}

@Override

public void onFailure(BleException exception) {

bleManager.handleException(exception);

}

};

####对这个character进行notify,并添加监听回调

参数中的callback和uuid将会形成关联,一旦设备的此uuid对应的character发生数据变化,此callback将会回调结果。此callbak将会唯一存在,和uuid是一一对应的关系。

bleManager.notifyDevice(UUID_SERVICE, UUID_NOTIFY, notifyCallback);

####停止对这个character的notify

可以与stopListenCharacterCallback配合使用。

bleManager.stopNotify(UUID_SERVICE, UUID_NOTIFY);

####indicate

bleManager.indicateDevice(UUID_SERVICE, UUID_INDICATE, indicateCallback);

####停止对这个character的indicate

bleManager.stopIndicate(UUID_SERVICE, UUID_INDICATE);

####write

bleManager.writeDevice(

UUID_SERVICE,

UUID_WRITE,

HexUtil.hexStringToBytes(SAMPLE_WRITE_DATA),

writeCallback);

####read

bleManager.readDevice(

UUID_SERVICE,

UUID_READ,

readCallback);

####移除这个character上的监听回调

uuid作为参数,即不再监听这个uuid对应的character。此方法适用于移除notify、indicate、write、read对应的callback。与stopNotify、stopIndicate两者不同的是:stopListenCharacterCallback的功能仅仅是:移除回调监听;而后两者的功能是:中心设备停止对外围设备的制定character的Data变化的监听。前者是方法层面上的,后者设备交互上的。可以配合同时使用。

bleManager.stopListenCharacterCallback(UUID_NOTIFY);

获取当前连接的状态

boolean a = bleManager.isInScanning();

boolean b = bleManager.isConnectingOrConnected();

boolean c = bleManager.isConnected();

boolean d = bleManager.isServiceDiscovered();

####复位(断开此次蓝牙连接,移除所有回调)

bleManager.closeBluetoothGatt();

####判断设备是否支持ble

bleManager.isSupportBle();

####开启或关闭蓝牙

bleManager.enableBluetooth();

bleManager.disableBluetooth();

####其他

其他蓝牙操作可参考示例代码,或从BleManager这个类中开放的方法中找到。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以使用Bluetooth Low Energy(BLE)API扫描、连接和断开连接BLE设备。以下是一些基本步骤: 1. 获取 BluetoothAdapter 对象:使用 BluetoothManager 获取 BluetoothAdapter 对象。 ```java BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter(); ``` 2. 检查蓝牙是否已启用:使用 isEnabled() 方法检查蓝牙是否已启用。 ```java if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } ``` 3. 开始扫描:使用 startScan() 方法开始扫描BLE设备。可以使用 BluetoothLeScanner 对象进行扫描。 ```java BluetoothLeScanner scanner = bluetoothAdapter.getBluetoothLeScanner(); scanner.startScan(mScanCallback); ``` 4. 停止扫描:使用 stopScan() 方法停止扫描BLE设备。 ```java scanner.stopScan(mScanCallback); ``` 5. 连接设备:使用 BluetoothDevice 对象连接BLE设备。可以使用 connectGatt() 方法进行连接。 ```java BluetoothGatt mBluetoothGatt = bluetoothDevice.connectGatt(this, false, mGattCallback); ``` 6. 断开连接:使用 disconnect() 方法断开连接。 ```java mBluetoothGatt.disconnect(); ``` 7. 关闭 GATT 连接:使用 close() 方法关闭 GATT 连接。 ```java mBluetoothGatt.close(); ``` 以上是一些基本步骤来扫描、连接和断开连接BLE设备。具体实现还需要根据具体需求进行调整和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值