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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

問題描述

/**

簡(jiǎn)化藍(lán)牙操作的工具類*/

public class BluetoothUtils {

public static final int ENABLE_BLUETOOTH = 0; // 發(fā)現(xiàn)藍(lán)牙未開啟發(fā)送的開啟藍(lán)牙消息public static final int DEVICE_SCAN_STARTED = 1; // 掃描設(shè)備開始時(shí)發(fā)送的消息public static final int DEVICE_SCAN_STOPPED = 2; // 掃描終止時(shí)發(fā)送的消息public static final int DEVICE_SCAN_COMPLETED = 3; // 掃描設(shè)備完成時(shí)發(fā)送的消息public static final int DEVICE_CONNECTED = 4; // 連接上設(shè)備時(shí)發(fā)送的消息public static final int DATA_SENDED = 5; // 發(fā)送數(shù)據(jù)后發(fā)送清除edittext內(nèi)容的消息public static final int DATA_READED = 6; // 讀取到數(shù)據(jù)后發(fā)送使適配器更新的消息public static final int CHARACTERISTIC_ACCESSIBLE = 7; // 可操作特征值時(shí)發(fā)送的消息private boolean mScanning; // 設(shè)備掃描狀態(tài)的標(biāo)志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);}/** * 檢測(cè)藍(lán)牙開啟狀態(tài),若未開啟則發(fā)送開啟藍(lán)牙消息 */public void checkBluetoothEnabled() { if (mBleAdapter == null || !mBleAdapter.isEnabled()) {Message message = new Message();message.what = ENABLE_BLUETOOTH;handler.sendMessage(message); }}/** * 檢測(cè)當(dāng)前設(shè)備掃描的狀態(tài),若在掃描中則停止掃描 */public void checkDeviceScanning() { if (mScanning) {scanBleDevice(false); }}/** * 檢測(cè)藍(lán)牙連接狀態(tài),若已連接則斷開并關(guān)閉連接 */public void checkGattConnected() { if (mBluetoothGatt != null) {if (mBluetoothGatt.connect()) { mBluetoothGatt.disconnect(); mBluetoothGatt.close();} }}/** * 掃描設(shè)備的方法,掃描按鈕點(diǎn)擊后調(diào)用,掃描持續(xù)3秒 * * @param enable 掃描方法的使能標(biāo)志 */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)建一個(gè)新的設(shè)備列表對(duì)話框 */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ù)組轉(zhuǎn)化為標(biāo)準(zhǔn)的16進(jìn)制字符串 * * @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();}/** * 將字符串轉(zhuǎn)為16進(jìn)制值的字節(jié)數(shù)組 * * @param s 字符串?dāng)?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ù)組轉(zhuǎn)化為對(duì)應(yīng)的字符串 * * @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();}/** * 變換文本的方法,有動(dòng)畫效果 * * @param textView 目標(biāo)文本view對(duì)象 * @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);}/** * 獲取已連接設(shè)備的設(shè)備名 * * @return 字符串形式的設(shè)備名 */public String getDeviceName() { return mBluetoothGatt.getDevice().getName();}/** * 獲取已讀取的數(shù)據(jù) * * @return 字節(jié)數(shù)組數(shù)據(jù) */public byte[] getReadedData() { return readedData;}/** * 獲取已讀取的數(shù)據(jù)長(zhǎng)度 * * @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之后的回調(diào) */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) { // 得到目標(biāo)特征值 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);} } };/** * 藍(lán)牙掃描時(shí)的回調(diào) */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();} }};

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

列表項(xiàng)目

問題解答

回答1:

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

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

日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产一区成人| 日韩不卡免费高清视频| 青青草国产成人99久久| 五月婷婷亚洲| 高清av不卡| 精品国产一级| 欧美日本精品| 日本不卡视频在线观看| 悠悠资源网久久精品| 日本免费久久| av资源中文在线天堂| 狂野欧美性猛交xxxx| 国产女人18毛片水真多18精品| 欧美在线资源| 韩日一区二区三区| 蜜桃tv一区二区三区| 成人在线免费观看网站| 精品久久视频| 国产精品福利在线观看播放| 欧美黄页在线免费观看| 国产精品tv| 激情久久一区二区| 日产精品一区二区| 亚洲伦乱视频| 久久影视一区| 视频在线观看国产精品| 免费人成精品欧美精品| 亚洲精品女人| 另类欧美日韩国产在线| 亚洲黄色免费av| 欧美 日韩 国产一区二区在线视频 | 久久av中文| 久久精品国产99国产| 国产一二在线播放| 国产专区一区| 亚洲精品精选| 国产精品美女久久久久久不卡| 国产精品天堂蜜av在线播放| 国产第一亚洲| 在线视频日韩| 成人在线黄色| av在线日韩| 亚洲精品极品| 日韩电影免费网站| 欧美午夜精品一区二区三区电影| 一区免费在线| 国产毛片精品| 欧美一区二区三区高清视频| 亚洲麻豆一区| а√天堂8资源中文在线| 亚洲一区av| 国产传媒在线观看| 少妇精品久久久一区二区| 丰满少妇一区| 亚洲精品大片| 欧美日韩亚洲在线观看| 久久激五月天综合精品| 亚洲午夜在线| 精品不卡一区| 免费成人av在线播放| 麻豆视频在线观看免费网站黄| 亚洲综合福利| 久久精品高清| 国产91在线精品| 日本成人在线不卡视频| 亚洲午夜视频| 在线手机中文字幕| 欧美日韩一区二区高清| 视频在线在亚洲| 蜜臀久久99精品久久一区二区 | 精品视频在线观看网站| 四虎精品永久免费| 人人精品人人爱| 中文字幕一区久| 久久久久久亚洲精品美女| 日韩美女国产精品| 三级一区在线视频先锋| 久久久夜夜夜| 91精品推荐| 99热精品久久| 成人av二区| 午夜精品一区二区三区国产| 日本不卡免费高清视频在线| 久久精品福利| 国产一区二区久久久久| 精品在线网站观看| 六月婷婷综合| 欧美日韩中文一区二区| 国产成人精品福利| 色网在线免费观看| 91精品啪在线观看国产18| 亚洲不卡av不卡一区二区| 亚洲啊v在线| 久久精品国产99久久| 免费人成在线不卡| 国产亚洲欧美日韩精品一区二区三区| 日本在线视频一区二区| 国产亚洲欧美日韩在线观看一区二区| 久久丁香四色| 久久精品高清| 午夜性色一区二区三区免费视频| 青青草国产成人99久久| 欧美韩日一区| 久久亚洲精品中文字幕蜜潮电影| 婷婷综合亚洲| 久久精品99国产精品日本| 美女一区网站| 日本aⅴ免费视频一区二区三区| 国产探花在线精品一区二区| 精品国产美女a久久9999| 色爱av综合网| 久久国内精品| 婷婷丁香综合| 久久的色偷偷| 99视频在线精品国自产拍免费观看| 日韩欧美在线精品| 久久久久久久久久久妇女 | 日韩av不卡一区二区| 国产精品久久观看| 日韩三级久久| 欧美日韩国产免费观看视频| 欧美国产中文高清| 免费视频一区二区| 亚洲精品一区三区三区在线观看| 日韩国产在线一| 欧美另类综合| 中文av在线全新| 国产欧美日韩精品一区二区免费 | 亚洲精品一级二级三级| 亚洲天堂免费电影| 国产精品红桃| 亚洲a级精品| 亚洲一区二区网站| 999国产精品| 中文字幕在线视频网站| 男女男精品网站| 亚洲永久av| 日韩一区亚洲二区| 国产精品一二| 国产欧美69| 日韩福利在线观看| 亚洲精品日本| 午夜一区在线| 免费日韩av| 亚洲女人av| 丝袜国产日韩另类美女| 日本韩国欧美超级黄在线观看| 国产精品15p| 国产欧美日韩影院| 国产乱人伦丫前精品视频| 国产亚洲激情| 噜噜噜躁狠狠躁狠狠精品视频| 亚洲免费影视| 亚洲精品自拍| 91亚洲精品视频在线观看| 一本一道久久a久久| 免费人成在线不卡| 日韩视频二区| 一区二区日韩免费看| 清纯唯美亚洲综合一区| 欧美在线不卡| 久久久精品国产**网站| 蜜桃视频在线网站| 黄页网站一区| 97久久亚洲| 国产精品久久久久久久免费观看| 欧美日韩在线观看首页| 九色精品91| 欧美一区二区三区免费看| 国产精品毛片久久久| 日韩免费看片| 天堂成人免费av电影一区| 蜜臀va亚洲va欧美va天堂| 国产精品香蕉| 欧美不卡视频| 国产精品视频一区视频二区| 欧美日韩国产v| 亚洲精品激情| 麻豆视频在线看| 亚洲精品大全| 久久精品国产亚洲夜色av网站 | 日韩制服丝袜先锋影音| 国产精品videossex久久发布| 免费不卡中文字幕在线| 国产精品白浆| 天使萌一区二区三区免费观看| 精品中国亚洲| 国产免费播放一区二区| 一区二区亚洲精品| 欧美日韩尤物久久| 日韩综合在线| 麻豆国产欧美日韩综合精品二区| 欧美特黄a级高清免费大片a级| 精品免费在线| 国产欧美日韩在线观看视频| 日韩亚洲在线| 亚洲午夜黄色| 亚洲成av人片一区二区密柚| 国产精品蜜月aⅴ在线| 综合国产精品|