当前位置: 首页 > news >正文

网络网站知识app职业教育网站开发

网络网站知识app,职业教育网站开发,网站编辑器无法显示,只做网站应该找谁在安卓上进行BLE开发时#xff0c;就不必像理解BLE协议栈那样复杂了。因为安卓的BLE包为我们提供了十分丰富的API、各类常量、各类连接通信情况下的回调API等。 具体流程 一、声明权限 二、获取Adapter适配器 三、开启蓝牙 四、BLE扫描与停止 五、连接设备 六、枚举特征…在安卓上进行BLE开发时就不必像理解BLE协议栈那样复杂了。因为安卓的BLE包为我们提供了十分丰富的API、各类常量、各类连接通信情况下的回调API等。 具体流程 一、声明权限 二、获取Adapter适配器 三、开启蓝牙 四、BLE扫描与停止 五、连接设备 六、枚举特征值及其属性 七、利用特征值通讯 八、关闭蓝牙 一、声明权限 在AndroidManifest.xml文件中声明应用需要的特性及权限。 !-- 声明App使用条件为支持BLE -- uses-feature android:nameandroid.hardware.bluetooth_le android:requiredtrue/ !-- 声明蓝牙权限 -- uses-permission android:nameandroid.permission.BLUETOOTH/ uses-permission android:nameandroid.permission.BLUETOOTH_ADMIN/ !-- 安卓6.0开始需要此权限 -- uses-permission android:nameandroid.permission.ACCESS_COARSE_LOCATION/ 二、获取Adapter适配器 final BluetoothManager mBluetoothManager (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter mBluetoothManager.getAdapter(); 三、开启蓝牙 if (mBluetoothAdapter ! null !mBluetoothAdapter.isEnabled()) {Intent intent new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(intent, BLE_ENABLE); } 四、BLE扫描与停止 private boolean mScanning;//是否正在搜索 private Handler mHandler new Handler(); // 预设15秒扫描时间 private static final int SCAN_PERIOD 15000;private void scanLeDevice(final boolean enable) {if (enable) {// 控制BLE扫描时间mHandler.postDelayed(new Runnable() {Overridepublic void run() {mBluetoothAdapter.stopLeScan(mLeScanCallback);}}, SCAN_PERIOD);mScanning true;// 开始扫描mBluetoothAdapter.startLeScan(mLeScanCallback);} else {mScanning false;// 停止扫描mBluetoothAdapter.stopLeScan(mLeScanCallback);} }在BLE扫描回调函数中保存设备对应的BlueToothDevice对象rssi信号强度等。 private BluetoothAdapter.LeScanCallback mLeScanCallback new BluetoothAdapter.LeScanCallback() {Overridepublic void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {// 保存device及相关信息也可以通知UI线程显示信息}};五、连接设备 使用扫描结果中保存的BluetoothDevice对象调用connectGatt进行通讯。 BluetoothGatt mBluetoothGatt; // 参数一context上下文 // 参数二是否自动重连 // 参数三: 连接回调 mBluetoothGatt mBluetoothDevice.connectGatt(context, false, mGattCallback); 连接回调函数实现如下开发时在此处处理各类可能遇到的情况。 private BluetoothGattCallback mGattCallback new BluetoothGattCallback() {// 连接状态改变的回调Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {// 具体处理见后文第八项};// 发现服务回调Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {// 具体处理见后文第六项};Overridepublic void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {// 由mBluetoothGatt.readRemoteRssi()调用得到可不停刷新rssi信号强度Log.e(TAG, 信号强度RSSI rssi);}// 写描述信息回调Overridepublic void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptordescriptor, int status) {};// 写操作回调Overridepublic void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {if (status BluetoothGatt.GATT_SUCCESS) {Log.e(TAG, 写入成功 characteristic.getValue());}};// 读操作回调Overridepublic void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {if (status BluetoothGatt.GATT_SUCCESS) {Log.e(TAG, 读取成功 characteristic.getValue());}}// 数据改变回调接收BLE设备发送的数据Overridepublic void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {};}; }六、枚举特征值及其属性 Override // 发现服务回调即调用了mBluetoothGatt.discoverServices()执行的回调 public void onServicesDiscovered(BluetoothGatt gatt, int status) {if (status BluetoothGatt.GATT_SUCCESS) {// 获取ServiceListBluetoothGattService mGattServices gatt.getServices();// 获取Service的Characteristicsfor (BluetoothGattService gattService : mGattServices) {ListBluetoothGattCharacteristic mGattCharacteristics gattService.getCharacteristics();for (BluetoothGattCharacteristic gattCharacteristic :mGattCharacteristics) {int charaProp gattCharacteristic.getProperties();// 所有Characteristics按属性分类if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) 0) {Log.e(TAG, gattCharacteristic的UUID为: gattCharacteristic.getUuid());Log.e(TAG, gattCharacteristic的属性为可读);readUuids.add(gattCharacteristic.getUuid());}if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) 0) {Log.e(TAG, gattCharacteristic的UUID为: gattCharacteristic.getUuid());Log.e(TAG, gattCharacteristic的属性为可写);writeUuids.add(gattCharacteristic.getUuid());}if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) 0) {Log.e(TAG, gattCharacteristic的UUID为: gattCharacteristic.getUuid() gattCharacteristic);Log.e(TAG, gattCharacteristic的属性为具备通知属性);notifyUuids.add(gattCharacteristic.getUuid());}}}} else {Log.e(TAG, onServicesDiscovered 失败status status);} }七、利用特征值通讯 1、写数据 public void writeChara(byte) {BluetoothGattCharacteristic mGattCharacteristic mBluetoothGatt.getCharacteristic(writeUuid);mGattCharacteristic.setValue(sendValue);mBluetoothGatt.writeCharacteristic(mGattCharacteristic); } 2、读数据 public void readChara() {// 读取数据BluetoothGattCharacteristic mGattCharacteristic mBluetoothGatt.getCharacteristic(readUuid);mBluetoothGatt.readCharacteristic(mGattCharacteristic); }3、监听通知属性的数据 mBluetoothGatt.setCharacteristicNotification(mGattCharacteristic, enabled); BluetoothGattDescriptor mGattDescriptor mGattCharacteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); mGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(mGattDescriptor); 八、关闭蓝牙 合理的关闭步骤为先调用BluetoothGatt#disconnect进行断开连接此时会在BluetoothGattCallback#onConnectionStateChange接收到断开成功的回调然后在回调中调用BluetoothGatt#close释放相关资源。 // 首先执行该disconnect操作然后等待回调通知 mBluetoothGatt.disconnect(); Override public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {if (newState ! BluetoothGatt.GATT_SUCCESS) {// 连接失败直接在此处理即可因此时调用disconnect无法进入下面条件Log.e(TAG, 连接失败, status: status);gatt.close();return;}if (newState BluetoothGatt.STATE_DISCONNECTED) {// 连接断开Log.e(TAG, 连接断开);mBluetoothGatt.close();} else if (newState BluetoothProfile.STATE_CONNECTED) {// 连接成功后启动服务发现Log.e(TAG, 连接成功);mBluetoothGatt.discoverServices();} };
http://www.huolong8.cn/news/387718/

