日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

android - 安卓app實現(xiàn)與藍牙模塊的數(shù)據(jù)通信,當藍牙模塊離開有效距離時與手機app斷開連接,app想在斷開連接時有所提示,要怎么實現(xiàn)

瀏覽:299日期:2024-08-26 14:18:32

問題描述

/**

簡化藍牙操作的工具類*/

public class BluetoothUtils {

public static final int ENABLE_BLUETOOTH = 0; // 發(fā)現(xiàn)藍牙未開啟發(fā)送的開啟藍牙消息public static final int DEVICE_SCAN_STARTED = 1; // 掃描設備開始時發(fā)送的消息public static final int DEVICE_SCAN_STOPPED = 2; // 掃描終止時發(fā)送的消息public static final int DEVICE_SCAN_COMPLETED = 3; // 掃描設備完成時發(fā)送的消息public static final int DEVICE_CONNECTED = 4; // 連接上設備時發(fā)送的消息public static final int DATA_SENDED = 5; // 發(fā)送數(shù)據(jù)后發(fā)送清除edittext內容的消息public static final int DATA_READED = 6; // 讀取到數(shù)據(jù)后發(fā)送使適配器更新的消息public static final int CHARACTERISTIC_ACCESSIBLE = 7; // 可操作特征值時發(fā)送的消息private boolean mScanning; // 設備掃描狀態(tài)的標志private byte[] readedData; // 讀取到的字節(jié)數(shù)組數(shù)據(jù)private Context context;private Handler handler;private BluetoothAdapter mBleAdapter;private BluetoothGatt mBluetoothGatt;private BluetoothGattCharacteristic mCharacteristic;private DeviceListAdapter mDeviceListAdapter;private DataBuffer dataBuffer;public BluetoothUtils(Context context, Handler handler) { this.context = context; this.handler = handler;dataBuffer = new DataBuffer(4096); }public void initialize() { BluetoothManager manager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE); mBleAdapter = manager.getAdapter(); mDeviceListAdapter = new DeviceListAdapter(context);}/** * 檢測藍牙開啟狀態(tài),若未開啟則發(fā)送開啟藍牙消息 */public void checkBluetoothEnabled() { if (mBleAdapter == null || !mBleAdapter.isEnabled()) {Message message = new Message();message.what = ENABLE_BLUETOOTH;handler.sendMessage(message); }}/** * 檢測當前設備掃描的狀態(tài),若在掃描中則停止掃描 */public void checkDeviceScanning() { if (mScanning) {scanBleDevice(false); }}/** * 檢測藍牙連接狀態(tài),若已連接則斷開并關閉連接 */public void checkGattConnected() { if (mBluetoothGatt != null) {if (mBluetoothGatt.connect()) { mBluetoothGatt.disconnect(); mBluetoothGatt.close();} }}/** * 掃描設備的方法,掃描按鈕點擊后調用,掃描持續(xù)3秒 * * @param enable 掃描方法的使能標志 */public void scanBleDevice(boolean enable) { if (enable) {handler.postDelayed(new Runnable() { @Override public void run() {mScanning = false;mBleAdapter.stopLeScan(mLeScanCallback);Message message = new Message();message.what = DEVICE_SCAN_COMPLETED;handler.sendMessage(message); }}, 5000);mScanning = true; //mBleAdapter.startLeScan(new UUID[] {UUID.fromString('0000F445-0000-1000-8000-00805F9B34FB'),UUID.fromString('0000FEE0-0000-1000-8000-00805F9B34FB')},mLeScanCallback);mBleAdapter.startLeScan(mLeScanCallback);Message message = new Message();message.what = DEVICE_SCAN_STARTED;handler.sendMessage(message); } else {mScanning = false;mBleAdapter.stopLeScan(mLeScanCallback);Message message = new Message();message.what = DEVICE_SCAN_STOPPED;handler.sendMessage(message); }}/** * 往特征值里寫入數(shù)據(jù)的方法 * * @param data 字節(jié)數(shù)組類型的數(shù)據(jù) */public void writeData(byte[] data) { if (mBluetoothGatt != null) {if (mBluetoothGatt.connect() && mCharacteristic != null &&data != null) { mCharacteristic.setValue(data); mBluetoothGatt.writeCharacteristic(mCharacteristic);} }}/** * 創(chuàng)建一個新的設備列表對話框 */public void creatDeviceListDialog() { if (mDeviceListAdapter.getCount() > 0) {new AlertDialog.Builder(context).setCancelable(true) .setAdapter(mDeviceListAdapter, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) { BluetoothDevice device = mDeviceListAdapter.getDevice(which); mBluetoothGatt = device.connectGatt(context, false, mGattCallback);} }).show(); }}/** * 開啟特征值的notification,然后才能讀取數(shù)據(jù) */public void setCharacteristicNotification() { String clientUuid = '00002902-0000-1000-8000-00805f9b34fb'; mBluetoothGatt.setCharacteristicNotification(mCharacteristic, true); BluetoothGattDescriptor descriptor = mCharacteristic. getDescriptor(UUID.fromString(clientUuid)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor);}/** * 字節(jié)數(shù)組轉化為標準的16進制字符串 * * @param bytes 字節(jié)數(shù)組數(shù)據(jù) * @return 字符串 */public String bytesToString(byte[] bytes) { final char[] hexArray = '0123456789ABCDEF'.toCharArray(); char[] hexChars = new char[bytes.length * 2]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) {int v = bytes[i] & 0xFF;hexChars[i * 2] = hexArray[v >>> 4];hexChars[i * 2 + 1] = hexArray[v & 0x0F];sb.append(hexChars[i * 2]);sb.append(hexChars[i * 2 + 1]);sb.append(’ ’); } return sb.toString();}/** * 將字符串轉為16進制值的字節(jié)數(shù)組 * * @param s 字符串數(shù)據(jù) * @return buf 字節(jié)數(shù)組 */public byte[] stringToBytes(String s) { byte[] buf = new byte[s.length() / 2]; for (int i = 0; i < buf.length; i++) {try { buf[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);} catch (NumberFormatException e) { e.printStackTrace();} } return buf;}/** * Ascii編碼的字節(jié)數(shù)組轉化為對應的字符串 * * @param bytes 字節(jié)數(shù)組 * @return 字符串 */public String asciiToString(byte[] bytes) { char[] buf = new char[bytes.length]; StringBuilder sb = new StringBuilder(); for (int i = 0; i < buf.length; i++) {buf[i] = (char) bytes[i];sb.append(buf[i]); } return sb.toString();}/** * 變換文本的方法,有動畫效果 * * @param textView 目標文本view對象 * @param convertTextId 變換后的文本resId */public void convertText(final TextView textView, final int convertTextId) { final Animation scaleIn = AnimationUtils.loadAnimation(context, R.anim.text_scale_in); Animation scaleOut = AnimationUtils.loadAnimation(context, R.anim.text_scale_out); scaleOut.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) { textView.setText(convertTextId); textView.startAnimation(scaleIn);}@Overridepublic void onAnimationRepeat(Animation animation) {} }); textView.startAnimation(scaleOut);}/** * 獲取已連接設備的設備名 * * @return 字符串形式的設備名 */public String getDeviceName() { return mBluetoothGatt.getDevice().getName();}/** * 獲取已讀取的數(shù)據(jù) * * @return 字節(jié)數(shù)組數(shù)據(jù) */public byte[] getReadedData() { return readedData;}/** * 獲取已讀取的數(shù)據(jù)長度 * * @return */public int getDataLen() { return dataBuffer.getSize();}/** * 獲取已讀取的數(shù)據(jù) * * @return */public int getData(byte[] data_out,int len) { return dataBuffer.dequeue(data_out, len);}

/** * 連接Gatt之后的回調 */private BluetoothGattCallback mGattCallback =new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {if (newState == BluetoothProfile.STATE_CONNECTED) { Message message = new Message(); message.what = DEVICE_CONNECTED; handler.sendMessage(message); mDeviceListAdapter.clear(); gatt.discoverServices();} } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {if (characteristic != null) { dataBuffer.enqueue(characteristic.getValue(), characteristic.getValue().length); Message message = new Message(); message.what = DATA_READED; handler.sendMessage(message);} } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {if (status == BluetoothGatt.GATT_SUCCESS) { Message message = new Message(); message.what = DATA_SENDED; handler.sendMessage(message);} } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) {if (status == BluetoothGatt.GATT_SUCCESS) { // 得到目標特征值 String serviceUuid = '0000fee0-0000-1000-8000-00805f9b34fb'; String characterUuid = '0000fee1-0000-1000-8000-00805f9b34fb'; BluetoothGattService service = gatt.getService(UUID .fromString(serviceUuid)); mCharacteristic = service.getCharacteristic(UUID .fromString(characterUuid)); //開啟通知 setCharacteristicNotification(); Message message = new Message(); message.what = CHARACTERISTIC_ACCESSIBLE; handler.sendMessage(message);} } };/** * 藍牙掃描時的回調 */private BluetoothAdapter.LeScanCallback mLeScanCallback =new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {String buf = bytesToString(scanRecord);System.out.println('BluetoothUtils.enclosing_method():'+device.getName()+'nscanRecord'+buf+'rssi:'+rssi);//if ('E0 FE'.equals(buf.substring(0, buf.length()))) { mDeviceListAdapter.addDevice(device); mDeviceListAdapter.notifyDataSetChanged();} }};

}如上是藍牙操作的工具類,想在藍牙模塊與app斷開連接時,app那邊有提示大概怎么實現(xiàn)

列表項目

問題解答

回答1:

android - 安卓app實現(xiàn)與藍牙模塊的數(shù)據(jù)通信,當藍牙模塊離開有效距離時與手機app斷開連接,app想在斷開連接時有所提示,要怎么實現(xiàn)這里不就是連接狀態(tài)的回調嗎?

else if (newState == BluetoothProfile.STATE_DISCONNECTED) { TODO 這里做斷開以后的邏輯}

日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美日韩亚洲一区在线观看| 久久最新视频| 综合五月婷婷| 三级亚洲高清视频| 免费在线视频一区| 中文字幕一区二区三区四区久久 | 亚洲欧美不卡| 日韩视频一区| 天堂成人国产精品一区| 免费在线视频一区| 91国内精品| 国产精品羞羞答答在线观看| 国产精品久久久久久久久久久久久久久 | 久久激情综合网| 日本a级不卡| 国产欧美日韩免费观看| 卡一卡二国产精品| 亚洲最新无码中文字幕久久 | 欧美三级网址| 欧美不卡在线| 欧美专区一区二区三区| 中文字幕亚洲影视| 欧美自拍一区| 91综合网人人| 伊人久久成人| 日本不卡视频一二三区| 久久精品999| 天堂中文av在线资源库| 激情综合自拍| 亚洲日产国产精品| 欧美精品二区| 少妇精品导航| 在线精品视频一区| 欧美日韩精品一区二区三区视频 | 中文无码日韩欧| 国产欧美亚洲一区| 成人国产精品一区二区免费麻豆| 久久天堂av| 蜜桃久久久久久| 国产精品视频3p| 少妇久久久久| 日韩黄色在线观看| 中文字幕在线官网| 香蕉久久国产| 国产精品入口久久| 97精品一区| 免费日韩av片| 国产精品一区高清| 久久九九精品| 天海翼精品一区二区三区| 国产一区二区三区不卡视频网站| 红桃视频欧美| 精品一区二区三区四区五区| 九色精品91| 国产精品黄色| 国产综合亚洲精品一区二| 日韩精品欧美大片| 视频福利一区| 国产美女视频一区二区| 久久高清免费| 国产精品调教| 丝袜美腿亚洲一区| 国内精品麻豆美女在线播放视频| 先锋影音国产一区| 欧美男人天堂| 欧美亚洲一区二区三区| 亚洲成人免费| 麻豆91在线播放| 久久一二三区| 日韩欧美中文| 日韩福利视频网| 亚洲国内欧美| 久久精品一本| 日本亚洲不卡| 日韩精品欧美激情一区二区| 国产精品免费大片| 日本欧洲一区二区| 亚洲免费福利| 日本亚洲视频在线| 久久免费大视频| 国语对白精品一区二区| 日韩欧美中文字幕在线视频| 黄色不卡一区| 日韩理论片av| 久久av影视| 日韩一区二区三区免费视频 | 亚洲久久在线| 秋霞影视一区二区三区| 久久激五月天综合精品| 爽好久久久欧美精品| 日韩精品久久久久久久电影99爱| 久久久久久久久成人| 日本不卡一区二区| 久久午夜精品| 99久久99久久精品国产片果冰| 国产精品久久乐| 日本高清久久| 亚洲人成亚洲精品| 伊人久久亚洲美女图片| 色婷婷狠狠五月综合天色拍| 久久久久九九精品影院| 国产精选一区| 日韩高清一区在线| 综合激情在线| 欧美综合国产| 黄色成人精品网站| 欧美福利在线| 夜鲁夜鲁夜鲁视频在线播放| 韩日一区二区| 国产66精品| 久久精品三级| 久久精品天堂| 麻豆传媒一区二区三区| 国产精品日韩精品在线播放| 久久国产尿小便嘘嘘| 日韩国产91| 青草国产精品| 日韩中文字幕无砖| 亚洲精品美女| 中文字幕一区二区三区日韩精品 | 久久视频国产| 国产综合亚洲精品一区二| 欧美一区三区| 国产主播一区| 欧美福利在线| 99精品99| 久久xxxx| 亚洲资源在线| 97久久超碰| 国产精品蜜月aⅴ在线| 国产美女精品视频免费播放软件| 久久国产生活片100| 另类综合日韩欧美亚洲| 欧美激情日韩| 国产aⅴ精品一区二区三区久久 | 国产第一亚洲| 日韩欧美另类一区二区| 久久久久久久久久久妇女| 91精品国产自产在线观看永久∴ | 日韩有码av| 国产精品成人3p一区二区三区| 麻豆精品久久久| 国产一区二区三区日韩精品| 欧美羞羞视频| 久久av一区| 欧美精品中文字幕亚洲专区| 美女视频黄 久久| 久久久成人网| 国产美女精品| 亚洲aa在线| 麻豆一区二区在线| 日韩免费福利视频| 亚洲一卡久久| 国产亚洲一卡2卡3卡4卡新区| 精品久久久久中文字幕小说| 久久精品主播| 亚洲精品免费观看| 国产精品jk白丝蜜臀av小说| 亚洲黄色免费看| 香蕉久久国产| 国产精品日本一区二区三区在线| 国产成人1区| 亚洲一区国产一区| 国产欧美三级| 亚洲高清av| 亚洲精品美女91| 鲁大师精品99久久久| 欧美亚洲精品在线| 日本成人中文字幕在线视频| 国产66精品| 鲁大师成人一区二区三区| 国产亚洲高清在线观看| 精品国产日韩欧美精品国产欧美日韩一区二区三区 | 欧洲毛片在线视频免费观看| 日韩欧美久久| 激情黄产视频在线免费观看| 亚洲欧美日韩在线观看a三区| 国产精品一页| 国产在线|日韩| 亚洲字幕久久| av在线最新| 亚洲影院天堂中文av色| 久久精品一区二区三区中文字幕| 精品在线99| 欧美激情视频一区二区三区免费 | 快she精品国产999| 麻豆中文一区二区| 亚洲午夜一级| 欧美私人啪啪vps| 99精品在线| 国产精品玖玖玖在线资源| 亚洲国产成人精品女人| 国产精品亚洲片在线播放| 欧美精品一区二区三区精品| 欧美一区在线观看视频| 欧美日韩国产在线观看网站 | 国产精品一区二区精品视频观看| 国产麻豆久久| 国产精品美女久久久久久不卡| 9色国产精品|