网站域名交易,国外免费域名申请,网站排名优化推广厦门,新增域名网站建设方案BLE(Bluetooth Low Energy)低功耗蓝牙兴起的原因BLE蓝牙的兴起主要是因为可穿戴设备的流行#xff0c;由于传统蓝牙不能满足可穿戴设备的续航要求#xff0c;因此大部分可穿戴设备采用蓝牙4.0技术#xff0c;即BLE蓝牙技术。BLE的特点快速搜索、快速连接、超低功耗连接和数据…BLE(Bluetooth Low Energy)低功耗蓝牙兴起的原因BLE蓝牙的兴起主要是因为可穿戴设备的流行由于传统蓝牙不能满足可穿戴设备的续航要求因此大部分可穿戴设备采用蓝牙4.0技术即BLE蓝牙技术。BLE的特点快速搜索、快速连接、超低功耗连接和数据传输,但是数据传输速率低。在Android开发中BLE蓝牙一包数据最多20个字节因此在Android系统下最好不要使用BLE蓝牙传输大量的数据BLE蓝牙开发流程1、申请蓝牙权限注意在Android6.0以上需要打开位置不然搜索不到蓝牙设备2、获取蓝牙适配器判断本地蓝牙是否可用打开蓝牙/*获取蓝牙适配器*/mBluetoothManager (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);mBluetoothAdapter mBluetoothManager.getAdapter();/*** 判断蓝牙是否可用*/public boolean isBlueAdapterEnable() {return mBluetoothAdapter !null mBluetoothAdapter.isEnabled();}如果本地蓝牙未打开调用已下代码打开本地蓝牙/* 打开蓝牙适配器*/Intent intent new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);context.startActivityForResult(intent, 0);3、建立BluetoothAdapter.LeScanCall类的对象BluetoothAdapter.LeScanCallback scanCallback new BluetoothAdapter.LeScanCallback(){Overridepublic void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {//TODO处理搜索到的蓝牙设备逻辑}};4、搜索蓝牙设备mBluetoothAdapter .startLeScan(scanCallback);5、停止搜索蓝牙设备mBluetoothAdapter.stopLeScan(scanCallback)6、建立BluetoothGattCallBack类对象private BluetoothGattCallbackgattCallback new BluetoothGattCallback() {/* 断开或连接 状态发生变化时调用* */Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {super.onConnectionStateChange(gatt, status, newState);//连接成功if (newState BluetoothGatt.STATE_CONNECTED) {Log.i(TAG, 连接成功);gatt.discoverServices();}else {Log.i(TAG, 连接失败);connectedfalse;}}/* 发现设备(真正建立连接)*/Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {super.onServicesDiscovered(gatt, status);//直到这里才是真正建立了可通信的连接connectedture;//获取初始化服务和特征值mBluetoothGatt gatt;initServiceAndChara(mBluetoothGatt);//订阅通知mBluetoothGatt.setCharacteristicNotification(mBluetoothGatt.getService(NotifyUUID).getCharacteristic(NOTIFY_DESCRIPTOR), true);}/* 读操作的回调*/Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicRead(gatt, characteristic, status);Log.e(TAG, onCharacteristicRead());}/* 写操作的回调*/Overridepublic void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicWrite(gatt, characteristic, status);Log.e(TAG, onCharacteristicWrite() status status ,value HexUtil.encodeHexStr(characteristic.getValue()));}/ * 接收到硬件返回的数据 */Overridepublic void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {super.onCharacteristicChanged(gatt, characteristic);Log.e(TAG,onCharacteristicChanged()characteristic.getValue());}};/*初始化mBlueGatt的服务和特征*/private void initServiceAndChara(BluetoothGatt mBluetoothGatt) {List bluetoothGattServices mBluetoothGatt.getServices();for (BluetoothGattService bluetoothGattService : bluetoothGattServices) {List characteristics bluetoothGattService.getCharacteristics();for (BluetoothGattCharacteristic characteristic : characteristics) {int charaProp characteristic.getProperties();if ((charaProp BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) {NotifyUUID bluetoothGattService.getUuid();NOTIFY_DESCRIPTOR characteristic.getUuid();}if ((charaProp BluetoothGattCharacteristic.PROPERTY_WRITE) 0) {bleWriteGattCharacteristic characteristic;}if ((charaProp BluetoothGattCharacteristic.PROPERTY_READ)0){bleReadGattCharacteristiccharacteristic;}Log.i(NOTIFY_DESCRIPTOR, NotifyUUID: NotifyUUID ,NOTIFY_DESCRIPTOR NOTIFY_DESCRIPTOR);}}}7、读取蓝牙数据public void readBleDeviceData(){if (connected mBluetoothGatt!null){mBluetoothGatt.readCharacteristic(bleReadGattCharacteristic);}}8、向蓝牙设备发送数据bleWriteGattCharacteristic.setValue(byte[] bytes);mBluetoothGatt.writeCharacteristic(bleWriteGattCharacteristic);9、断开蓝牙设备连接mBluetoothGatt.disconnect();