相关文章:

  • 山西省最新干部调整杭州 seo网站建设 网络服务
  • 德保县建设局的网站wordpress 图片加文字
  • 互联网公司网站建设ppt朋友说做网站什么的怎么赚钱
  • 经典企业网站欣赏WordPress怎么添加留言功能
  • 可以做网站的公司企业电子商务网站开发实训目的
  • 网站锚点链接怎么做什么做电子书下载网站
  • 在线ps网站辽宁省住房和城乡建设部网站主页
  • 南昌启航科技外国网站在内地做seo
  • 网站开发常用数据库聚财洋气三个字公司名字
  • 适合个人做的外贸平台昆明网站seo优化
  • 四川网站备案核验单关于单位网站建设的
  • 网站域名等级成都app开发团队
  • 网站页面布局的目的公司建设网站哪家好
  • 个人做网站 优帮云怎样在别人网站做加强链接
  • 深圳公司网站设计哪家好青海住房建设厅网站
  • 张家口市网站建设商标交易网
  • 建立网站ftp是什么网页界面设计中一般使用的分辨率显示密度是
  • 网站审核要多久网站备案拍照要求
  • 公司推广做哪个网站吗一级造价师停考最新消息
  • wep购物网站开发模板wordpress 放弃
  • 网站域名怎么起菜鸟建网站
  • 公园网站建设方案 ppt模板三亚
  • 网站充值 下模板宁波正规优化seo公司
  • 企业网站建设人员分析免费注册的网站
  • 社交类网站开发新品发布会一般在哪里举行
  • 建设网站的法律可行性上海装修公司前十强排名榜
  • 福田做网站库车网站建设
  • 搭建网站是什么专业旅行网站定制公司
  • 东莞搜索seo网站关键词优化wordpress积分冻结
  • 制作网站比较大的几家公司辽宁网站推广