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

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

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

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

問題描述

/**

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

public class BluetoothUtils {

public static final int ENABLE_BLUETOOTH = 0; // 發現藍牙未開啟發送的開啟藍牙消息public static final int DEVICE_SCAN_STARTED = 1; // 掃描設備開始時發送的消息public static final int DEVICE_SCAN_STOPPED = 2; // 掃描終止時發送的消息public static final int DEVICE_SCAN_COMPLETED = 3; // 掃描設備完成時發送的消息public static final int DEVICE_CONNECTED = 4; // 連接上設備時發送的消息public static final int DATA_SENDED = 5; // 發送數據后發送清除edittext內容的消息public static final int DATA_READED = 6; // 讀取到數據后發送使適配器更新的消息public static final int CHARACTERISTIC_ACCESSIBLE = 7; // 可操作特征值時發送的消息private boolean mScanning; // 設備掃描狀態的標志private byte[] readedData; // 讀取到的字節數組數據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);}/** * 檢測藍牙開啟狀態,若未開啟則發送開啟藍牙消息 */public void checkBluetoothEnabled() { if (mBleAdapter == null || !mBleAdapter.isEnabled()) {Message message = new Message();message.what = ENABLE_BLUETOOTH;handler.sendMessage(message); }}/** * 檢測當前設備掃描的狀態,若在掃描中則停止掃描 */public void checkDeviceScanning() { if (mScanning) {scanBleDevice(false); }}/** * 檢測藍牙連接狀態,若已連接則斷開并關閉連接 */public void checkGattConnected() { if (mBluetoothGatt != null) {if (mBluetoothGatt.connect()) { mBluetoothGatt.disconnect(); mBluetoothGatt.close();} }}/** * 掃描設備的方法,掃描按鈕點擊后調用,掃描持續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); }}/** * 往特征值里寫入數據的方法 * * @param data 字節數組類型的數據 */public void writeData(byte[] data) { if (mBluetoothGatt != null) {if (mBluetoothGatt.connect() && mCharacteristic != null &&data != null) { mCharacteristic.setValue(data); mBluetoothGatt.writeCharacteristic(mCharacteristic);} }}/** * 創建一個新的設備列表對話框 */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,然后才能讀取數據 */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);}/** * 字節數組轉化為標準的16進制字符串 * * @param bytes 字節數組數據 * @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進制值的字節數組 * * @param s 字符串數據 * @return buf 字節數組 */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編碼的字節數組轉化為對應的字符串 * * @param bytes 字節數組 * @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();}/** * 獲取已讀取的數據 * * @return 字節數組數據 */public byte[] getReadedData() { return readedData;}/** * 獲取已讀取的數據長度 * * @return */public int getDataLen() { return dataBuffer.getSize();}/** * 獲取已讀取的數據 * * @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那邊有提示大概怎么實現

列表項目

問題解答

回答1:

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

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

主站蜘蛛池模板: 国产黄色的视频 | 国产精品99 | 一级级黄 | 国产三级精品在线观看 | 欧美乱xxxxxxxxx | 产国语一级特黄aa大片 | 久久高清一区二区三区 | 91免费视 | 九九久久久久久久爱 | 热99re久久精品精品免费 | 娇小性色xxxxx中文 | 国产人妖视频一区在线观看 | 成人午夜精品久久不卡 | 91香蕉小视频 | 久久久国产99久久国产久 | 亚洲综合专区 | 一级一级女人18毛片 | 色偷偷亚洲女人天堂观看欧 | 网友自拍视频在线观看 | 97免费在线视频 | 久久久久国产一级毛片高清板 | 日本特黄特色大片免费视频观看 | 中文字幕在线永久视频 | 国产大长吊 | 亚洲精品亚洲人成在线播放 | 色婷婷五 | 一级特黄aa大片免费 | www免费播放观看在线视频 | 中文字幕久久综合伊人 | 久久一区二区精品 | 亚洲精品高清久久 | 91sao国产在线观看 | 亚洲精品国产精品乱码视色 | 撸大师视频在线观看 | 欧美日产国产亚洲综合图区一 | 免费看黄资源大全高清 | 日韩毛片 | 久久精品综合视频 | 国产h视频在线观看高清 | 国产精品亚洲va在线观看 | 欧美中文字幕一区二区三区 |