淺談安卓藍牙開發

PS:只是記錄和說明在做藍牙開發過程中遇到一些問題和想法,說錯請諒解

關於藍牙開發,如果不是要實現自動配對和自動連接的話,需要以下步驟

1 . 打開藍牙適配器

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    Toast.makeText(MainActivity.this, "空值", Toast.LENGTH_SHORT).show();
    }
if (!bluetoothAdapter.isEnabled()) {
     bluetoothAdapter.enable(); //強制性打開
    }

2 . 註冊廣播,搜索藍牙

/* 1. 編寫一個繼承廣播的類 */
public class BlueToothReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ( BluetoothDevice.ACTION_FOUND.equals(action) ){
                bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if( !deviceList.contains(bluetoothDevice) ){
             /*private List<BluetoothDevice > deviceList;*/
                    deviceList.add(bluetoothDevice);
             /*private List<String > devices;*/
                    devices.add(bluetoothDevice.getName() + bluetoothDevice.getAddress() + "\n" +
                    bluetoothDevice.getBondState());
                }
            } else if ( BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action) ){
            //這裏showDevices函數是用於顯示在listview中
            showDevices(); 
            return ;
            }
        }
    }

/* 2. 註冊廣播*/
blueToothReceiver = new BlueToothReceiver();
IntentFilter intentFilter = new IntentFilter();      
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);      
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FI
NISHED);
this.registerReceiver(blueToothReceiver, intentFilter);

3 . 搜索藍牙

Intent enable = new 
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);         
enable.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATIO
N, 1200); 
startActivity(enable);
bluetoothAdapter.startDiscovery(); //開始搜索藍牙,通過廣播的形式
/*當然可以不通過廣播的形式去搜索周邊的藍牙,可以通過獲取手機已經綁定的周邊藍牙*/
/*只要實現 BluetoothAdapter.getDefaultAdapter().getBondedDevices();
將獲取到手機綁定的所有的藍牙。
*/

4 . 藍牙設備連接

/*一些事項說明:
* 1. 在一開始做藍牙開發的時候,我是利用兩部手機進行藍牙通訊的。根據官
* 方的API說明,藍牙通訊是需要相同的UUID才能進行連接的。當時用一個SPP
* 的UUID進行連接,發現兩部手機的藍牙無法進行通訊。在想根據
* SerialPortServiceClass_UUID = '{00001101-0000-1000-8000-
* 00805F9B34FB}'這個單詞猜測,該UUID估計只能是實現藍牙模塊用串口跟
* 手機進行連接,拿到藍牙模塊後發現真的是直接連接上去了。。。
* 
* 2. 至於網上其他方法,例如反射機制方法去連接,也是可以的。
* 
* 3. 遇到拋出異常----service discovery failed,不知道是不是一開始
* 沒有正常關閉BluetoothSocket的原因,導致拋出這個異常。
* 
* 4. connection refused。在測試時候發現,由於藍牙適配器有時候是沒
* 正常打開的,導致異常的出現。
* 
* 5. 在進行連接藍牙的時候,經常拋出這兩個異常。網上有人說由於安卓系統
* 存在的bug,估計也不知道是不是。發現只有正確的打開藍牙適配器,正確的
* 連接藍牙,在退出程序時候,將所有打開的接口全部關閉,纔不會發生拋出異
* 常的情況。(自己的想法,錯誤勿噴。)
*/

bluetoothAdapter.cancelDiscovery();
UUID my_uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
        try {
            bluetoothSocket = 
         device.createRfcommSocketToServiceRecord(my_uuid);
            bluetoothSocket.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }

5 . 打開 I/O 流

try {
    InputStream inputStream = bluetoothSocket.getInputStream();
    OutputStram outputStream = bluetoothSocket.getOutputStream();
} catch (IOException e) {
     e.printStackTrace();
}

6 . 關於要實現自動配對和自動連接的,利用反射機制的方法。
可以查看下面這篇文章
藍牙自動配對和連接
關於裏面所說到利用反射機制得到的函數方法,在官方的SDK版本中有一些函數並沒有出現。例如cancelPairingUserInput這個方法,查看幾個最新的SDK版本都沒有這個函數,所以也不知道到底是怎麼實現的。當然可以試試看滴。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章