本文主要介绍 Android 系统下 通过Ble 方式连接蓝牙的方法:
Ble 蓝牙连接原理:
Ble 连接优点:
1 : 低功耗,省电
Ble 连接缺点:
1 : 兼容性差,需要Android 4.3以上才能支持
2 : 连接不稳定
Ble 连接步骤:
1: 获取远程设备信息
BluetoothDevice device =
mBluetoothAdapter.getRemoteDevice(address);
2: 与远程设备建立一个gatt连接
device.connectGatt(mContext, false, mGattCallback);
3: 搜索设备
mBluetoothGatt.discoverServices();
4 : 初始化设别参数信息
BluetoothGattService gattService =
mBluetoothGatt.getService(UUID.fromString(UUID_SERVICE));BluetoothGattDescriptor descriptor =
characteristic.getDescriptor(UUID.fromString(UUID_Des));
5:断开连接
mBluetoothGatt.disconnect();
至此,手机与设备之间建立了一个通信连接,接下来我们可以通过回调 onCharacteristicRead 和 onCharacteristicWrite 方法来读取或写入设备信息。