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

个人建设什么网站网站怎么认证

个人建设什么网站,网站怎么认证,wordpress图片全部压缩,wordpress更新主题Android蓝牙4.0 Ble读写数据详解 -2上一篇说了如何扫描与链接蓝牙 这篇文章讲讲与蓝牙的数据传输#xff0c;与一些踩到的坑。先介绍一款调试工具#xff0c;专门调试Ble蓝牙的app。名字叫:nRF-Connect 谷歌应用商店也能下载到。这里我先连接一个蓝牙设备 贴几个截图。UUID的…Android蓝牙4.0 Ble读写数据详解 -2上一篇说了如何扫描与链接蓝牙 这篇文章讲讲与蓝牙的数据传输与一些踩到的坑。先介绍一款调试工具专门调试Ble蓝牙的app。名字叫:nRF-Connect 谷歌应用商店也能下载到。这里我先连接一个蓝牙设备 贴几个截图。UUID的话 就相当于钥匙蓝牙设备当中有通道那么通道是需要UUID进行匹配的当连接上设备之后可以看到UUID的通道 接下来按照设备厂商提供的文档找到我们需要的UUID通道比如说我这里需要的是0x6a的Service通道 然后点开最后一个Service通道查看展开Service后 可以看到有两个Characteristic通道我们看Properties属性 一个是NOTIFY 一个是WRITE 也有可能会有READ这个属性的通道可以拿这个app输出写出指令给蓝牙在不清楚是蓝牙的问题还是自己的问题的时候这个工具还是挺好使的。Notify的话需要注意这个Descriptors的UUID 这个在注册Notify的时候需要用到这里虽然看不全但是之后可以通过打印得到。简单说一下这三种属性的通道的用途WRITE:顾名思义写的意思该通道是用来向蓝牙设备写出数据的通道READ:向蓝牙设备进行读取数据的通道 这个通道有一个坑 后续会详细写上Notify:该通道需要注册监听这是一个通知的通道蓝牙向你传输数据的话就能直接收到监听。我这边的话 因为一些原因所以没有使用READ通道获取数据 只用了Notify通道 当然 也会讲讲怎么使用READ准备工作先将UUID管理起来我这里的话 采用静态常量的形式保存起来了。public class UUIDManager {/*** 服务的UUID*/public static final String SERVICE_UUID 00006a00-0000-1000-8000-00805f9b34fb;/*** 订阅通知的UUID*/public static final String NOTIFY_UUID 00006a02-0000-1000-8000-00805f9b34fb;/*** 写出数据的UUID*/public static final String WRITE_UUID 00006a02-0000-1000-8000-00805f9b34fb;/*** NOTIFY里面的Descriptor UUID*/public static final String NOTIFY_DESCRIPTOR 00002902-0000-1000-8000-00805f9b34fb;}处理通知回调接口蓝牙的数据回调其实并不是回调到主线程当中所以如果接收到数据之后就进行视图操作的话是会失败的。所以我打算切换到主线程进行回调当然也可以使用EventBus不过我不喜欢这东西就没去用。回调接口的话打算使用list集合存储起来然后回调到各个需要数据的地方。 创建以下三个类/*** Created by Pencilso on 2017/4/20.* 蓝牙数据回调监听接口*/public interface BlueNotifyListener {public void onNotify(Message notify);}/*** Created by Pencilso on 2017/4/25.* 处理回调所有接口*/public class NotifyThread implements Runnable {private List listeners;private Message notify;Overridepublic void run() {if (notify null || listenersnull)return;try {Iterator iterator listeners.iterator();while (iterator.hasNext()) {BlueNotifyListener next iterator.next();if (next null)iterator.remove();elsenext.onNotify(notify);}} catch (Exception e) {e.printStackTrace();}}public void setListeners(List listeners) {this.listeners listeners;}public void setNotify(Message notify) {this.notify notify;}}/*** Created by Pencilso on 2017/4/26.* 蓝牙的Code类 用来自定义回调的标识*/public class BlueCodeUtils {/*** 蓝牙状态 已连接*/public static final int BLUETOOTH_STATE_CONNECT 0x1;/*** 蓝牙状态 已断开*/public static final int BLUETOOTH_STATE_DISCONNECT 0x2;//*******这些只是自定义的code 就不复制太多了}编写蓝牙的功能新建BluetoothBinder类 继承自BluetoothGattCallback然后把蓝牙的功能模块写在这里面。主要的功能都在这个类里面我把这个放到了服务里面所以在创建BluetoothBinder的时候需要传递一个Service对象import android.annotation.SuppressLint;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothGatt;import android.bluetooth.BluetoothGattCallback;import android.bluetooth.BluetoothGattCharacteristic;import android.bluetooth.BluetoothGattDescriptor;import android.bluetooth.BluetoothGattService;import android.bluetooth.BluetoothManager;import android.bluetooth.BluetoothProfile;import android.content.Context;import android.os.Build;import android.os.Message;import android.support.annotation.RequiresApi;import android.util.Log;import java.util.List;import java.util.UUID;import cc.petnet.trenmotion.Interface.IBluetoothInterface;import cc.petnet.trenmotion.utils.HandleUtils;import cc.petnet.trenmotion.utils.LogUtils;/*** Created by Pencilso on 2017/4/20.* 蓝牙操作的Binder*/RequiresApi(api Build.VERSION_CODES.JELLY_BEAN_MR2)public class BluetoothBinder extends BluetoothGattCallback implements IBluetoothInterface {private static BluetoothBinder bluetoothBinder;private final Service bluetoothService;//service服务private final BluetoothAdapter adapter;//蓝牙的适配器private List notifyList;//监听的集合private BluetoothManager bluetoothManager;//蓝牙管理者private BluetoothGattService gattService;//通道服务private BluetoothGatt bluetoothGatt;public static IBluetoothInterface getInstace() {return bluetoothBinder;}RequiresApi(api Build.VERSION_CODES.JELLY_BEAN_MR2)protected BluetoothBinder(BluetoothService bluetoothService) {bluetoothBinder this;this.bluetoothService bluetoothService;bluetoothManager (BluetoothManager) bluetoothService.getSystemService(Context.BLUETOOTH_SERVICE);adapter bluetoothManager.getAdapter();}Overridepublic void onDestroy() {bluetoothBinder null;}Overridepublic void addNotifyListener(T notifyListener) {if (notifyListener ! null)notifyList.add(notifyListener);}Overridepublic void removeNotifyListener(BlueNotifyListener notifyListener) {if (notifyListener ! null)notifyList.remove(notifyListener);}/*** 广播蓝牙监听消息* 因为蓝牙发送过来的消息 并不是处于主线程当中的* 所以如果直接对蓝牙的数据展示视图的话 会展示不了的 这里的话 封装到主线程当中遍历回调数据** param notify*/public void traverseListener(Message notify) {NotifyThread notifyThread new NotifyThread();//创建一个遍历线程notifyThread.setListeners(notifyList);notifyThread.setNotify(notify);HandleUtils.getInstace().post(notifyThread);}/*** 系统的蓝牙是否已经打开** return*/Overridepublic boolean isEnable() {return adapter.isEnabled();}Overridepublic void enableBluetooth(boolean enable) {if (enable)adapter.enable();elseadapter.disable();}SuppressWarnings(deprecation)SuppressLint(NewApi)Overridepublic void startScanner(BluetoothAdapter.LeScanCallback callback) {adapter.startLeScan(callback);}SuppressLint(NewApi)SuppressWarnings(deprecation)Overridepublic void stopScanner(BluetoothAdapter.LeScanCallback callback) {adapter.stopLeScan(callback);}RequiresApi(api Build.VERSION_CODES.JELLY_BEAN_MR2)Overridepublic void connectDevices(String address) {BluetoothDevice remoteDevice adapter.getRemoteDevice(address);BluetoothGatt bluetoothGatt remoteDevice.connectGatt(bluetoothService, false, this);}/*** 蓝牙设备状态的监听** param gatt* param status* param newState 蓝牙的状态被改变*/Overridepublic void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {super.onConnectionStateChange(gatt, status, newState);Message message new Message();switch (newState) {//对蓝牙反馈的状态进行判断case BluetoothProfile.STATE_CONNECTED://已链接message.what BlueCodeUtils.BLUETOOTH_STATE_CONNECT;gatt.discoverServices();break;case BluetoothProfile.STATE_DISCONNECTED://已断开message.what BlueCodeUtils.BLUETOOTH_STATE_DISCONNECT;break;}traverseListener(message);/*** 这里还有一个需要注意的比如说蓝牙设备上有一些通道是一些参数之类的信息比如最常见的版本号。* 一般这种情况 版本号都是定死在某一个通道上直接读取也不需要发送指令的。* 如果遇到这种情况一定要在发现服务之后 再去读取这种数据 不要一连接成功就去获取*/}/*** 发现服务** param gatt* param status*/Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) {super.onServicesDiscovered(gatt, status);gattService gatt.getService(UUID.fromString(UUIDManager.SERVICE_UUID));// 获取到服务的通道bluetoothGatt gatt;//获取到Notify的Characteristic通道 这个根据协议来定 如果设备厂家给的协议不是Notify的话 就不用做以下操作了BluetoothGattCharacteristic notifyCharacteristic gattService.getCharacteristic(UUID.fromString(UUIDManager.NOTIFY_UUID));BluetoothUtils.enableNotification(gatt, true, notifyCharacteristic);//注册Notify通知}/*** 向蓝牙写入数据** param data*/SuppressLint(NewApi)Overridepublic boolean writeBuletoothData(String data) {if (bluetoothService null) {return false;}BluetoothGattCharacteristic writeCharact gattService.getCharacteristic(UUID.fromString(UUIDManager.WRITE_UUID));bluetoothGatt.setCharacteristicNotification(writeCharact, true); // 设置监听// 当数据传递到蓝牙之后// 会回调BluetoothGattCallback里面的write方法writeCharact.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);// 将需要传递的数据 打碎成16进制writeCharact.setValue(BluetoothUtils.getHexBytes(data));return bluetoothGatt.writeCharacteristic(writeCharact);}/*** 蓝牙Notify推送过来的数据** param gatt* param characteristic*/Overridepublic void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {super.onCharacteristicChanged(gatt, characteristic);//如果推送的是十六进制的数据的写法String data BluetoothUtils.bytesToHexString(characteristic.getValue()); // 将字节转化为String字符串Message message new Message();message.what BlueCodeUtils.BLUETOOTH_PUSH_MESSAGE;message.obj data;traverseListener(message);//回调数据// String data characteristic.getStringValue(0); // 使用场景 例如某个通道里面静态的定死了某一个值就用这种写法获取 直接获取到String类型的数据}/*** 这里有一个坑 一定要注意如果设备返回数据用的不是Notify的话 一定要注意这个问题* 这个方法是 向蓝牙设备写出数据成功之后回调的方法写出成功之后干嘛呢 主动去蓝牙获取数据没错自己主动去READ通道获取蓝牙数据* 如果用的是Notify的话 不用理会该方法 写出到蓝牙之后 等待Notify的监听 即onCharacteristicChanged方法回调。** param gatt* param characteristic* param status*/Overridepublic void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicWrite(gatt, characteristic, status);if (status BluetoothGatt.GATT_SUCCESS) { //写出成功 接下来 该去读取蓝牙设备的数据了//这里的READUUID 应该是READ通道的UUID 不过我这边的设备没有用到READ通道 所以我这里就注释了 具体使用 视情况而定// BluetoothGattCharacteristic readCharact gattService.getCharacteristic(UUID.fromString(READUUID));// gatt.readCharacteristic(readCharact);}}/*** 调用读取READ通道后返回的数据回调* 比如说 在onCharacteristicWrite里面调用 gatt.readCharacteristic(readCharact);之后 会回调该方法** param gatt* param characteristic* param status*/Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {super.onCharacteristicRead(gatt, characteristic, status);//如果推送的是十六进制的数据的写法String data BluetoothUtils.bytesToHexString(characteristic.getValue()); // 将字节转化为String字符串Message message new Message();message.what BlueCodeUtils.BLUETOOTH_PUSH_MESSAGE;message.obj data;traverseListener(message);//回调数据// String data characteristic.getStringValue(0); // 使用场景 例如某个通道里面静态的定死了某一个值就用这种写法获取 直接获取到String类型的数据}}其实这个BluetoothBinder还定义了一个接口IBluetoothInterface然后让BluetoothBinder实现并且将BluetoothBinder设置单利暴露方法提供返回IBluetoothInterface对象import android.bluetooth.BluetoothAdapter;import cc.petnet.trenmotion.service.bluetooth.BlueNotifyListener;/*** Created by Pencilso on 2017/4/20.*/public interface IBluetoothInterface {/**** param notifyListener 添加监听事件* param BlueNotifyListener监听回调接口*/ void addNotifyListener(T notifyListener);/**** param notifyListener 删除监听接口*/void removeNotifyListener(BlueNotifyListener notifyListener);/*** 系统是否开启了蓝牙** return*/boolean isEnable();/*** 打开或者关闭系统蓝牙** param enable*/void enableBluetooth(boolean enable);/*** 启动扫描** param callback*/void startScanner(BluetoothAdapter.LeScanCallback callback);/*** 停止扫描** param callback*/void stopScanner(BluetoothAdapter.LeScanCallback callback);void onDestroy();/*** 连接设备** param address*/void connectDevices(String address);/*** 向蓝牙写出数据* param data* return*/public boolean writeBuletoothData(String data);}Handler工具 主要用来切换到主线程当中public class HandleUtils {private static Handler handler null;public static Handler getInstace() {if (handler null)handler new Handler();return handler;}}BluetoothUtilspublic class BluetoothUtils {/*** 是否开启蓝牙的通知** param enable* param characteristic* return*/SuppressLint(NewApi)public static boolean enableNotification(BluetoothGatt bluetoothGatt, boolean enable, BluetoothGattCharacteristic characteristic) {if (bluetoothGatt null || characteristic null) {return false;}if (!bluetoothGatt.setCharacteristicNotification(characteristic, enable)) {return false;}//获取到Notify当中的Descriptor通道 然后再进行注册BluetoothGattDescriptor clientConfig characteristic.getDescriptor(UUID.fromString(UUIDManager.NOTIFY_DESCRIPTOR));if (clientConfig null) {return false;}if (enable) {clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);} else {clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);}return bluetoothGatt.writeDescriptor(clientConfig);}/*** 将字节 转换为字符串** param src 需要转换的字节数组* return 返回转换完之后的数据*/public static String bytesToHexString(byte[] src) {StringBuilder stringBuilder new StringBuilder();if (src null || src.length 0) {return null;}for (int i 0; i src.length; i) {int v src[i] 0xFF;String hv Integer.toHexString(v);if (hv.length() 2) {stringBuilder.append(0);}stringBuilder.append(hv);}return stringBuilder.toString();}/*** 将字符串转化为16进制的字节** param message* 需要被转换的字符* return*/public static byte[] getHexBytes(String message) {int len message.length() / 2;char[] chars message.toCharArray();String[] hexStr new String[len];byte[] bytes new byte[len];for (int i 0, j 0; j len; i 2, j) {hexStr[j] chars[i] chars[i 1];bytes[j] (byte) Integer.parseInt(hexStr[j], 16);}return bytes;}}蓝牙基本上到这已经结束了。
http://www.zqtcl.cn/news/93884/

