亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

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

瀏覽:143日期: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 這里做斷開以后的邏輯}

主站蜘蛛池模板: 亚洲成人在线视频观看 | 日韩在线国产 | 久久久久久91香蕉国产 | 中文字幕一区精品欧美 | 亚洲高清视频免费 | 爱啪网站| 色 综合 欧美 亚洲 国产 | 亚州中文字幕 | 免费黄色| 99久久精品费精品国产一区二 | 精品你懂的 | 华人在线视频 | 欧美人成人亚洲专区中文字幕 | 黄a视频| 日韩 亚洲 欧美 中文 高清 | 一级做a爱过程免费视频时看 | 久久亚洲国产最新网站 | 被窝福利无限 | 日韩手机在线视频 | 国产精品免费视频一区一 | 欧美啊v在线观看 | 青青青国产观看免费视频 | 麻豆网站在线 | 国产在线精品福利91香蕉 | 亚洲精品久中文字幕 | aaa大片| 好吊色青青青国产欧美日韩 | 久久中文字幕美谷朱里 | 丝袜足液精子免费视频 | 一及黄色毛片 | 一级黄色片毛片 | 国产黄a三级三级看三级 | 特级aa一级欧美毛片 | 色综合天天综合网国产人 | 一级看片免费视频 | 久久99精品久久久久久h | 国产视频福利 | 中文字幕亚洲高清综合 | 在线视频一区二区三区 | 国产精品一区二区在线播放 | 亚洲最大综合网 |