編輯:關於 Android 程式設計
新年第一篇。
最近在做一個藍牙開鎖的小專案,手機去連接單片機總是出問題,和手機的連接也不太穩定。看了不少藍牙方面的文件後,做了一份關於藍牙連接的小結。
在做 Android 藍牙串口連接時,一般會使用:
BluetoothSocket tmp = null; // Get a BluetoothSocket for a connection with the // given BluetoothDevice try { tmp = device.createRfcommSocketToServiceRecord(MY_UUID); } catch (IOException e) { Log.e(TAG, "create() failed", e); }
然後把 tmp 賦給 BluetoothSocket,接著呼叫 connect 方法進行藍牙裝置的連接。可是 BluetoothSocket 的 connect 方法本身就會拋出很多異常錯誤。
以下根據對藍牙開發的一點研究,可透過以下方法來解決:
方法一:先進行藍牙自動配對,配對成功後,透過 UUID 取得 BluetoothSocket,然後執行 connect() 方法。
方法二:透過 UUID 取得 BluetoothSocket,然後先根據 mDevice.getBondState() 判斷是否需要配對,最後執行 connect() 方法。
private class ConnectThread extends Thread { String macAddress = ""; public ConnectThread(String mac) { macAddress = mac; } public void run() { connecting = true; connected = false; if(mBluetoothAdapter == null){ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress); mBluetoothAdapter.cancelDiscovery(); try { socket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid); } catch (IOException e) { // TODO Auto-generated catch block //e.printStackTrace(); Log.e(TAG, "Socket", e); } //adapter.cancelDiscovery(); while (!connected && connetTime <= 10) { connectDevice(); } // 重置ConnectThread //synchronized (BluetoothService.this) { //ConnectThread = null; //} } public void cancel() { try { socket.close(); socket = null; } catch (Exception e) { e.printStackTrace(); } finally { connecting = false; } } }
接著是被呼叫的連接裝置方法 connectDevice():
protected void connectDevice() { try { // 連接建立之前的先配對 if (mBluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) { Method creMethod = BluetoothDevice.class .getMethod("createBond"); Log.e("TAG", "開始配對"); creMethod.invoke(mBluetoothDevice); } else { } } catch (Exception e) { // TODO: handle exception //DisplayMessage("無法配對!"); e.printStackTrace(); } mBluetoothAdapter.cancelDiscovery(); try { socket.connect(); //DisplayMessage("連接成功!"); //connetTime++; connected = true; } catch (IOException e) { // TODO: handle exception //DisplayMessage("連接失敗!"); connetTime++; connected = false; try { socket.close(); socket = null; } catch (IOException e2) { // TODO: handle exception Log.e(TAG, "Cannot close connection when connection failed"); } } finally { connecting = false; } }
方法三:利用反射透過埠號取得 BluetoothSocket,然後執行 connect() 方法。
private class ConnectThread extends Thread { String macAddress = ""; public ConnectThread(String mac) { macAddress = mac; } public void run() { connecting = true; connected = false; if(mBluetoothAdapter == null){ mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); } mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(macAddress); mBluetoothAdapter.cancelDiscovery(); initSocket(); //adapter.cancelDiscovery(); while (!connected && connetTime <= 10) { try { socket.connect(); connected = true; } catch (IOException e1) { connetTime++; connected = false; // 關閉 socket try { socket.close(); socket = null; } catch (IOException e2) { //TODO: handle exception Log.e(TAG, "Socket", e2); } } finally { connecting = false; } //connectDevice(); } // 重置ConnectThread //synchronized (BluetoothService.this) { //ConnectThread = null; //} } public void cancel() { try { socket.close(); socket = null; } catch (Exception e) { e.printStackTrace(); } finally { connecting = false; } } }
接著是初始化並取得 BluetoothSocket 的方法:
/** * 取得BluetoothSocket */ private void initSocket() { BluetoothSocket temp = null; try { Method m = mBluetoothDevice.getClass().getMethod( "createRfcommSocket", new Class[] { int.class }); temp = (BluetoothSocket) m.invoke(mBluetoothDevice, 1);//這裡端口為1 } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } socket = temp; }
要點:
1. 藍牙配對和連接是兩回事,不可混為一談。
2. 藍牙串口連接可透過埠號(1-30)和 UUID 兩種方式進行操作。
3. 透過 UUID 進行藍牙連接,最好先進行配對操作。
Android 螢幕適配規則:限定符優先級與資源匹配流程
要做多螢幕適配,第一步得先讓系統「認得出」不同螢幕。Android 提供了一組限定符(qualifier,也有人譯成限定詞),用來描述資源適用於哪一類裝置。 上圖列出常
Android 繪製 QQ 風格登入界面的完整程式碼
先看一下成果: 這個看起來簡單的畫面,實際做下去會碰到四個不大不小的問題: 1、如何為 EditText 設定自定義背景。 2、頁面一打開 EditText 就自
深度解析 Android LayoutInflater:三個參數的真相與源碼剖析
對於做 Android 開發的人來說,LayoutInflater 並不陌生,尤其在 ListView 的 Adapter 的 getView 方法中幾乎必然出現。但對於
1080P手機變卡又耗電?手動降低解析度與密度
1080P手機若跑遊戲或模擬器吃緊,可手動降低解析度。Root後編輯build.prop,把ro.sf.lcd_density從480改為320,再用終端模擬器調整顯示尺