相关文章:

  • jsp做网站 案例网站模板 招聘
  • 德州建设银行兑换网站服务器网站跳转怎么做的
  • 金华专业做网站公司湖南网站建设服务
  • 企业网站设计沈阳苏宁电器网站建设特点分析
  • 建设工程类公司网站易语言可以做api网站对接吗
  • 青岛做网站皆赴青岛博wordpress 数据库 备份
  • 外贸公司网站空间哈尔滨seo优化专注
  • 建筑行业综合查询平台优化推广联盟
  • 北京管庄网站建设公司开平网站制作
  • 如何做销售直播网站最专业网站建设
  • 太原市住房和城乡建设局的网站首页网络推广服务外包公司
  • 湘icp备 网站建设 农业 湖南稿定设计免费版
  • 公司网站推广方法陕西省住房建设厅官网
  • 网站关键词排名突然没了无锡企业网站建设报价
  • 找做网站的人网站改版 301跳转
  • 网站备案一次就可以了吧营销管理培训课程
  • 怎么做网站背景专做民宿预定的网站
  • wordpress安装谷歌分析代码建网站seo
  • 百度外卖网站建设与维护方法建设 银行网网站
  • 小程序开发定制开发上海优化价格
  • 来宾住房和城乡建设局网站做外贸推广要做哪些平台
  • 无锡建设网站制作wordpress 知乎
  • 动漫网站源码免费怎么怎么做网站
  • 和两个黑人同时做网站中工互联网站建设
  • windows10PHP 网站建设app应用分发平台开发
  • 中唯建设工程有限公司网站做网站页面对PS切图
  • 个人网页制作成品欣赏seo网站沙盒期
  • 亚马逊站外推广网站怎么做制作营销网站模板免费下载
  • 加拿大网站后缀设计师互联网
  • 做物流的网站有哪些内容共同建设网站心得