当前位置: 首页 > 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://wiki.neutronadmin.com/news/387718/

相关文章:

  • 外贸网站推广seo网站后台怎么制作
  • 上海手机网站建设报价表绵阳网站开发
  • 绍兴seo网站优化潍坊中脉网站建设
  • 衡水移动网站建设给公司网站设计
  • 织梦网站导入链接怎么做徐汇网站制作
  • 企业门户网站国内外研究现状校园网站建设的用处
  • 精准营销的主要价值网站seo检测报告
  • 定制网站开发介绍图广告在线设计网站
  • 好用网站推荐免费宁海网站建设
  • 网站建设实训内容提高怎样做网站的外链
  • seo优化网站排名做好网络推广的技巧
  • 建立一个门户网站网站数据怎么做论文注释
  • 免费上线个人网站海北北京网站建设
  • 厦门网络公司网站开发贾汪建设局网站
  • 网站建设及运维方案wordpress注册输入密码
  • 深圳企业做网站公网站内容做淘宝店铺链接影响排名吗
  • 网站界面设计应遵循的原则网站建设的例子
  • 西部数码网站备案查询如何看一个网站是否做推广
  • dz仿网站头部WordPress访问mysql慢
  • h5网站免费建设百度关键词检测工具
  • 哪个网站可以做简历积极推进在线网站建设
  • 株洲网站建设报价wordpress a5
  • 免费建设网站制作营销公司网站模板下载
  • 网络维护网站网站上传文件存储方式
  • 上海网站建设润滋天津seo代理商
  • wap织梦手机网站网站建设及制作教程
  • 网站建设加盟合作网站运营服务商
  • 微企免费网站建设互联网推广代运营
  • 重庆营销网站制作wordpress 微信分享
  • 怎样在织梦后台里面做网站地图东莞本地招聘网站有哪些